Using Java 7 API to read a file byte by byte

Deadly 提交于 2019-12-01 11:40:00

问题


I have a 2 gb file I want to read in Java (actually four 2gb files). And so there's a new feature in Java 7 that can let me read all the bytes at once.

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

public class reconstructor {

    public static void main(String[] args) throws IOException {

        Path p = Paths.get("test.txt");     
        for (int i = 0; i < 1; i++) {
            byte[] b = Files.readAllBytes(p);
            Files.write(p, b, StandardOpenOption.APPEND);
        }

    }

}

This is a dumb program that wil read a file with a single byte pre entered in it and continuously read that file and append what it read back onto the same file. Now obviously, the RAM is not big enough to read a 2gb file at one time, let alone four of them, so I was wondering if there was any quick way, without using external libraries (unless that is the only way) to read four files byte by byte so that the RAM does not get overloaded (otherwise I end up with a Java heap error).


回答1:


Reading byte by byte is the other extreme solution, and will be very inefficient. You should simply use a BufferedInputStream, and read the bytes chunk by chunk.

Read the Java IO tutorial about byte streams.




回答2:


Try Memory Mapped I/O, the last example in this tutorial: http://www.polarsparc.com/pdf/MemoryMappedIOinJava.pdf



来源:https://stackoverflow.com/questions/13111419/using-java-7-api-to-read-a-file-byte-by-byte

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!