• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reset Input Stream

 
Sidharth Dash
Greenhorn
Posts: 11
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all

I am using an Inputstream to read data from a file that contain around 3.2mill. records.
After reading every 100000 i want to close the input stream to releases any system resources associated with the stream and again open it for further reading.
but i want to continue reading from the next line .
i know about the mark() and reset() method but i dont thing that would be helpful in this problem.

P.S: All i want to do is realese all system resourses associated with the input stream.
 
Stephan van Hulst
Bartender
Posts: 6337
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What system resources are you talking about?

I see no reason not to process the entire stream one go.
 
Sidharth Dash
Greenhorn
Posts: 11
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ya you are right,But i am using a file having record length of 28000 and 3.2 mill records. and i have faced these situations:

1)no. of records processed = 1 mill. with normal execution
2)no. of records processed = 2.2 mill. when reset the output stream after every 100000 record processed.

so i just want to test the execution after reseting the input stream after processing of each 100000 records.
I may sound silly but io want to try it.
any help will do good.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's possible, but could be tricky. You can use the skip() method on InputStream to do it - be sure to read the API for that one, and use the method in a loop to ensure that you actually skip all the bytes you want to skip. You can also use seek(long) in RandomAccessFile, or position(long) in FileChannel, if you prefer, and those methods shouldn't need a loop.

The problem though is that all these methods require you to know the offset, in bytes, of the place in the file you want to go to. Depending on what's in the file, that may be tricky to keep track of. If all records are of a known fixed length, great, you can calculate that pretty easily. If it's a text file and lines are of variable length, you need to keep track of how many bytes were in each line. Which probably requires you know something about the character encoding used in the file - is it 1 byte per character, or 2 bytes, or is it variable (as in UTF-8)? In the last case, it's tougher to work out how many bytes are in each line. Also, you need to know how many bytes are in each line separator - does it use \n, \r\n, or \n\r? Or something else? This is also something that can vary, though usually it doesn't.

It's also tempting to insert a FilterInputStream into the input stream chain, allowing you to count how many bytes have been read from the underlying stream. However this doesn't exactly correlate with how many bytes have been consumed by the outermost stream - intermediate streams may read ahead, and cache what they haven't used yet. I don't see a good way to get accurate results from this.

So anyway, yeah, it's possible, but may well be more trouble than it's worth. Especially if you just "want to try it" but don't necessarily need it.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would also submit that it seems likely there's a bug in the code, and ultimately, finding and fixing that bug would be your best course of action. Can you show us the code to loop through the file?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic