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.

Examples

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
[src]

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.

Examples

use std::fs::File;

let mut f = try!(File::open("foo.txt"));

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.

Examples

use std::fs::File;

let mut f = try!(File::create("foo.txt"));

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.

Examples

use std::fs::File;
use std::io::prelude::*;

let mut f = try!(File::create("foo.txt"));
try!(f.write_all(b"Hello, world!"));

try!(f.sync_all());

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.

Examples

use std::fs::File;
use std::io::prelude::*;

let mut f = try!(File::create("foo.txt"));
try!(f.write_all(b"Hello, world!"));

try!(f.sync_data());

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.

Errors

This function will return an error if the file is not opened for writing.

Examples

use std::fs::File;

let mut f = try!(File::create("foo.txt"));
try!(f.set_len(10));

Trait Implementations

impl Read for File
[src]

impl Write for File
[src]

impl<'a> Read for &'a File
[src]

impl<'a> Write for &'a File
[src]

impl FileExt for File
[src]

🔬 This is a nightly-only experimental API.   (file_offset)

🔬 This is a nightly-only experimental API.   (file_offset)