std::fs::File - Rust (original) (raw)
Struct std::fs::File1.0.0 [−] [src]
pub struct File { /* fields omitted */ }
A reference to an open file on the filesystem.
An instance of a File
can be read and/or written depending on what options it was opened with. Files also implement Seek to alter the logical cursor that the file contains internally.
Files are automatically closed when they go out of scope.
Create a new file and write bytes to it:
use std::fs::File; use std::io::prelude::*;
let mut file = File::create("foo.txt")?; file.write_all(b"Hello, world!")?;Run
Read the contents of a file into a String:
use std::fs::File; use std::io::prelude::*;
let mut file = File::open("foo.txt")?; let mut contents = String::new(); file.read_to_string(&mut contents)?; assert_eq!(contents, "Hello, world!");Run
It can be more efficient to read the contents of a file with a bufferedReader. This can be accomplished with BufReader:
use std::fs::File; use std::io::BufReader; use std::io::prelude::*;
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!");Run
impl [File](../../std/fs/struct.File.html "struct std::fs::File")
[src]
pub fn [open](#method.open)<P: [AsRef](../../std/convert/trait.AsRef.html "trait std::convert::AsRef")<[Path](../../std/path/struct.Path.html "struct std::path::Path")>>(path: P) -> [Result](../../std/io/type.Result.html "type std::io::Result")<[File](../../std/fs/struct.File.html "struct std::fs::File")>
[src]
Attempts to open a file in read-only mode.
See the OpenOptions::open method for more details.
This function will return an error if path
does not already exist. Other errors may also be returned according to OpenOptions::open.
use std::fs::File;
let mut f = File::open("foo.txt")?;Run
pub fn [create](#method.create)<P: [AsRef](../../std/convert/trait.AsRef.html "trait std::convert::AsRef")<[Path](../../std/path/struct.Path.html "struct std::path::Path")>>(path: P) -> [Result](../../std/io/type.Result.html "type std::io::Result")<[File](../../std/fs/struct.File.html "struct std::fs::File")>
[src]
Opens a file in write-only mode.
This function will create a file if it does not exist, and will truncate it if it does.
See the OpenOptions::open function for more details.
use std::fs::File;
let mut f = File::create("foo.txt")?;Run
pub fn [sync_all](#method.sync%5Fall)(&self) -> [Result](../../std/io/type.Result.html "type std::io::Result")<[()](../primitive.unit.html)>
[src]
Attempts to sync all OS-internal metadata to disk.
This function will attempt to ensure that all in-core data reaches the filesystem before returning.
use std::fs::File; use std::io::prelude::*;
let mut f = File::create("foo.txt")?; f.write_all(b"Hello, world!")?;
f.sync_all()?;Run
pub fn [sync_data](#method.sync%5Fdata)(&self) -> [Result](../../std/io/type.Result.html "type std::io::Result")<[()](../primitive.unit.html)>
[src]
This function is similar to sync_all, except that it may not synchronize file metadata to the filesystem.
This is intended for use cases that must synchronize content, but don't need the metadata on disk. The goal of this method is to reduce disk operations.
Note that some platforms may simply implement this in terms ofsync_all.
use std::fs::File; use std::io::prelude::*;
let mut f = File::create("foo.txt")?; f.write_all(b"Hello, world!")?;
f.sync_data()?;Run
pub fn [set_len](#method.set%5Flen)(&self, size: [u64](../primitive.u64.html)) -> [Result](../../std/io/type.Result.html "type std::io::Result")<[()](../primitive.unit.html)>
[src]
Truncates or extends the underlying file, updating the size of this file to become size
.
If the size
is less than the current file's size, then the file will be shrunk. If it is greater than the current file's size, then the file will be extended to size
and have all of the intermediate data filled in with 0s.
This function will return an error if the file is not opened for writing.
use std::fs::File;
let mut f = File::create("foo.txt")?; f.set_len(10)?;Run
pub fn [metadata](#method.metadata)(&self) -> [Result](../../std/io/type.Result.html "type std::io::Result")<[Metadata](../../std/fs/struct.Metadata.html "struct std::fs::Metadata")>
[src]
Queries metadata about the underlying file.
use std::fs::File;
let mut f = File::open("foo.txt")?; let metadata = f.metadata()?;Run
pub fn [try_clone](#method.try%5Fclone)(&self) -> [Result](../../std/io/type.Result.html "type std::io::Result")<[File](../../std/fs/struct.File.html "struct std::fs::File")>
1.9.0
Create a new File
instance that shares the same underlying file handle as the existing File
instance. Reads, writes, and seeks will affect both File
instances simultaneously.
Create two handles for a file named foo.txt
:
use std::fs::File;
let mut file = File::open("foo.txt")?; let file_copy = file.try_clone()?;Run
Assuming there’s a file named foo.txt
with contents abcdef\n
, create two handles, seek one of them, and read the remaining bytes from the other handle:
use std::fs::File; use std::io::SeekFrom; use std::io::prelude::*;
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");Run
pub fn [set_permissions](#method.set%5Fpermissions)(&self, perm: [Permissions](../../std/fs/struct.Permissions.html "struct std::fs::Permissions")) -> [Result](../../std/io/type.Result.html "type std::io::Result")<[()](../primitive.unit.html)>
1.16.0
Changes the permissions on the underlying file.
This function currently corresponds to the fchmod
function on Unix and the SetFileInformationByHandle
function on Windows. Note that, thismay change in the future.
This function will return an error if the user lacks permission change attributes on the underlying file. It may also return an error in other os-specific unspecified cases.
use std::fs::File;
let file = File::open("foo.txt")?; let mut perms = file.metadata()?.permissions(); perms.set_readonly(true); file.set_permissions(perms)?;Run
impl [Debug](../../std/fmt/trait.Debug.html "trait std::fmt::Debug") for [File](../../std/fs/struct.File.html "struct std::fs::File")
[src]
impl [Read](../../std/io/trait.Read.html "trait std::io::Read") for [File](../../std/fs/struct.File.html "struct std::fs::File")
[src]
fn [read](../../std/io/trait.Read.html#tymethod.read)(&mut self, buf: [&mut [](../primitive.slice.html)[u8](../primitive.u8.html)[]](../primitive.slice.html)) -> [Result](../../std/io/type.Result.html "type std::io::Result")<[usize](../primitive.usize.html)>
[src]
Pull some bytes from this source into the specified buffer, returning how many bytes were read. Read more
unsafe fn [initializer](../../std/io/trait.Read.html#method.initializer)(&self) -> [Initializer](../../std/io/struct.Initializer.html "struct std::io::Initializer")
[src]
🔬 This is a nightly-only experimental API. (read_initializer
#42788)
Determines if this Read
er can work with buffers of uninitialized memory. Read more
fn [read_to_end](../../std/io/trait.Read.html#method.read%5Fto%5Fend)(&mut self, buf: &mut [Vec](../../std/vec/struct.Vec.html "struct std::vec::Vec")<[u8](../primitive.u8.html)>) -> [Result](../../std/io/type.Result.html "type std::io::Result")<[usize](../primitive.usize.html)>
[src]
Read all bytes until EOF in this source, placing them into buf
. Read more
fn [read_to_string](../../std/io/trait.Read.html#method.read%5Fto%5Fstring)(&mut self, buf: &mut [String](../../std/string/struct.String.html "struct std:🧵:String")) -> [Result](../../std/io/type.Result.html "type std::io::Result")<[usize](../primitive.usize.html)>
[src]
Read all bytes until EOF in this source, appending them to buf
. Read more
fn [read_exact](../../std/io/trait.Read.html#method.read%5Fexact)(&mut self, buf: [&mut [](../primitive.slice.html)[u8](../primitive.u8.html)[]](../primitive.slice.html)) -> [Result](../../std/io/type.Result.html "type std::io::Result")<[()](../primitive.unit.html)>
1.6.0
Read the exact number of bytes required to fill buf
. Read more
`fn by_ref(&mut self) -> &mut Self where
Creates a "by reference" adaptor for this instance of Read
. Read more
ⓘImportant traits for Bytes
fn [bytes](../../std/io/trait.Read.html#method.bytes)(self) -> [Bytes](../../std/io/struct.Bytes.html "struct std::io::Bytes")<Self> where Self: [Sized](../../std/marker/trait.Sized.html "trait std:📑:Sized"),
[src]
Transforms this Read
instance to an [Iterator
] over its bytes. Read more
ⓘImportant traits for Chars
fn [chars](../../std/io/trait.Read.html#method.chars)(self) -> [Chars](../../std/io/struct.Chars.html "struct std::io::Chars")<Self> where Self: [Sized](../../std/marker/trait.Sized.html "trait std:📑:Sized"),
[src]
🔬 This is a nightly-only experimental API. (io
#27802)
the semantics of a partial read/write of where errors happen is currently unclear and may change
Transforms this Read
instance to an [Iterator
] over [char
]s. Read more
ⓘImportant traits for Chain<T, U>
fn [chain](../../std/io/trait.Read.html#method.chain)<R: [Read](../../std/io/trait.Read.html "trait std::io::Read")>(self, next: R) -> [Chain](../../std/io/struct.Chain.html "struct std::io::Chain")<Self, R> where Self: [Sized](../../std/marker/trait.Sized.html "trait std:📑:Sized"),
[src]
Creates an adaptor which will chain this stream with another. Read more
ⓘImportant traits for Take
fn [take](../../std/io/trait.Read.html#method.take)(self, limit: [u64](../primitive.u64.html)) -> [Take](../../std/io/struct.Take.html "struct std::io::Take")<Self> where Self: [Sized](../../std/marker/trait.Sized.html "trait std:📑:Sized"),
[src]
Creates an adaptor which will read at most limit
bytes from it. Read more
impl [Write](../../std/io/trait.Write.html "trait std::io::Write") for [File](../../std/fs/struct.File.html "struct std::fs::File")
[src]
impl [Seek](../../std/io/trait.Seek.html "trait std::io::Seek") for [File](../../std/fs/struct.File.html "struct std::fs::File")
[src]
impl<'a> [Read](../../std/io/trait.Read.html "trait std::io::Read") for &'a [File](../../std/fs/struct.File.html "struct std::fs::File")
[src]
fn [read](../../std/io/trait.Read.html#tymethod.read)(&mut self, buf: [&mut [](../primitive.slice.html)[u8](../primitive.u8.html)[]](../primitive.slice.html)) -> [Result](../../std/io/type.Result.html "type std::io::Result")<[usize](../primitive.usize.html)>
[src]
Pull some bytes from this source into the specified buffer, returning how many bytes were read. Read more
unsafe fn [initializer](../../std/io/trait.Read.html#method.initializer)(&self) -> [Initializer](../../std/io/struct.Initializer.html "struct std::io::Initializer")
[src]
🔬 This is a nightly-only experimental API. (read_initializer
#42788)
Determines if this Read
er can work with buffers of uninitialized memory. Read more
fn [read_to_end](../../std/io/trait.Read.html#method.read%5Fto%5Fend)(&mut self, buf: &mut [Vec](../../std/vec/struct.Vec.html "struct std::vec::Vec")<[u8](../primitive.u8.html)>) -> [Result](../../std/io/type.Result.html "type std::io::Result")<[usize](../primitive.usize.html)>
[src]
Read all bytes until EOF in this source, placing them into buf
. Read more
fn [read_to_string](../../std/io/trait.Read.html#method.read%5Fto%5Fstring)(&mut self, buf: &mut [String](../../std/string/struct.String.html "struct std:🧵:String")) -> [Result](../../std/io/type.Result.html "type std::io::Result")<[usize](../primitive.usize.html)>
[src]
Read all bytes until EOF in this source, appending them to buf
. Read more
fn [read_exact](../../std/io/trait.Read.html#method.read%5Fexact)(&mut self, buf: [&mut [](../primitive.slice.html)[u8](../primitive.u8.html)[]](../primitive.slice.html)) -> [Result](../../std/io/type.Result.html "type std::io::Result")<[()](../primitive.unit.html)>
1.6.0
Read the exact number of bytes required to fill buf
. Read more
`fn by_ref(&mut self) -> &mut Self where
Creates a "by reference" adaptor for this instance of Read
. Read more
ⓘImportant traits for Bytes
fn [bytes](../../std/io/trait.Read.html#method.bytes)(self) -> [Bytes](../../std/io/struct.Bytes.html "struct std::io::Bytes")<Self> where Self: [Sized](../../std/marker/trait.Sized.html "trait std:📑:Sized"),
[src]
Transforms this Read
instance to an [Iterator
] over its bytes. Read more
ⓘImportant traits for Chars
fn [chars](../../std/io/trait.Read.html#method.chars)(self) -> [Chars](../../std/io/struct.Chars.html "struct std::io::Chars")<Self> where Self: [Sized](../../std/marker/trait.Sized.html "trait std:📑:Sized"),
[src]
🔬 This is a nightly-only experimental API. (io
#27802)
the semantics of a partial read/write of where errors happen is currently unclear and may change
Transforms this Read
instance to an [Iterator
] over [char
]s. Read more
ⓘImportant traits for Chain<T, U>
fn [chain](../../std/io/trait.Read.html#method.chain)<R: [Read](../../std/io/trait.Read.html "trait std::io::Read")>(self, next: R) -> [Chain](../../std/io/struct.Chain.html "struct std::io::Chain")<Self, R> where Self: [Sized](../../std/marker/trait.Sized.html "trait std:📑:Sized"),
[src]
Creates an adaptor which will chain this stream with another. Read more
ⓘImportant traits for Take
fn [take](../../std/io/trait.Read.html#method.take)(self, limit: [u64](../primitive.u64.html)) -> [Take](../../std/io/struct.Take.html "struct std::io::Take")<Self> where Self: [Sized](../../std/marker/trait.Sized.html "trait std:📑:Sized"),
[src]
Creates an adaptor which will read at most limit
bytes from it. Read more
impl<'a> [Write](../../std/io/trait.Write.html "trait std::io::Write") for &'a [File](../../std/fs/struct.File.html "struct std::fs::File")
[src]
impl<'a> [Seek](../../std/io/trait.Seek.html "trait std::io::Seek") for &'a [File](../../std/fs/struct.File.html "struct std::fs::File")
[src]
impl [From](../../std/convert/trait.From.html "trait std::convert::From")<[File](../../std/fs/struct.File.html "struct std::fs::File")> for [Stdio](../../std/process/struct.Stdio.html "struct std::process::Stdio")
1.20.0
impl [AsRawFd](../../std/os/unix/io/trait.AsRawFd.html "trait std::os::unix::io::AsRawFd") for [File](../../std/fs/struct.File.html "struct std::fs::File")
[src]
impl [FromRawFd](../../std/os/unix/io/trait.FromRawFd.html "trait std::os::unix::io::FromRawFd") for [File](../../std/fs/struct.File.html "struct std::fs::File")
1.1.0
impl [IntoRawFd](../../std/os/unix/io/trait.IntoRawFd.html "trait std::os::unix::io::IntoRawFd") for [File](../../std/fs/struct.File.html "struct std::fs::File")
1.4.0
impl [FileExt](../../std/os/unix/fs/trait.FileExt.html "trait std::os::unix::fs::FileExt") for [File](../../std/fs/struct.File.html "struct std::fs::File")
1.15.0
impl [FileExt](../../std/os/windows/fs/trait.FileExt.html "trait std::os::windows::fs::FileExt") for [File](../../std/fs/struct.File.html "struct std::fs::File")
1.15.0
impl [AsRawHandle](../../std/os/windows/io/trait.AsRawHandle.html "trait std::os::windows::io::AsRawHandle") for [File](../../std/fs/struct.File.html "struct std::fs::File")
[src]
impl [FromRawHandle](../../std/os/windows/io/trait.FromRawHandle.html "trait std::os::windows::io::FromRawHandle") for [File](../../std/fs/struct.File.html "struct std::fs::File")
1.1.0
impl [IntoRawHandle](../../std/os/windows/io/trait.IntoRawHandle.html "trait std::os::windows::io::IntoRawHandle") for [File](../../std/fs/struct.File.html "struct std::fs::File")
1.4.0