如何获取声明为 File
变量的文件的大小/长度?
有一个选项可以通过 fs::metadata
使用文件名获取它:
let x = fs::metadata(file_path)?.len();
但是我已经创建并编辑了一个文件:
let mut my_file = File::create(file_path).unwrap();
my_file.write_all(buffer) // buffer is a data which comes from network and put into Vec<u8>
// From here, does it possible to get my_file length?
// Looking something like my_file.len();
是否有任何函数可以给出文件
的长度?
最佳答案
std::fs::File
提供了一个 metadata
方法。它可以像这样与您的 my_file
一起使用:
my_file.metadata().unwrap().len();
关于file - 如何在 Rust 中获取已打开文件的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54303398/