|
||
|
||||
|
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.
|
|
|||
|
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()
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
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 |