开发者

Read and a write a file in a reverse order - Java

开发者 https://www.devze.com 2023-03-15 19:01 出处:网络
I have a very big file (might be even 1G) that I want to create a new file from in a reversed order (in Java).

I have a very big file (might be even 1G) that I want to create a new file from in a reversed order (in Java). For example:

Original file:

This is the first line
This is the 2nd line
This is the 3rd line

The reversed file:

This is the 3rd line
This is the 2nd line
This is the first line

Since the file is very big, loading the entire file to memory at once and reversing the order there might be proble开发者_开发问答matic (there is a limit to the memory I can use). How can I achieve this in Java?

Thanks


Nothing very direct, I'm afraid. But you can easily create some (say) ReverseBufferedRead class wrapping a RandomAccessFile.

See also here.


Read the file by chunks of few hundreds lines, reverse the order of lines in the chunks and write them to temporary files. Then join the temporary files in the reverse order and clean up.

In other words, use disk instead of memory.


I would propose making a RandomAccessFile for the output and using setLength() to make it appropriately sized.

Then start scanning the original file and write it out in chunks starting at the end of the RandomAccessFile in reverse.

Java-ish Pseudo:

out.seek(size_of_out_file); //seek to end
RandomAccessFile out = new RandomAccessFile("out_fname", "rw");
out.setLength(size_of_file_to_be_reversed)
File in = new File ("in_fname");
while (hasMoreData(in)){
  String chunk = in.readsize();
  out.seekBackwardsBy(chunk.length());
  out.write(chunk.reverse);
  out.seekBackwardsBy(chunk.length());
}


Reading a file line-by-line in reverse order is fundamentally tricky.

It's not too bad if you've got a fixed width encoding. It's feasible if you've got a variable width encoding which you can detect the first byte of etc (e.g. UTF-8). It's virtually impossible to do efficiently if the encoding is variable width with no sensible way of determining boundaries (or if it uses "shifting" for example).

I have an implementation in C# in another question, but it would take a fair amount of effort to port that to Java.


If you use the RandomAccessFile like leonbloy suggested you can use a FileChannel

to skip to the end of the file, you can then read the line and write it to another file.

There is a simple example here in the Java tutorials: example


I would assume you know how to read a file. One way i would advise you do it is with an ArrayList of generic type string. So you read each line of the file and store it in that list. After reading you print the list out or do whatever you want to.

Just wrote something that might be of help here : http://pastebin.com/iWTVrAvm


Read using RandomAccessFile - position the file using randomAccesFile.length()and write using BufferedWriter


A better solution is use a ReversedLinesFileReader provided in Apache Commons IO package. Look at the API here https://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/input/ReversedLinesFileReader.html

0

精彩评论

暂无评论...
验证码 换一张
取 消