我想知道这两行代码块之间的区别。
byte[] fileBytes = FileUtils.readFileToByteArray(new File(completeFilePath.toString()));
..
return new FileTransfer(errorFileName, "application/vnd.ms-excel", is);
和
File csvFile = new File(completeFilePath.toString());
InputStream is = new BufferedInputStream(new FileInputStream(csvFile));
return new FileTransfer(errorFileName, "application/vnd.ms-excel", is);
任何一个优点和缺点都欢迎清除细节。 在此先感谢。
答案 0 :(得分:2)
FileTransfer
有多个构造函数,它们需要不同的参数。
您的第一个示例调用构造函数,该构造函数将内容作为字节数组(byte[]
)。
您的第二个示例调用带有InputStream
的构造函数,并将从传递的InputStream
中读取内容。
如果您的文件很大,显然不要使用第一个文件,因为它需要将整个文件读入内存。
第二种方法似乎在所有情况下都更好,除非你还需要文件内容,那么你必须阅读两次。