Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Java program to read last line of a file

  1. #1
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Java program to read last line of a file

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

    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();
     }
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.


  2. #2
    Junior Member
    Join Date
    Sep 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to Read the last line of a file

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

    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

  3. #3
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    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

Similar Threads

  1. How to Read a Portion of a File in Java?
    By jazz2k8 in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: July 7th, 2009, 04:16 PM
  2. How to Send command line arguments in Eclipse?
    By JavaPF in forum Java JDK & IDE Tutorials
    Replies: 0
    Last Post: April 23rd, 2009, 11:37 AM
  3. Reading a file line by line using the Scanner class
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: April 17th, 2009, 07:34 AM
  4. How to Read a file line by line using BufferedReader?
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: May 19th, 2008, 06:32 AM