JavaFiles.readAllBytes(Path) 遇见的坑

Posted wumengchao1234

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaFiles.readAllBytes(Path) 遇见的坑相关的知识,希望对你有一定的参考价值。

Files.readAllBytes(Path)方法把整个文件读入内存,此方法返回一个字节数组,还可以把结果传递给String的构造器,以便创建字符串输出。

在针对大文件的读取的时候,可能会出现内存不足,导致堆溢出。

最后还是采用原始的IO方式去读写文件,将文件读入byt数组中

InputStream input = null;
byte[] byt = null;
try {
File file = localPath.toFile();
input = new FileInputStream(file);

byt = new byte[input.available()];

input.read(byt);
} catch (FileNotFoundException e) {
logger.info("file not find!");
} catch (IOException e) {
logger.info("IOException :" + e);
}finally {
input.close();
}















以上是关于JavaFiles.readAllBytes(Path) 遇见的坑的主要内容,如果未能解决你的问题,请参考以下文章