pub struct FileUtil;Expand description
文件常规操作,如读写、目录创建、删除等
Implementations§
Source§impl FileUtil
impl FileUtil
Sourcepub fn list(path: &Path, recurse: bool) -> Vec<String>
pub fn list(path: &Path, recurse: bool) -> Vec<String>
罗列指定路径下的子目录及文件。
参数 path 是要罗列内容的目标路径。
参数 recurse 决定是否以递归方式罗列子文件夹内的内容。
返回值是一个包含所有子目录及文件路径的 Vec<String>。
如果在读取目录时发生错误,将返回一个空的 Vec。
Sourcepub fn metadata(file_path: &str) -> Result<Metadata>
pub fn metadata(file_path: &str) -> Result<Metadata>
获取指定文件的元数据信息
该函数封装了标准库中的 fs::metadata 方法,用于获取文件的基本信息, 包括文件大小、创建时间、修改时间、文件类型等元数据。
§参数
file_path- 要获取元数据的文件路径字符串引用
§返回值
返回一个 io::Resultstd::fs::Metadata 类型:
- 成功时返回包含文件元数据的 Metadata 对象
- 失败时返回对应的 IO 错误信息
§错误处理
当文件不存在或没有访问权限时,会返回相应的 IO 错误
Sourcepub fn last_midified(file_path: &str) -> Result<String>
pub fn last_midified(file_path: &str) -> Result<String>
Sourcepub fn read_string(path: &Path) -> Result<String, Error>
pub fn read_string(path: &Path) -> Result<String, Error>
读取本地文件内容并以UTF-8字符串形式返回。
参数 path 是要读取的文件路径。
如果文件成功打开并读取,将返回包含文件内容的 String。
如果在打开或读取文件时发生I/O错误,将返回对应的 io::Error。
pub fn read_string_by_iter(file_path: &str) -> Result<impl Iterator, Error>
Sourcepub fn read_bytes(path: &Path) -> Result<Vec<u8>, Error>
pub fn read_bytes(path: &Path) -> Result<Vec<u8>, Error>
读取本地文件内容并以字节数组形式返回。
参数 path 是要读取的文件路径。
如果文件成功打开并读取,将返回包含文件内容的 Vec<u8>。
如果在打开或读取文件时发生I/O错误,将返回对应的 io::Error。
Sourcepub fn write_string(path: &Path, content: String) -> Result<()>
pub fn write_string(path: &Path, content: String) -> Result<()>
将给定的字符串内容以覆盖方式写入到指定路径的文本文件。
参数 path 是要写入的文件路径。
参数 content 是要写入文件的字符串内容。
如果文件成功创建并写入内容,将返回 Ok(())。
如果在创建或写入文件时发生I/O错误,将返回对应的 io::Error。
Sourcepub fn append_string(path: &Path, content: String) -> Result<()>
pub fn append_string(path: &Path, content: String) -> Result<()>
将给定的字符串内容以追加方式写入到指定路径的文件。
参数 path 是要写入的文件路径。
参数 content 是要追加到文件的字符串内容。
如果文件成功打开并追加内容,将返回 Ok(())。
如果在打开或写入文件时发生I/O错误,将返回对应的 io::Error。
Sourcepub fn write_bytes(path: &Path, bytes: &[u8]) -> Result<()>
pub fn write_bytes(path: &Path, bytes: &[u8]) -> Result<()>
将给定的字节数组内容写入到指定路径的文件。
参数 path 是要写入的文件路径。
参数 bytes 是要写入文件的字节数组内容。
如果文件成功创建并写入内容,将返回 Ok(())。
如果在创建或写入文件时发生I/O错误,将返回对应的 io::Error。
Sourcepub fn create_dir_with_parents(dir_path: &str) -> Result<()>
pub fn create_dir_with_parents(dir_path: &str) -> Result<()>
创建目录及其所有必要的父目录
Sourcepub fn delete_file(file_path: &str) -> Result<()>
pub fn delete_file(file_path: &str) -> Result<()>
删除单个文件
Sourcepub fn delete_directory(dir_path: &str) -> Result<()>
pub fn delete_directory(dir_path: &str) -> Result<()>
删除含有子文件的目录