aspose file tools*
The moose likes Java in General and the fly likes How to Read the Last Line in the File Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to Read the Last Line in the File" Watch "How to Read the Last Line in the File" New topic
Author

How to Read the Last Line in the File

Vijay Kumar koganti
Ranch Hand

Joined: Jan 23, 2006
Posts: 53
Hi all,

I have a log File which is more than 20MB.I am trying to Write a code which can REad the File and provide me the Last Line in the File..I have tried by Finding the Total Number of Characters in the File and then Decremented the Position to get the Last Line in the File but it was a Major Performance Hit as the File has characters in almost Crores...


Can some one help me..How to read the last Line in the File in some other Way ...It'z very much Urgent....


vijay kumar k.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Hi,

Welcome to JavaRanch!

Please don't post the same question to more than one forum; see our explanation here. I've deleted the other copy of this thread.

As to your question: use RandomAccessFile to read a chunk near the end of the file; look backwards for newline characters. Be sure to hand the case where the last line ends with a newline, as well as the case where it does not.


[Jess in Action][AskingGoodQuestions]
Vijay Kumar koganti
Ranch Hand

Joined: Jan 23, 2006
Posts: 53
Hi Ernest,

import java.io.RandomAccessFile;
public class Aceessfile {
private RandomAccessFile randomfile;
private long position;
int thisCode;
char thisChar;
String thisLine;
public void ReverseFileReader (String filename) throws Exception {
randomfile=new RandomAccessFile(filename,"r");
position=randomfile.length();
System.out.println("Total number of Chars:" +position);
for(; {
if (position < 0) break;
randomfile.seek(position-1);thisCode=randomfile.readByte();
thisChar=(char)thisCode;
if (thisChar == 10 && !(randomfile.readLine() == null)) {
randomfile.seek(position);
thisLine=randomfile.readLine();
System.out.println("The last line is:"+thisLine);
break;
}position--;
}
}

public static void main (String[] args)throws Exception{
new Aceessfile().ReverseFileReader("c:\\a.txt");
}
}


By using this code i am able to Achieve my Result My main concern is i am using position=randomfile.length(); to get the position using which i am able to read the Line but this was a Major Performance Issue, so can you suggest me if there is any other Way that i can attain the Same Result..
Vijay Kumar koganti
Ranch Hand

Joined: Jan 23, 2006
Posts: 53
Can Some one Resolve my Issue...or suggest me
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

I don't see why this would be a "major performance issue." Just for fun, I tried this program on a 78MB file, and as I expected, it ran in a tiny fraction of a second. On any "real" computer, the runtime should not depend on the file size.

If the file is on an extremely fragmented filesystem with a tiny block size, so that a lot of disk access was necessary just to find the last part of the file, I suppose this could become slower, but I can't see it ever being a "major performance issue."
Vijay Kumar koganti
Ranch Hand

Joined: Jan 23, 2006
Posts: 53
Is there any other way other than the way i have tried to Get the Desired Result...I am sure Java has some thing in Store for me, it would be great if you can provide me the address for that.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

There is also java.nio.FileChannel, but be aware that RandomAccessFile, like everything else in java.io, has been reimplemented on top of the java.nio classes, so that if RandomAccessFile is a problem for some reason, FileChannel is likely to cause the same problems. But do let us know how it works out.
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
Originally posted by vijaykumar koganti:
....
My main concern is i am using position=randomfile.length(); to get the position using which i am able to read the Line but this was a Major Performance Issue, so can you suggest me if there is any other Way that i can attain the Same Result..


I just checked it for a 26 MB file and you know it just took 10 milli second



Naseem
[ September 06, 2006: Message edited by: Naseem Khan ]

Asking Smart Questions FAQ - How To Put Your Code In Code Tags
Vijay Kumar koganti
Ranch Hand

Joined: Jan 23, 2006
Posts: 53
Hi..

Thanks for your Suggestions..

But here is a Major challenge now...I need to find out a word 'exception' in the Given File(It might be in multiple places), I need to Retrieve all the Lines where it has word exception in it..

Once again thanks for the Valuable suggestions...

System.currentTimeMillis(); //Can fetch me System Time

How can i fetch the Last Modified time of the File..
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
When accessing files, a good candidate for what you need is the File class. The method you need returns the last time accessed in milliseconds, so if you need a date(hint for what to type of classes to check for), there are some classes that might fit the bill. Or if you have the time and inclination, you can write your own. Writing your own library classes is a great way to become a good programmer. It can be challenging and you can use the existing API class to compare for accuracy and performance.

Disk IO is one of the more time-intesive tasks you can do, but the Java IO classes are quite efficient.

http://java.sun.com/j2se/1.5.0/docs/api/index.html

When stuck, the API docs is a great place to start.
[ September 06, 2006: Message edited by: Rusty Shackleford ]

"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
Vijay Kumar koganti
Ranch Hand

Joined: Jan 23, 2006
Posts: 53
HI..
I am using File.lastModified(); which returns long but i am not able to compare this with the System time i got from the
System.currentTimeMillis();

I think both are returning Long ...How can i compare both of them..The condition should be that if the difference in both the Time stamps is less than 1min it is Successfull and if it is not it is Unsuccessfull..

can you help me out...

No body has told me how to Find a Word 'exception' in the File....
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
long is a primitive so you compare them with the comparison operators(ie <, > , ect), after you get the difference. Converting milliseconds to seconds is straightforward arthimetic, but really not even necessary.

I don't think there is an API to specifically do a search like you need. So to write it yourself, you need an algorithm. On paper, make up a file that is just like one you would use(but much smaller). Then go through it like a computer would to find that string. Your first algorithm will likely be very inefficent, but don't worry too much about that. Get it working, then optimize it, if you need to do that.
[ September 06, 2006: Message edited by: Rusty Shackleford ]
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
Originally posted by vijaykumar koganti:
HI..
I am using File.lastModified(); which returns long but i am not able to compare this with the System time i got from the
System.currentTimeMillis();

I think both are returning Long ...How can i compare both of them..The condition should be that if the difference in both the Time stamps is less than 1min it is Successfull and if it is not it is Unsuccessfull..

can you help me out...

No body has told me how to Find a Word 'exception' in the File....



1. System.currentTimeMillis() will not return you the system time rather it gives you the time elapsed in millisecond between the current time and midnight, January 1, 1970 UTC.

2. I don't know what exactly you are trying to achieve by comparing the differences of two time with one minute?

3. You can use java.util.Scanner class for finding the number of occurances of a given pattern. Its java documentation is very informative.


Naseem
[ September 06, 2006: Message edited by: Naseem Khan ]
Vijay Kumar koganti
Ranch Hand

Joined: Jan 23, 2006
Posts: 53
Thanks for all your replys

but is there any way that i can find the System Time so that i have to compare it with the FileModified Time...

All i need to found is that the File is getting Updated or not..If it is Show me all the Lines that has Exception some where in the Line and i need my Program to print all the Lines..

Thanks in Advance,
vijay
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Naseem]: 1. System.currentTimeMillis() will not return you the system time rather it gives you the time elapsed in millisecond between the current time and midnight, January 1, 1970 UTC.

I would say that is the system time. It's just a matter of what format it's in.

[vijaykumar]: but is there any way that i can find the System Time so that i have to compare it with the FileModified Time...

Both System.currentTimeMillis() and File.lastModifiedTime() return time values measured the same way: "A long value... measured in milliseconds since the epoch (00:00:00 GMT, January 1, 1970". (That's from the API which is, again, extremely useful for this sort of thing.) So you can compare System.currentTimeMillis() and File.lastModifiedTime() directly. Subtracting one from the other gives you the elapsed time (in milliseconds) since the file was modified.


"I'm not back." - Bill Harding, Twister
Vijay Kumar koganti
Ranch Hand

Joined: Jan 23, 2006
Posts: 53
Hi all,

Thanks for your support and clarifications....

regards,
vijay kumar k.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to Read the Last Line in the File