Go Back   Java Programming Forums > Learning Java > Java Code Snippets and Tutorials

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 19-05-2008, 12:44 PM
JavaPF's Avatar
mmm.. coffee
 
5 Highscores

Join Date: May 2008
Location: United Kingdom
Posts: 1,211
Thanks: 61
Thanked 66 Times in 64 Posts
JavaPF is someone you want to know!JavaPF is someone you want to know!JavaPF is someone you want to know!

I'm feeling Inspired
Default How to Read the last line of a file

This program will output the last line of the file 'file.txt'

Java Code
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
 
public class ReadLastLine{
 
 public static void main(String[] args) throws Exception {
 
  FileInputStream in = new FileInputStream("file.txt");
  BufferedReader br = new BufferedReader(new InputStreamReader(in));
 
  String strLine = null, tmp;
 
  while ((tmp = br.readLine()) != null)
  {
     strLine = tmp;
  }
 
  String lastLine = strLine;
 
  System.out.println(lastLine);
 
  in.close();
 }
}



__________________
Don't forget to add code tags around your code:

Forum Tip: Add to peoples reputation () by clicking the button on their useful posts.
Reply With Quote Share this thread on Facebook
Sponsored Links
  #2 (permalink)  
Old 09-09-2009, 06:43 PM
Junior Member
 

Join Date: Sep 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
JavaGuru is on a distinguished road
Default Re: How to Read the last line of a file

Hi,
I found there is another way of doing this.

Java Code
RandomAccessFile rfile = new RandomAccessFile(file,"r"); 
      
rfile.seek(file.length()); 
Third One -
FileInputStream fis=new FileInputStream("D:\\ test\\sample.txt");
                  
String fileContent = new Scanner(fis).useDelimiter("\\Z").next()
Source : Java : Quick way to read to get the last line from a File : "java" Category at Simply Share It
Reply With Quote
  #3 (permalink)  
Old 10-09-2009, 07:26 AM
Json's Avatar
Super Moderator
 

Join Date: Jul 2009
Location: Manchester, United Kingdom
Posts: 861
Thanks: 31
Thanked 112 Times in 108 Posts
Json will become famous soon enoughJson will become famous soon enough

I'm feeling Happy
Default Re: How to Read the last line of a file

Yeah if you use the BufferedReader and you have a file which might be a couple of gigabyte in size, first of all your JVM will need a lot of memory and it will take you a while to do.

RandomAccess might be the better way to go.

// Json
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to Read a Portion of a File jazz2k8 File I/O & Other I/O Streams 3 07-07-2009 09:16 PM
How to Send command line arguments in Eclipse JavaPF Java Code Snippets and Tutorials 0 23-04-2009 04:37 PM
How to Read a file line by line using the Scanner class JavaPF Java Code Snippets and Tutorials 0 17-04-2009 12:34 PM
How to Read a file line by line - java.io.BufferedReader JavaPF Java Code Snippets and Tutorials 0 19-05-2008 11:32 AM


100 most searched terms
Search Cloud
2 dimensional arraylist java 2d arraylist java actionlistener actionlistener in java actionlistener java actionlistener jbutton addactionlistener addactionlistener java avatar hardware id convert double to integer java double format java double to int java double to integer in java double to integer java eclipse shortcut keys eclipse tutorial for beginners exception in thread "awt-eventqueue-0" java.lang.outofmemoryerror: java heap space exception in thread "main" java.lang.outofmemoryerror: java heap space format double java get mouse position java hardware id avatar java 2 dimensional arraylist java 2d arraylist java actionlistener java addactionlistener java button actionlistener java convert double to int java double format java double to int java double to integer java for beginner eclippse java format double java forum java forums java get mouse position java ipod touch java jbutton java list to map java mouse position java programming forum java programming forums java sendkeys java.lang.reflect.invocationtargetexception java.util.arraylist jbutton actionlistener jbutton java programming forums string to int java two dimensional arraylist java writing apps for ipod touch

All times are GMT. The time now is 09:20 PM.
Powered by vBulletin® Copyright ©2000-2009, Jelsoft Enterprises Ltd.