简体   繁体   English

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

[英]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.

相关问题 为什么Files.readAllBytes首先读取bufsize为1? - Why does Files.readAllBytes first read with a bufsize of 1? JAVA Files.readAllBytes()而不更改字符集 - JAVA Files.readAllBytes() without changing charset FileUtils.readFileToByteArray与Files.readAllBytes - FileUtils.readFileToByteArray vs Files.readAllBytes 为什么 Files.readAllBytes() 不接受编码参数? - Why is Files.readAllBytes() not accepting an encoding parameter? 将使用 Files.readAllBytes 加载的字节打印到数组中 - Printing bytes loaded with Files.readAllBytes into array Files.readAllBytes与Files.lines获得MalformedInputException - Files.readAllBytes vs Files.lines getting MalformedInputException Java 的 Files.readAllBytes() 的 Javascript 等价物是什么? - What is the Javascript equivalent of Java's Files.readAllBytes()? BigInteger(line.getBytes())和Files.readAllBytes()提供不同的输出 - BigInteger(line.getBytes()) and Files.readAllBytes() giving different outputs 我正在尝试加载500Mb文件(Files.readAllBytes),并且我需要大于2 Gb的堆大小。 为什么? - I'm trying to load 500Mb file (Files.readAllBytes) and I need more than 2 Gb Heap size. Why? byte [] Files.readAllBytes()和FileChannels.map之间的区别(ByteBuffer / MappedByteBuffer) - Difference between byte[] Files.readAllBytes(), and FileChannels.map (ByteBuffer/MappedByteBuffer)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM