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.
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.
Joined: Mar 05, 2008
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?