file - 如何在 Rust 中获取已打开文件的大小?

标签 file rust

如何获取声明为 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/

相关文章:

java - 如何使用 JFileChooser 保存/加载 ArrayList 数组?

java - 在 5000 多个文本文件中搜索特定字符串。

rust - Rust 模式匹配如何确定绑定(bind)变量是引用还是值?

iterator - 将父特征限制为引用

linux - 是否有 "lock files"的标准 Linux 库?

c - 如何获取文件中当前单词前后的单词?

rust - 调用iter()。sum(): the trait bound `f64: std::iter::Sum<&std::vec::Vec<f64>>` is not satisfied时出错

performance - 如何禁用 Rust 中的循环展开?

rust - 如何解决 RefCell 问题?

php - 如何使用 PHP 替换 php 页面中的 js 文件路径?