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