[英]Does Files.readAllBytes() closes the inputstream after reading the file?
Does this java method close the inputstream after reading the file?这个 java 方法是否在读取文件后关闭输入流?
Files.readAllBytes(Paths.get("file")) Files.readAllBytes(Paths.get(“文件”))
Yes, it closes.是的,它关闭了。 See it in the javadoc .
在javadoc中查看。
Reads all the bytes from a file.
从文件中读取所有字节。 The method ensures that the file is closed when all bytes have been read or an I/O error, or other runtime exception, is thrown.
该方法确保在读取所有字节或抛出 I/O 错误或其他运行时异常时关闭文件。
Note that this method is intended for simple cases where it is convenient to read all bytes into a byte array.
请注意,此方法适用于方便将所有字节读入字节数组的简单情况。 It is not intended for reading in large files.
它不适用于读取大文件。
public static byte[] readAllBytes(Path path) throws IOException {
try (SeekableByteChannel sbc = Files.newByteChannel(path);
InputStream in = Channels.newInputStream(sbc)) {
long size = sbc.size();
if (size > (long)MAX_BUFFER_SIZE)
throw new OutOfMemoryError("Required array size too large");
return read(in, (int)size);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.