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_string_by_iter(file_path: &str) -> Result<impl Iterator, 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<()>

删除含有子文件的目录

Source

pub fn glob(pattern: &str) -> Result<Vec<String>>

使用glob模式匹配文件路径

该函数接收一个glob模式字符串,搜索匹配的文件路径,并返回路径字符串的向量。

§参数
  • pattern - glob模式字符串,用于匹配文件路径
§返回值

返回一个Result类型,成功时包含匹配到的文件路径字符串向量,失败时包含IO错误

§示例
let files = FileUtil::glob("D:/*/sub_dir").unwrap();
Source

pub fn recursive_files(path: &str) -> Result<Vec<String>, Box<dyn Error>>

递归遍历指定路径下的所有文件(不会把目录作为结果返回)

该函数会递归遍历给定路径下的所有文件和子目录中的文件, 并将所有文件路径收集到一个字符串向量中返回。

§参数
  • path - 需要遍历的根目录路径字符串
§返回值
  • Ok(Vec<String>) - 包含所有文件路径的字符串向量,路径分隔符统一为正斜杠
  • Err(Box<dyn std::error::Error>) - 遍历过程中发生的错误
§错误

当路径不存在或无法访问时,会返回相应的错误信息

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,