Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reading Data from a file and extracting the data

 
Varshini Priya
Ranch Hand
Posts: 95
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I have a huge log file and I would like to know if I can use file I/O streams to read the entries log files and extract all the data only for a particular day into an other file.

Note: The log file has a specific format in which the first line of the log specifies Date/Time.Also Not all the lines in the logfile has date/Time stamp present in it . When there are errors during a particular time, the exceptions are also thrown in the fiel for that time. I want to extract all the data for a particular day along with the exception details as well.


Please give me ideas interms of how I can acheive the above. Thanks in advance
 
Varshini Priya
Ranch Hand
Posts: 95
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I have tried executing the below code and Im getting an exception





The exception which Im getting is


Exception in thread "main" java.lang.NullPointerException
at java.io.FilterInputStream.available(Unknown Source)
at Fileread.main(Fileread.java:24)




I have tried executing the program in debug mode and Im getting the error while executing the print statement. It is throwing a null pointer exception. Please advise how to fix this. Thanks
 
Rob Spoor
Sheriff
Pie
Posts: 20531
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out line 23: "dis = new DataInputStream(dis);". I'm sure you're using the wrong argument there.
 
Varshini Priya
Ranch Hand
Posts: 95
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks Rob, You are write, I have corrected the line 23





Still the program is throwing some exceptions



java.io.IOException: Stream closed
at java.io.BufferedInputStream.getInIfOpen(Unknown Source)
at java.io.BufferedInputStream.available(Unknown Source)
at java.io.FilterInputStream.available(Unknown Source)
05/21/2010 10:05:48 INFO [main] [LMS:ConfigurationCache] - --preLoad passwordPolicyConfiguration.
at Fileread.main(Fileread.java:24)




It is printing only one line from the text file and it is throwing IO exceptions.
 
Rob Spoor
Sheriff
Pie
Posts: 20531
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
FilterInputStream.close() closes the InputStream it wraps. Since both BufferedInputStream and DataInputStream extend FilterInputStream you are trying to close the original FileInputStream not once but thrice. The closing, if successful, would be as follows:
- fis.close() -> close fis
- bis.close() -> close bis so close fis
- dis.close() -> close dis so close bis so close fis

So you'll only need one of the three close statements. You can even choose which one since in the end all will close the only one that actually needs closing - fis. (However, I usually tend to close the outermost, in this case dis. While for InputStream it wouldn't matter much for OutputStreams the wrapping streams may still want to write something.)
 
Varshini Priya
Ranch Hand
Posts: 95
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob. I did include only one close statement in the code to close the Inputstream. I have closed the outermost one. But still it throws the below exceptions.



at java.io.BufferedInputStream.getInIfOpen(Unknown Source)
at java.io.BufferedInputStream.available(Unknown Source)
at java.io.FilterInputStream.available(Unknown Source)
at Fileread.main(Fileread.java:24)


 
Prabhakar Reddy Bokka
Ranch Hand
Posts: 197
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
move the closing statements to out side of while loop.

and close in the reverse order.

dis.close();
bis.close();
fis.close();

It works fine.
 
Varshini Priya
Ranch Hand
Posts: 95
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Thanks it worked. Im trying to extract specific data from a file for a particular day, between a particular timeframe i.e for eg I wanted to extract data from on the 07-jun for the timeframe between 2:00pm - 4:00pm . Can you please help me how to extract this. Thanks
 
Rob Spoor
Sheriff
Pie
Posts: 20531
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's stick to your other thread for that, shall we?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic