Struct std::io::fs::File
pub struct File { // some fields omitted }
Unconstrained file access type that exposes read and write operations
Can be constructed via File::open()
, File::create()
, and
File::open_mode()
.
Error
This type will return errors as an IoResult<T>
if operations are
attempted against it for which its underlying file descriptor was not
configured at creation time, via the FileAccess
parameter to
File::open_mode()
.
Methods
impl File
fn open_mode(path: &Path, mode: FileMode, access: FileAccess) -> IoResult<File>
Open a file at path
in the mode specified by the mode
and access
arguments
Example
use std::io::{File, Open, ReadWrite}; let p = Path::new("/some/file/path.txt"); let file = match File::open_mode(&p, Open, ReadWrite) { Ok(f) => f, Err(e) => fail!("file error: {}", e), }; // do some stuff with that file // the file will be closed at the end of this block
FileMode
and FileAccess
provide information about the permissions
context in which a given stream is created. More information about them
can be found in std::io
's docs. If a file is opened with Write
or ReadWrite
access, then it will be created it does not already
exist.
Note that, with this function, a File
is returned regardless of the
access-limitations indicated by FileAccess
(e.g. calling write
on a
File
opened as Read
will return an error at runtime).
Error
This function will return an error under a number of different circumstances, to include but not limited to:
- Opening a file that does not exist with
Read
access. - Attempting to open a file with a
FileAccess
that the user lacks permissions for - Filesystem-level errors (full disk, etc)
fn open(path: &Path) -> IoResult<File>
Attempts to open a file in read-only mode. This function is equivalent to
File::open_mode(path, Open, Read)
, and will raise all of the same
errors that File::open_mode
does.
For more information, see the File::open_mode
function.
Example
use std::io::File; let contents = File::open(&Path::new("foo.txt")).read_to_end();
fn create(path: &Path) -> IoResult<File>
Attempts to create a file in write-only mode. This function is
equivalent to File::open_mode(path, Truncate, Write)
, and will
raise all of the same errors that File::open_mode
does.
For more information, see the File::open_mode
function.
Example
use std::io::File; let mut f = File::create(&Path::new("foo.txt")); f.write(bytes!("This is a sample file"));
fn path<'a>(&'a self) -> &'a Path
Returns the original path which was used to open this file.
fn fsync(&mut self) -> IoResult<()>
Synchronizes all modifications to this file to its permanent storage device. This will flush any internal buffers necessary to perform this operation.
fn datasync(&mut self) -> IoResult<()>
This function is similar to fsync
, except that it may not synchronize
file metadata to the filesystem. This is intended for use case which
must synchronize content, but don't need the metadata on disk. The goal
of this method is to reduce disk operations.
fn truncate(&mut self, size: i64) -> IoResult<()>
Either truncates or extends the underlying file, updating the size of
this file to become size
. This is equivalent to unix's truncate
function.
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 eof(&self) -> bool
Tests whether this stream has reached EOF.
If true, then this file will no longer continue to return data via
read
.
Trait Implementations
impl Reader for File
fn read(&mut self, buf: &mut [u8]) -> IoResult<uint>
Read bytes, up to the length of buf
and place them in buf
.
Returns the number of bytes read. The number of bytes read my
be less than the number requested, even 0. Returns Err
on EOF.
Error
If an error occurs during this I/O operation, then it is returned as
Err(IoError)
. Note that end-of-file is considered an error, and can be
inspected for in the error's kind
field. Also note that reading 0
bytes is not considered an error in all circumstances
fn read_byte(&mut self) -> IoResult<u8>
Reads a single byte. Returns Err
on EOF.
fn fill(&mut self, buf: &mut [u8]) -> IoResult<()>
Fills the provided slice with bytes from this reader
This will continue to call read
until the slice has been completely
filled with bytes.
Error
If an error occurs at any point, that error is returned, and no further bytes are read.
fn push_exact(&mut self, buf: &mut ~[u8], len: uint) -> IoResult<()>
Reads exactly len
bytes and appends them to a vector.
May push fewer than the requested number of bytes on error
or EOF. If Ok(())
is returned, then all of the requested bytes were
pushed on to the vector, otherwise the amount len
bytes couldn't be
read (an error was encountered), and the error is returned.
fn read_exact(&mut self, len: uint) -> IoResult<~[u8]>
Reads exactly len
bytes and gives you back a new vector of length
len
Error
Fails with the same conditions as read
. Additionally returns error
on EOF. Note that if an error is returned, then some number of bytes may
have already been consumed from the underlying reader, and they are lost
(not returned as part of the error). If this is unacceptable, then it is
recommended to use the push_exact
or read
methods.
fn read_to_end(&mut self) -> IoResult<~[u8]>
Reads all remaining bytes from the stream.
Error
Returns any non-EOF error immediately. Previously read bytes are discarded when an error is returned.
When EOF is encountered, all bytes read up to that point are returned.
fn read_to_str(&mut self) -> IoResult<~str>
Reads all of the remaining bytes of this stream, interpreting them as a UTF-8 encoded stream. The corresponding string is returned.
Error
This function returns all of the same errors as read_to_end
with an
additional error if the reader's contents are not a valid sequence of
UTF-8 bytes.
fn bytes<'r>(&'r mut self) -> Bytes<'r, Self>
Create an iterator that reads a single byte on each iteration, until EOF.
Error
Any error other than EndOfFile
that is produced by the underlying Reader
is returned by the iterator and should be handled by the caller.
fn read_le_uint_n(&mut self, nbytes: uint) -> IoResult<u64>
Reads n
little-endian unsigned integer bytes.
n
must be between 1 and 8, inclusive.
fn read_le_int_n(&mut self, nbytes: uint) -> IoResult<i64>
Reads n
little-endian signed integer bytes.
n
must be between 1 and 8, inclusive.
fn read_be_uint_n(&mut self, nbytes: uint) -> IoResult<u64>
Reads n
big-endian unsigned integer bytes.
n
must be between 1 and 8, inclusive.
fn read_be_int_n(&mut self, nbytes: uint) -> IoResult<i64>
Reads n
big-endian signed integer bytes.
n
must be between 1 and 8, inclusive.
fn read_le_uint(&mut self) -> IoResult<uint>
Reads a little-endian unsigned integer.
The number of bytes returned is system-dependant.
fn read_le_int(&mut self) -> IoResult<int>
Reads a little-endian integer.
The number of bytes returned is system-dependant.
fn read_be_uint(&mut self) -> IoResult<uint>
Reads a big-endian unsigned integer.
The number of bytes returned is system-dependant.
fn read_be_int(&mut self) -> IoResult<int>
Reads a big-endian integer.
The number of bytes returned is system-dependant.
fn read_be_u64(&mut self) -> IoResult<u64>
Reads a big-endian u64
.
u64
s are 8 bytes long.
fn read_be_u32(&mut self) -> IoResult<u32>
Reads a big-endian u32
.
u32
s are 4 bytes long.
fn read_be_u16(&mut self) -> IoResult<u16>
Reads a big-endian u16
.
u16
s are 2 bytes long.
fn read_be_i64(&mut self) -> IoResult<i64>
Reads a big-endian i64
.
i64
s are 8 bytes long.
fn read_be_i32(&mut self) -> IoResult<i32>
Reads a big-endian i32
.
i32
s are 4 bytes long.
fn read_be_i16(&mut self) -> IoResult<i16>
Reads a big-endian i16
.
i16
s are 2 bytes long.
fn read_be_f64(&mut self) -> IoResult<f64>
Reads a big-endian f64
.
f64
s are 8 byte, IEEE754 double-precision floating point numbers.
fn read_be_f32(&mut self) -> IoResult<f32>
Reads a big-endian f32
.
f32
s are 4 byte, IEEE754 single-precision floating point numbers.
fn read_le_u64(&mut self) -> IoResult<u64>
Reads a little-endian u64
.
u64
s are 8 bytes long.
fn read_le_u32(&mut self) -> IoResult<u32>
Reads a little-endian u32
.
u32
s are 4 bytes long.
fn read_le_u16(&mut self) -> IoResult<u16>
Reads a little-endian u16
.
u16
s are 2 bytes long.
fn read_le_i64(&mut self) -> IoResult<i64>
Reads a little-endian i64
.
i64
s are 8 bytes long.
fn read_le_i32(&mut self) -> IoResult<i32>
Reads a little-endian i32
.
i32
s are 4 bytes long.
fn read_le_i16(&mut self) -> IoResult<i16>
Reads a little-endian i16
.
i16
s are 2 bytes long.
fn read_le_f64(&mut self) -> IoResult<f64>
Reads a little-endian f64
.
f64
s are 8 byte, IEEE754 double-precision floating point numbers.
fn read_le_f32(&mut self) -> IoResult<f32>
Reads a little-endian f32
.
f32
s are 4 byte, IEEE754 single-precision floating point numbers.
fn read_u8(&mut self) -> IoResult<u8>
Read a u8.
u8
s are 1 byte.
fn read_i8(&mut self) -> IoResult<i8>
Read an i8.
i8
s are 1 byte.
fn by_ref<'a>(&'a mut self) -> RefReader<'a, Self>
Creates a wrapper around a mutable reference to the reader.
This is useful to allow applying adaptors while still retaining ownership of the original value.
impl Writer for File
fn write(&mut self, buf: &[u8]) -> IoResult<()>
Write the entirety of a given buffer
Errors
If an error happens during the I/O operation, the error is returned as
Err
. Note that it is considered an error if the entire buffer could
not be written, and if an error is returned then it is unknown how much
data (if any) was actually written.
fn flush(&mut self) -> IoResult<()>
Flush this output stream, ensuring that all intermediately buffered contents reach their destination.
This is by default a no-op and implementers of the Writer
trait should
decide whether their stream needs to be buffered or not.
fn write_str(&mut self, s: &str) -> IoResult<()>
Write a rust string into this sink.
The bytes written will be the UTF-8 encoded version of the input string.
If other encodings are desired, it is recommended to compose this stream
with another performing the conversion, or to use write
with a
converted byte-array instead.
fn write_line(&mut self, s: &str) -> IoResult<()>
Writes a string into this sink, and then writes a literal newline (\n
)
byte afterwards. Note that the writing of the newline is not atomic in
the sense that the call to write
is invoked twice (once with the
string and once with a newline character).
If other encodings or line ending flavors are desired, it is recommended
that the write
method is used specifically instead.
fn write_char(&mut self, c: char) -> IoResult<()>
Write a single char, encoded as UTF-8.
fn write_int(&mut self, n: int) -> IoResult<()>
Write the result of passing n through int::to_str_bytes
.
fn write_uint(&mut self, n: uint) -> IoResult<()>
Write the result of passing n through uint::to_str_bytes
.
fn write_le_uint(&mut self, n: uint) -> IoResult<()>
Write a little-endian uint (number of bytes depends on system).
fn write_le_int(&mut self, n: int) -> IoResult<()>
Write a little-endian int (number of bytes depends on system).
fn write_be_uint(&mut self, n: uint) -> IoResult<()>
Write a big-endian uint (number of bytes depends on system).
fn write_be_int(&mut self, n: int) -> IoResult<()>
Write a big-endian int (number of bytes depends on system).
fn write_be_u64(&mut self, n: u64) -> IoResult<()>
Write a big-endian u64 (8 bytes).
fn write_be_u32(&mut self, n: u32) -> IoResult<()>
Write a big-endian u32 (4 bytes).
fn write_be_u16(&mut self, n: u16) -> IoResult<()>
Write a big-endian u16 (2 bytes).
fn write_be_i64(&mut self, n: i64) -> IoResult<()>
Write a big-endian i64 (8 bytes).
fn write_be_i32(&mut self, n: i32) -> IoResult<()>
Write a big-endian i32 (4 bytes).
fn write_be_i16(&mut self, n: i16) -> IoResult<()>
Write a big-endian i16 (2 bytes).
fn write_be_f64(&mut self, f: f64) -> IoResult<()>
Write a big-endian IEEE754 double-precision floating-point (8 bytes).
fn write_be_f32(&mut self, f: f32) -> IoResult<()>
Write a big-endian IEEE754 single-precision floating-point (4 bytes).
fn write_le_u64(&mut self, n: u64) -> IoResult<()>
Write a little-endian u64 (8 bytes).
fn write_le_u32(&mut self, n: u32) -> IoResult<()>
Write a little-endian u32 (4 bytes).
fn write_le_u16(&mut self, n: u16) -> IoResult<()>
Write a little-endian u16 (2 bytes).
fn write_le_i64(&mut self, n: i64) -> IoResult<()>
Write a little-endian i64 (8 bytes).
fn write_le_i32(&mut self, n: i32) -> IoResult<()>
Write a little-endian i32 (4 bytes).
fn write_le_i16(&mut self, n: i16) -> IoResult<()>
Write a little-endian i16 (2 bytes).
fn write_le_f64(&mut self, f: f64) -> IoResult<()>
Write a little-endian IEEE754 double-precision floating-point (8 bytes).
fn write_le_f32(&mut self, f: f32) -> IoResult<()>
Write a little-endian IEEE754 single-precision floating-point (4 bytes).
fn write_u8(&mut self, n: u8) -> IoResult<()>
Write a u8 (1 byte).
fn write_i8(&mut self, n: i8) -> IoResult<()>
Write a i8 (1 byte).
fn by_ref<'a>(&'a mut self) -> RefWriter<'a, Self>
Creates a wrapper around a mutable reference to the writer.
This is useful to allow applying wrappers while still retaining ownership of the original value.
impl Seek for File
fn tell(&self) -> IoResult<u64>
Return position of file cursor in the stream
fn seek(&mut self, pos: i64, style: SeekStyle) -> IoResult<()>
Seek to an offset in a stream
A successful seek clears the EOF indicator. Seeking beyond EOF is allowed, but seeking before position 0 is not allowed.
Errors
- Seeking to a negative offset is considered an error
- Seeking past the end of the stream does not modify the underlying stream, but the next write may cause the previous data to be filled in with a bit pattern.