Struct std::fs::FileUnstable
[-] [+]
[src]
pub struct File { // some 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.
Example
fn main() { use std::io::prelude::*; use std::fs::File; fn foo() -> std::io::Result<()> { let mut f = try!(File::create("foo.txt")); try!(f.write_all(b"Hello, world!")); let mut f = try!(File::open("foo.txt")); let mut s = String::new(); try!(f.read_to_string(&mut s)); assert_eq!(s, "Hello, world!"); Ok(()) } }use std::io::prelude::*; use std::fs::File; let mut f = try!(File::create("foo.txt")); try!(f.write_all(b"Hello, world!")); let mut f = try!(File::open("foo.txt")); let mut s = String::new(); try!(f.read_to_string(&mut s)); assert_eq!(s, "Hello, world!");
Methods
impl File
fn open<P: AsPath + ?Sized>(path: &P) -> Result<File>
Attempts to open a file in read-only mode.
See the OpenOptions::open
method for more details.
Errors
This function will return an error if path
does not already exist.
Other errors may also be returned according to OpenOptions::open
.
fn create<P: AsPath + ?Sized>(path: &P) -> Result<File>
Open 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.
fn path(&self) -> Option<&Path>
Returns the original path that was used to open this file.
fn sync_all(&self) -> Result<()>
Attempt to sync all OS-internal metadata to disk.
This function will attempt to ensure that all in-core data reaches the filesystem before returning.
fn sync_data(&self) -> Result<()>
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 of
sync_all
.
fn set_len(&self, size: u64) -> Result<()>
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.
fn metadata(&self) -> Result<Metadata>
Queries information about the underlying file.