Struct std::fs::File 1.0.0[−][src]
pub struct File { /* fields omitted */ }
Expand description
对文件系统上打开的文件的引用。
可以通过打开 File
的选项来读取或者写入 File
的实例。文件还实现 Seek
,以更改文件内部包含的逻辑游标。
文件离开作用域时将自动关闭。Drop
的实现将忽略在关闭时检测到的错误。如果必须手动处理这些错误,请使用方法 sync_all
。
Examples
创建一个新文件并向其写入字节 (您也可以使用 write()
) :
use std::fs::File;
use std::io::prelude::*;
fn main() -> std::io::Result<()> {
let mut file = File::create("foo.txt")?;
file.write_all(b"Hello, world!")?;
Ok(())
}
Runuse std::fs::File;
use std::io::prelude::*;
fn main() -> std::io::Result<()> {
let mut file = File::open("foo.txt")?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
assert_eq!(contents, "Hello, world!");
Ok(())
}
Run使用缓冲的 Read
来读取文件的内容可能会更有效。这可以用 BufReader<R>
完成:
use std::fs::File;
use std::io::BufReader;
use std::io::prelude::*;
fn main() -> std::io::Result<()> {
let file = File::open("foo.txt")?;
let mut buf_reader = BufReader::new(file);
let mut contents = String::new();
buf_reader.read_to_string(&mut contents)?;
assert_eq!(contents, "Hello, world!");
Ok(())
}
Run请注意,虽然读写方法需要一个 &mut File
,但由于 Read
和 Write
的接口,&File
的持有者仍然可以修改文件,可以通过采用 &File
的方法,也可以通过检索底层操作系统对象并以这种方式修改文件。
另外,许多操作系统允许通过不同的进程并发修改文件。避免假定持有 &File
意味着文件不会更改。
Implementations
尝试以只读模式打开文件。
有关更多详细信息,请参见 OpenOptions::open
方法。
Errors
如果 path
还不存在,则此函数将返回错误。
根据 OpenOptions::open
,可能还会返回其他错误。
Examples
use std::fs::File;
fn main() -> std::io::Result<()> {
let mut f = File::open("foo.txt")?;
Ok(())
}
Run以只写模式打开文件。
如果该函数不存在,则此函数将创建一个文件,如果存在则将截断该文件。
有关更多详细信息,请参见 OpenOptions::open
函数。
Examples
use std::fs::File;
fn main() -> std::io::Result<()> {
let mut f = File::create("foo.txt")?;
Ok(())
}
Run返回一个新的 OpenOptions 对象。
如果不适合使用 open()
或 create()
,则此函数返回一个新的 OpenOptions 对象,可用于打开或创建具有特定选项的文件。
它等效于 OpenOptions::new()
,但允许您编写更具可读性的代码。
您可以编写 File::options().read(true).open("foo.txt")
来代替 OpenOptions::new().read(true).open("foo.txt")
。
这也避免了导入 OpenOptions
的需要。
有关更多详细信息,请参见 OpenOptions::new
函数。
Examples
use std::fs::File;
fn main() -> std::io::Result<()> {
let mut f = File::options().read(true).open("foo.txt")?;
Ok(())
}
Run该函数与 sync_all
类似,不同之处在于它可能不会将文件元数据同步到文件系统。
这适用于必须同步内容但不需要磁盘上元数据的用例。 此方法的目标是减少磁盘操作。
请注意,某些平台可能只是根据 sync_all
来实现此目的。
Examples
use std::fs::File;
use std::io::prelude::*;
fn main() -> std::io::Result<()> {
let mut f = File::create("foo.txt")?;
f.write_all(b"Hello, world!")?;
f.sync_data()?;
Ok(())
}
Run截断或扩展底层文件,将此文件的大小更新为 size
。
如果 size
小于当前文件的大小,则文件将被缩小。
如果它大于当前文件的大小,则文件将扩展到 size
,并且所有中间数据都用 0 填充。
文件的游标未更改。 特别是,如果游标位于末尾,并且使用此操作将文件缩小了,那么游标现在将超过末尾。
Errors
如果未打开文件进行写入,则此函数将返回错误。 同样,如果期望的长度由于实现细节而导致溢出,则将返回 std::io::ErrorKind::InvalidInput。
Examples
use std::fs::File;
fn main() -> std::io::Result<()> {
let mut f = File::create("foo.txt")?;
f.set_len(10)?;
Ok(())
}
Run请注意,即使使用 &self
而不是 &mut self
,此方法也会更改底层文件的内容。
创建一个新的 File
实例,该实例与现有 File
实例共享相同的底层文件句柄。
读取,写入和查找将同时影响两个 File
实例。
Examples
为名为 foo.txt
的文件创建两个句柄:
use std::fs::File;
fn main() -> std::io::Result<()> {
let mut file = File::open("foo.txt")?;
let file_copy = file.try_clone()?;
Ok(())
}
Run假设有一个名为 foo.txt
的文件,其内容为 abcdef\n
,创建两个句柄,查找其中一个,然后从另一个句柄读取剩余的字节:
use std::fs::File;
use std::io::SeekFrom;
use std::io::prelude::*;
fn main() -> std::io::Result<()> {
let mut file = File::open("foo.txt")?;
let mut file_copy = file.try_clone()?;
file.seek(SeekFrom::Start(3))?;
let mut contents = vec![];
file_copy.read_to_end(&mut contents)?;
assert_eq!(contents, b"def\n");
Ok(())
}
Run更改底层文件的权限。
特定于平台的行为
该函数当前对应于 Unix 上的 fchmod
函数和 Windows 上的 SetFileInformationByHandle
函数。
注意,这个 将来可能会改变。
Errors
如果用户缺少底层文件的权限更改属性,则此函数将返回错误。 在其他特定于操作系统的未指定情况下,它也可能返回错误。
Examples
fn main() -> std::io::Result<()> {
use std::fs::File;
let file = File::open("foo.txt")?;
let mut perms = file.metadata()?.permissions();
perms.set_readonly(true);
file.set_permissions(perms)?;
Ok(())
}
Run请注意,即使使用 &self
而不是 &mut self
,此方法也会更改底层文件的权限。
Trait Implementations
提取原始句柄,无需任何所有权。
从给定的偏移量读取填充 buf
所需的确切字节数。 Read more
从给定的偏移量读取填充 buf
所需的确切字节数。 Read more
执行转换。
将 File
转换为 Stdio
Examples
File
将在引擎盖下使用 Stdio::from
转换为 Stdio
。
use std::fs::File;
use std::process::Command;
// 使用包含 `Hello,world! ` 的 `foo.txt` 文件。
let file = File::open("foo.txt").unwrap();
let reverse = Command::new("rev")
.stdin(file) // 隐式文件转换为 Stdio
.output()
.expect("failed reverse command");
assert_eq!(reverse.stdout, b"!dlrow ,olleH");
Run执行转换。
消耗此对象,返回原始底层句柄。 Read more
与 read
相似,不同之处在于它读入缓冲区的一部分。 Read more
读取所有字节,直到此源中的 EOF 为止,然后将它们放入 buf
。 Read more
读取这个源中的所有字节,直到 EOF 为止,然后将它们追加到 buf
。 Read more
为这个 Read
实例创建一个 “by reference” 适配器。 Read more
创建一个适配器,将这个流与另一个链接起来。 Read more
与 read
相似,不同之处在于它读入缓冲区的一部分。 Read more
读取所有字节,直到此源中的 EOF 为止,然后将它们放入 buf
。 Read more
读取这个源中的所有字节,直到 EOF 为止,然后将它们追加到 buf
。 Read more
为这个 Read
实例创建一个 “by reference” 适配器。 Read more
创建一个适配器,将这个流与另一个链接起来。 Read more
将格式化的字符串写入此 writer,返回遇到的任何错误。 Read more
将格式化的字符串写入此 writer,返回遇到的任何错误。 Read more