• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reading Data from a file and extracting the data

 
Ranch Hand
Posts: 100
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 100
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22780
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 100
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22780
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 100
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)


 
Ranch Hand
Posts: 198
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 100
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22780
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's stick to your other thread for that, shall we?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic