my dog learned polymorphism*
The moose likes Beginning Java and the fly likes Reading Data from a file and extracting the data Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Reading Data from a file and extracting the data " Watch "Reading Data from a file and extracting the data " New topic
Author

Reading Data from a file and extracting the data

Varshini Priya
Ranch Hand

Joined: Feb 17, 2008
Posts: 93

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

Joined: Feb 17, 2008
Posts: 93

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

Joined: Oct 27, 2005
Posts: 19684
    
  20

Check out line 23: "dis = new DataInputStream(dis);". I'm sure you're using the wrong argument there.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Varshini Priya
Ranch Hand

Joined: Feb 17, 2008
Posts: 93


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

Joined: Oct 27, 2005
Posts: 19684
    
  20

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

Joined: Feb 17, 2008
Posts: 93

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

Joined: Jul 26, 2005
Posts: 189

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.


SCJP 5, SCWCD 5
Varshini Priya
Ranch Hand

Joined: Feb 17, 2008
Posts: 93

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

Joined: Oct 27, 2005
Posts: 19684
    
  20

Let's stick to your other thread for that, shall we?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reading Data from a file and extracting the data