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
 
7 Highscores

Join Date: May 2008
Location: United Kingdom
Posts: 1,581
Thanks: 103
Thanked 93 Times in 86 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 Stressed
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 syntax highlighted code tags around your code: [highlight=Java] code here [/highlight]

Forum Tip: Add to peoples reputation () by clicking the button on their useful posts.
Reply With Quote Share this thread on Facebook
Sponsored Links
Java Training from DevelopIntelligence
  #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: 1,181
Thanks: 55
Thanked 137 Times in 133 Posts
Json will become famous soon enoughJson 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
2d arraylist java actionlistener actionlistener in java actionlistener java addactionlistener addactionlistener in java addactionlistener java applications of oops could not create java virtual machine xp double format java double to int 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.nullpointerexception exception in thread "main" java.lang.outofmemoryerror: java heap space format double java get mouse position java java 2d arraylist java actionlistener java addactionlistener java convert list to map java double format java double formatting java double to int java double to integer java format double java forum java forums java get mouse position java list to map java mouse position java programming forum java programming forums java programming help java sendkeys java two dimensional arraylist java.lang.classformaterror: truncated class file java.lang.outofmemoryerror: java heap space java.util.arraylist jbutton actionlistener jtextarea font jtextfield font size jxl.read.biff.biffexception: unable to recognize ole stream programming mutators and generics two dimensional arraylist java writing ipod apps

All times are GMT. The time now is 07:22 AM.
Powered by vBulletin® Copyright ©2000-2009, Jelsoft Enterprises Ltd.