pub struct File { /* private fields */ }
Expand description
Represents a file in the file system.
Implementations§
Source§impl File
impl File
Sourcepub fn open<P: AsRef<Path>>(path: P) -> Result<Self>
pub fn open<P: AsRef<Path>>(path: P) -> Result<Self>
Attempts to open a file in read-only mode.
See the OpenOptions::open
method for more details.
If you only need to read the entire file contents, consider
fs::read()
or fs::read_to_string()
instead.
§Errors
This function will return an error if path
does not already exist.
Other errors may also be returned according to OpenOptions::open
.
Sourcepub fn create<P: AsRef<Path>>(path: P) -> Result<Self>
pub fn create<P: AsRef<Path>>(path: P) -> Result<Self>
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.
Depending on the platform, this function may fail if the
full directory path does not exist.
See the OpenOptions::open
function for more details.
See also fs::write()
for a simple function to
create a file with some given data.
§Errors
See OpenOptions::open
.
Sourcepub fn create_new<P: AsRef<Path>>(path: P) -> Result<File>
pub fn create_new<P: AsRef<Path>>(path: P) -> Result<File>
Creates a new file in read-write mode; error if the file exists.
This function will create a file if it does not exist, or return an error if it does. This
way, if the call succeeds, the file returned is guaranteed to be new.
If a file exists at the target location, creating a new file will fail with AlreadyExists
or another error based on the situation. See OpenOptions::open
for a
non-exhaustive list of likely errors.
This option is useful because it is atomic. Otherwise between checking whether a file exists and creating a new one, the file may have been created by another process (a TOCTOU race condition / attack).
This can also be written using
File::options().read(true).write(true).create_new(true).open(...)
.
§Errors
See OpenOptions::open
.
Sourcepub const fn options() -> OpenOptions
pub const fn options() -> OpenOptions
Returns a new OpenOptions object.
This function returns a new OpenOptions object that you can use to
open or create a file with specific options if open()
or create()
are not appropriate.
It is equivalent to OpenOptions::new()
, but allows you to write more
readable code. Instead of
OpenOptions::new().append(true).open("example.log")
,
you can write File::options().append(true).open("example.log")
. This
also avoids the need to import OpenOptions
.
See the OpenOptions::new
function for more details.
Sourcepub fn metadata(&self) -> Result<Metadata>
pub fn metadata(&self) -> Result<Metadata>
Queries metadata about the underlying file.
§Errors
InvalidData
: Internal filesystem error occurred.
Sourcepub fn sync_all(&self) -> Result<()>
pub fn sync_all(&self) -> Result<()>
Attempts to sync all OS-internal file content and metadata to disk.
This function will attempt to ensure that all in-memory data reaches the filesystem before returning.
This can be used to handle errors that would otherwise only be caught
when the File
is closed, as dropping a File
will ignore all errors.
Note, however, that sync_all
is generally more expensive than closing
a file by dropping it, because the latter is not required to block until
the data has been written to the filesystem.
If synchronizing the metadata is not required, use sync_data
instead.
§Errors
This function is infallible.
Sourcepub fn sync_data(&self) -> Result<()>
pub fn sync_data(&self) -> Result<()>
This function is similar to sync_all
, except that it might 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 of
sync_all
.
§Errors
This function is infallible.
Trait Implementations§
Source§impl Read for File
impl Read for File
Source§fn read(&mut self, buf: &mut [u8]) -> Result<usize>
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
Source§fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
buf
. Read moreSource§unsafe fn initializer(&self) -> Initializer
unsafe fn initializer(&self) -> Initializer
Read
er can work with buffers of uninitialized
memory. Read moreSource§fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
buf
. Read moreSource§fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
Read
. Read more