1

Possible Duplicate:
Java: Quickly read the last line of a text file?

Our mainframes system is sending a file which has header , detail records and a trailer. I want to persist these details records in to my database only if

  1. The file is a valid file - meaning one with valid header, detailed record and trailer.
  2. The number of detailed records count is equal to count in trailer record.

My goal is to read the last line first without reading the entire 1GB file.

AAGSDBGBBM337 2012-03-16165620000001 - Header format
Detailed Record 1
Detailed Record 2
Detailed Record 3
ZZGSDBGBBM337 2012-03-161656200000010000003 - Trailer format

Any suggestions ?

Community
  • 1
  • 1
aditya86c
  • 57
  • 2
  • 12
  • 1
    Maybe this will suit your needs: http://www.brilliantsheep.com/reading-the-last-line-of-a-file-in-java-through-random-access/ – Werner Kvalem Vesterås Oct 09 '12 at 14:22
  • Read last KB, line shouldn't be longer than than. Then split by `\n` and take last item. – Tomasz Nurkiewicz Oct 09 '12 at 14:23
  • read last Nkb (suppose line can't be greater then 2kb), and find first '\n' from the end – octo Oct 09 '12 at 14:23
  • Is it possible for you to execute system commands out of the java program? – Ancaron Oct 09 '12 at 14:23
  • You need to clarify about "sending", "java-ee" and "networking": Are you talking about files on the file system (as the title implies) or about streamed data over a network socket (as the networking tag implies) or something else (possibly a combination of both?). – Joachim Sauer Oct 09 '12 at 14:25
  • @Werner Vesterås : nice explanation.Thanks for all the suggestions.It means a lot to me.This question has been asked couple of times but I didn't see right explanation. – aditya86c Oct 09 '12 at 15:57

3 Answers3

1

Have you looked at the Apache Commons IO Tailer class ?

Edit: Looking at the source, it appears to only identify new lines appended to a file, rather than letting you read the last line (say) of an existing file. So as it stands it's not suitable. However it would be trivial to modifiy it to look backwards upon performing the initial seek()

shareef
  • 9,255
  • 13
  • 58
  • 89
Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
1

You can use the skip method of the FileInputStream to skip the first n bytes of your file. You can also use the RandomAccessFile with the same purpose.

Gilberto Torrezan
  • 5,113
  • 4
  • 31
  • 50
0

please find the link in the first comment by Werner Vesterås which solved my problem

Every others suggestions did make sense and thanks for posting.

aditya86c
  • 57
  • 2
  • 12