gpt4 book ai didi

java - Files.readAllBytes() 读取文件后是否关闭输入流?

转载 作者:行者123 更新时间:2023-12-01 19:33:51 26 4
gpt4 key购买 nike

这个java方法在读取文件后是否关闭输入流?

Files.readAllBytes(Paths.get("文件"))

最佳答案

是的,它关闭了。请参阅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.

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);
}
}

关于java - Files.readAllBytes() 读取文件后是否关闭输入流?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58534138/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com