是否存在 FileChannel.size()
和 File.length()
不同的情况?请参阅下面的代码示例:
File file = new File(name);
FileInputStream stream = new FileInputStream(file);
FileChannel channel = stream.getChannel();
long channel_size = channel.size();
long file_length = file.length();
最佳答案
它们可以不同,因为 new File(name).length()
始终检查给定的路径。当您使用 FileInputStream
时,无论发生什么情况,您都会附加到该文件。
例如,在 Linux 中您可以重命名、替换或删除正在使用的文件。 FileInputStream
将继续提供原始文件的大小,而 File
将提供替换它的内容(如果有)。
关于Java FileChannel.size() 与 File.length(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21293031/