Fastest Java way to remove the first/top line of a file (like a stack)

Posted by christangrant on Stack Overflow See other posts from Stack Overflow or by christangrant
Published on 2010-04-02T02:53:48Z Indexed on 2010/04/02 3:13 UTC
Read the original article Hit count: 243

Filed under:
|

I am trying to improve an external sort implementation in java.

I have a bunch of BufferedReader objects open for temporary files. I repeatedly remove the top line from each of these files. This pushes the limits of the Java's Heap. I would like a more scalable method of doing this without loosing speed because of a bunch of constructor calls.

One solution is to only open files when they are needed, then read the first line and then delete it. But I am afraid that this will be significantly slower.

So using Java libraries what is the most efficient method of doing this.

--Edit--

For external sort, the usual method is to break a large file up into several chunk files. Sort each of the chunks. And then treat the sorted files like buffers, pop the top item from each file, the smallest of all those is the global minimum. Then continue until for all items. http://en.wikipedia.org/wiki/External_sorting

My temporary files (buffers) are basically BufferedReader objects. The operations performed on these files are the same as stack/queue operations (peek and pop, no push needed).

I am trying to make these peek and pop operations more efficient. This is because using many BufferedReader objects takes up too much space.

© Stack Overflow or respective owner

Related posts about java

Related posts about file