FileUtil

Struct FileUtil 

Source
pub struct FileUtil;
Expand description

文件工具结构体

Implementations§

Source§

impl FileUtil

Source

pub fn list(path: &Path, recurse: bool) -> Vec<String>

罗列指定路径下的子目录及文件。

参数 path 是要罗列内容的目标路径。 参数 recurse 决定是否以递归方式罗列子文件夹内的内容。

返回值是一个包含所有子目录及文件路径的 Vec<String>。 如果在读取目录时发生错误,将返回一个空的 Vec

Source

pub fn metadata(file_path: &str) -> Result<Metadata>

获取指定文件的元数据信息

该函数封装了标准库中的 fs::metadata 方法,用于获取文件的基本信息, 包括文件大小、创建时间、修改时间、文件类型等元数据。

§参数
  • file_path - 要获取元数据的文件路径字符串引用
§返回值

返回一个 io::Resultstd::fs::Metadata 类型:

  • 成功时返回包含文件元数据的 Metadata 对象
  • 失败时返回对应的 IO 错误信息
§错误处理

当文件不存在或没有访问权限时,会返回相应的 IO 错误

Source

pub fn last_midified(file_path: &str) -> Result<String>

获取指定文件的最后修改时间

§参数
  • file_path - 要查询的文件路径字符串引用
§返回值

返回 Result<String, io::Error> 类型:

  • 成功时返回格式化的时间字符串,格式为 “YYYY-MM-DD HH:MM:SS”
  • 失败时返回对应的 IO 错误
§错误处理

当文件不存在或无法访问时,会返回相应的 IO 错误

Source

pub fn read_string(path: &Path) -> Result<String, Error>

读取本地文件内容并以UTF - 8字符串形式返回。

参数 path 是要读取的文件路径。

如果文件成功打开并读取,将返回包含文件内容的 String。 如果在打开或读取文件时发生I/O错误,将返回对应的 io::Error

Source

pub fn read_bytes(path: &Path) -> Result<Vec<u8>, Error>

读取本地文件内容并以字节数组形式返回。

参数 path 是要读取的文件路径。

如果文件成功打开并读取,将返回包含文件内容的 Vec<u8>。 如果在打开或读取文件时发生I/O错误,将返回对应的 io::Error

Source

pub fn write_string(path: &Path, content: String) -> Result<()>

将给定的字符串内容以覆盖方式写入到指定路径的文本文件。

参数 path 是要写入的文件路径。 参数 content 是要写入文件的字符串内容。

如果文件成功创建并写入内容,将返回 Ok(())。 如果在创建或写入文件时发生I/O错误,将返回对应的 io::Error

Source

pub fn append_string(path: &Path, content: String) -> Result<()>

将给定的字符串内容以追加方式写入到指定路径的文件。

参数 path 是要写入的文件路径。 参数 content 是要追加到文件的字符串内容。

如果文件成功打开并追加内容,将返回 Ok(())。 如果在打开或写入文件时发生I/O错误,将返回对应的 io::Error

Source

pub fn write_bytes(path: &Path, bytes: &[u8]) -> Result<()>

将给定的字节数组内容写入到指定路径的文件。

参数 path 是要写入的文件路径。 参数 bytes 是要写入文件的字节数组内容。

如果文件成功创建并写入内容,将返回 Ok(())。 如果在创建或写入文件时发生I/O错误,将返回对应的 io::Error

Source

pub fn create_dir_with_parents(dir_path: &str) -> Result<()>

创建目录及其所有必要的父目录

Source

pub fn delete_file(file_path: &str) -> Result<()>

删除单个文件

Source

pub fn delete_directory(dir_path: &str) -> Result<()>

删除含有子文件的目录

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.