?
Solved

file reading from last line

Posted on 2005-05-16
16
Medium Priority
?
2,093 Views
Last Modified: 2010-05-19
Hello,
i need to scan a log file and print the the variables in it.
Below is a sample log txt file which gets updated when its software is running.
So I want a sample java function which can go the last line, scans lines until it encounters #1000000 (LAST VARIABLE IN 4 LINE FROM LAST  BUT THIS CHANGES)
AND PRINT ALL LINES BELOW IT.

So here i must able to print values like 192.168.100.24, 192.168.100.22


U#1115927273#192.168.100.20#1#V#0#192.168.100.20#NA#1000000
U#1115927273#192.168.100.22#21#V#1#192.168.100.22#NA#2149
U#1115927273#192.168.100.24#22#I#1#192.168.100.24#NA#2950
D#1115927276#192.168.100.24#22#I#1#192.168.100.24#NA#-14
A#1115927297#192.168.100.24#21#V#1#192.168.100.24#NA#3000
U#1115927330#192.168.100.24#21#V#1#192.168.100.24#NA#2339
U#1115927330#192.168.100.20#1#V#0#192.168.100.20#NA#1000000
U#1115927330#192.168.100.22#22#I#1#192.168.100.22#NA#2910
D#1115927333#192.168.100.22#22#I#1#192.168.100.22#NA#-25
U#1115927383#192.168.100.20#1#V#0#192.168.100.20#NA#1000000
U#1115927383#192.168.100.24#22#I#1#192.168.100.24#NA#2990
U#1115927487#192.168.100.20#1#V#0#192.168.100.20#NA#1000000
U#1115927330#192.168.100.22#22#I#1#192.168.100.22#NA#2910
U#1115927487#192.168.100.24#22#I#1#192.168.100.24#NA#2990
D#1115927490#192.168.100.24#22#I#1#192.168.100.24#NA#-5


Thanks,
0
Comment
Question by:talktome_prashanth
  • 6
  • 4
  • 3
13 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 1000 total points
ID: 14011151
Use RandomAccessFile. Seek to the end of the file and keep seeking backwards until you've read the right number of line breaks. You can then read and collect from that point
0
 
LVL 13

Expert Comment

by:petmagdy
ID: 14011381
Hi talktome_prashanth,

this is Randam access to file example:

http://javaalmanac.com/egs/java.io/UseRandomAccessFile.html

Cheers!
0
 

Author Comment

by:talktome_prashanth
ID: 14011408
Thank You,
Can Anyone post some sample code for the above log file...
i guess we need tokenizer to split...
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 86

Expert Comment

by:CEHJ
ID: 14011437
>>i guess we need tokenizer to split...

No you don't. Just follow the procedure i mentioned. When you've sought back far enough you can read it line by line
0
 
LVL 13

Expert Comment

by:petmagdy
ID: 14011453
you can split using this example:

        String stream = "D#1115927490#192.168.100.24#22#I#1#192.168.100.24#NA#-5";
        String[] splitted = stream.split("[#]+");
        for(int i=0 ; i < splitted.length; i++) {
            System.out.println(splitted[i]);
        }
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 14011491
>>i guess we need tokenizer to split...

Oh i see what you mean - you mean each line. (See above)
0
 

Author Comment

by:talktome_prashanth
ID: 14011550
Here the problem is that there can be any number of lines before it encounters 1000000
here in the example it is only 3 lines but in reality there can be any lines.

What i was thinking was

seek to end of file
loop
{
read line
tokeniser(line by #)
check for last token.
if
lasttoken is not 1000000 then copy that line to an string buffer array   //we can split and print
else if lasttoken is 1000000 then copy that line and terminate reading file.  // yeah we have reached line where we have 1000000 so stop reading
}

Is my approach correct ? if yes can somebody help me in sample code...
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 14011572
>>Is my approach correct ?

Almost, you're still going to have to seek backwards first as you can't read lines backwards
0
 

Author Comment

by:talktome_prashanth
ID: 14011598
Ok thank you, i guess my problem is solved...
but last thing can i get some sample code???

thanks
0
 
LVL 13

Assisted Solution

by:petmagdy
petmagdy earned 1000 total points
ID: 14011650
talktome_prashanth,

why don't u use an open source component like the one mentioned in this thread, those components are easy to use:

http://www.codecomments.com/archive245-2004-6-221785.html
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 14011731
Actually, on second thoughts, it may be easier to ignore the lines thing and just seek to position filesize - n then read the whole of the end of the file into a String. What OS are you using btw?
0
 

Author Comment

by:talktome_prashanth
ID: 14011758
>> What OS are you using btw?

Its on Windows XP

log file is from AODV routing for Adhoc networks.
Here i wanted to know what machines are my neighbours. AODV automatically creates a log entry for each nieghbour in its log file. So we are checking log file to print out the neighbours. 1000000 is self machine i.e machine where AODV is running and all other entries are other machines.

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 14011839
If the position of the line varies a lot and/or the log file is not enormous, it may be easier just to scan the file forwards
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month17 days, 13 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question