Dart API Referencedart:ioFile

File abstract class

File objects are references to files.

To operate on the underlying file data you need to either get streams using openInputStream and openOutputStream or open the file for random access operations using open.

abstract class File {
  /**
   * Create a File object.
   */
  factory File(String name) => new _File(name);

  /**
   * Create a File object from a Path object.
   */
  factory File.fromPath(Path path) => new _File.fromPath(path);

  /**
   * Check if the file exists. Does not block and returns a
   * [:Future<bool>:].
   */
  Future<bool> exists();

  /**
   * Synchronously check if the file exists.
   */
  bool existsSync();

  /**
   * Create the file. Returns a [:Future<File>:] that completes with
   * the file when it has been created.
   *
   * Existing files are left untouched by create. Calling create on an
   * existing file might fail if there are restrictive permissions on
   * the file.
   */
  Future<File> create();

  /**
   * Synchronously create the file. Existing files are left untouched
   * by create. Calling create on an existing file might fail if there
   * are restrictive permissions on the file.
   */
  void createSync();

  /**
   * Delete the file. Returns a [:Future<File>:] that completes with
   * the file when it has been deleted.
   */
  Future<File> delete();

  /**
   * Synchronously delete the file.
   */
  void deleteSync();

  /**
   * Get a Directory object for the directory containing this
   * file. Returns a [:Future<Directory>:] that completes with the
   * directory.
   */
  Future<Directory> directory();

  /**
   * Synchronously get a Directory object for the directory containing
   * this file.
   */
  Directory directorySync();

  /**
   * Get the length of the file. Returns a [:Future<int>:] that
   * completes with the length in bytes.
   */
  Future<int> length();

  /**
   * Synchronously get the length of the file.
   */
  int lengthSync();

  /**
   * Get the last-modified time of the file. Returns a
   * [:Future<Date>:] that completes with a [Date] object for the
   * modification date.
   */
  Future<Date> lastModified();

  /**
   * Get the last-modified time of the file. Throws an exception
   * if the file does not exist.
   */
  Date lastModifiedSync();

  /**
   * Open the file for random access operations. Returns a
   * [:Future<RandomAccessFile>:] that completes with the opened
   * random access file. RandomAccessFiles must be closed using the
   * [close] method.
   *
   * Files can be opened in three modes:
   *
   * FileMode.READ: open the file for reading.
   *
   * FileMode.WRITE: open the file for both reading and writing and
   * truncate the file to length zero. If the file does not exist the
   * file is created.
   *
   * FileMode.APPEND: same as FileMode.WRITE except that the file is
   * not truncated.
   */
  Future<RandomAccessFile> open([FileMode mode = FileMode.READ]);

  /**
   * Synchronously open the file for random access operations. The
   * result is a RandomAccessFile on which random access operations
   * can be performed. Opened RandomAccessFiles must be closed using
   * the [close] method.
   *
   * See [open] for information on the [mode] argument.
   */
  RandomAccessFile openSync([FileMode mode = FileMode.READ]);

  /**
   * Get the canonical full path corresponding to the file name.
   * Returns a [:Future<String>:] that completes with the path.
   */
  Future<String> fullPath();

  /**
   * Synchronously get the canonical full path corresponding to the file name.
   */
  String fullPathSync();

  /**
   * Create a new independent input stream for the file. The file
   * input stream must be closed when no longer used to free up system
   * resources.
   */
  InputStream openInputStream();

  /**
   * Creates a new independent output stream for the file. The file
   * output stream must be closed when no longer used to free up
   * system resources.
   *
   * An output stream can be opened in two modes:
   *
   * FileMode.WRITE: create the stream and truncate the underlying
   * file to length zero.
   *
   * FileMode.APPEND: create the stream and set the position to the end of
   * the underlying file.
   */
  OutputStream openOutputStream([FileMode mode = FileMode.WRITE]);

  /**
   * Read the entire file contents as a list of bytes. Returns a
   * [:Future<List<int>>:] that completes with the list of bytes that
   * is the contents of the file.
   */
  Future<List<int>> readAsBytes();

  /**
   * Synchronously read the entire file contents as a list of bytes.
   */
  List<int> readAsBytesSync();

  /**
   * Read the entire file contents as text using the given
   * [encoding].
   *
   * Returns a [:Future<String>:] that completes with the string once
   * the file contents has been read.
   */
  Future<String> readAsText([Encoding encoding = Encoding.UTF_8]);

  /**
   * Synchronously read the entire file contents as text using the
   * given [encoding].
   */
  String readAsTextSync([Encoding encoding = Encoding.UTF_8]);

  /**
   * Read the entire file contents as lines of text using the give
   * [encoding].
   *
   * Returns a [:Future<List<String>>:] that completes with the lines
   * once the file contents has been read.
   */
  Future<List<String>> readAsLines([Encoding encoding = Encoding.UTF_8]);

  /**
   * Synchronously read the entire file contents as lines of text
   * using the given [encoding].
   */
  List<String> readAsLinesSync([Encoding encoding = Encoding.UTF_8]);

  /**
   * Get the name of the file.
   */
  String get name;
}

Constructors

factory File(String name) #

Create a File object.

factory File(String name) => new _File(name);

factory File.fromPath(Path path) #

Create a File object from a Path object.

factory File.fromPath(Path path) => new _File.fromPath(path);

Properties

final String name #

Get the name of the file.

String get name;

Methods

Future<File> create() #

Create the file. Returns a Future<File> that completes with the file when it has been created.

Existing files are left untouched by create. Calling create on an existing file might fail if there are restrictive permissions on the file.

Future<File> create();

void createSync() #

Synchronously create the file. Existing files are left untouched by create. Calling create on an existing file might fail if there are restrictive permissions on the file.

void createSync();

Future<File> delete() #

Delete the file. Returns a Future<File> that completes with the file when it has been deleted.

Future<File> delete();

void deleteSync() #

Synchronously delete the file.

void deleteSync();

Future<Directory> directory() #

Get a Directory object for the directory containing this file. Returns a Future<Directory> that completes with the directory.

Future<Directory> directory();

Directory directorySync() #

Synchronously get a Directory object for the directory containing this file.

Directory directorySync();

Future<bool> exists() #

Check if the file exists. Does not block and returns a Future<bool>.

Future<bool> exists();

bool existsSync() #

Synchronously check if the file exists.

bool existsSync();

Future<String> fullPath() #

Get the canonical full path corresponding to the file name. Returns a Future<String> that completes with the path.

Future<String> fullPath();

String fullPathSync() #

Synchronously get the canonical full path corresponding to the file name.

String fullPathSync();

Future<Date> lastModified() #

Get the last-modified time of the file. Returns a Future<Date> that completes with a Date object for the modification date.

Future<Date> lastModified();

Date lastModifiedSync() #

Get the last-modified time of the file. Throws an exception if the file does not exist.

Date lastModifiedSync();

Future<int> length() #

Get the length of the file. Returns a Future<int> that completes with the length in bytes.

Future<int> length();

int lengthSync() #

Synchronously get the length of the file.

int lengthSync();

Future<RandomAccessFile> open([FileMode mode = FileMode.READ]) #

Open the file for random access operations. Returns a Future<RandomAccessFile> that completes with the opened random access file. RandomAccessFiles must be closed using the close method.

Files can be opened in three modes:

FileMode.READ: open the file for reading.

FileMode.WRITE: open the file for both reading and writing and truncate the file to length zero. If the file does not exist the file is created.

FileMode.APPEND: same as FileMode.WRITE except that the file is not truncated.

Future<RandomAccessFile> open([FileMode mode = FileMode.READ]);

InputStream openInputStream() #

Create a new independent input stream for the file. The file input stream must be closed when no longer used to free up system resources.

InputStream openInputStream();

OutputStream openOutputStream([FileMode mode = FileMode.WRITE]) #

Creates a new independent output stream for the file. The file output stream must be closed when no longer used to free up system resources.

An output stream can be opened in two modes:

FileMode.WRITE: create the stream and truncate the underlying file to length zero.

FileMode.APPEND: create the stream and set the position to the end of the underlying file.

OutputStream openOutputStream([FileMode mode = FileMode.WRITE]);

RandomAccessFile openSync([FileMode mode = FileMode.READ]) #

Synchronously open the file for random access operations. The result is a RandomAccessFile on which random access operations can be performed. Opened RandomAccessFiles must be closed using the close method.

See open for information on the mode argument.

RandomAccessFile openSync([FileMode mode = FileMode.READ]);

Future<List<int>> readAsBytes() #

Read the entire file contents as a list of bytes. Returns a Future<List<int>> that completes with the list of bytes that is the contents of the file.

Future<List<int>> readAsBytes();

List<int> readAsBytesSync() #

Synchronously read the entire file contents as a list of bytes.

List<int> readAsBytesSync();

Future<List<String>> readAsLines([Encoding encoding = Encoding.UTF_8]) #

Read the entire file contents as lines of text using the give encoding.

Returns a Future<List<String>> that completes with the lines once the file contents has been read.

Future<List<String>> readAsLines([Encoding encoding = Encoding.UTF_8]);

List<String> readAsLinesSync([Encoding encoding = Encoding.UTF_8]) #

Synchronously read the entire file contents as lines of text using the given encoding.

List<String> readAsLinesSync([Encoding encoding = Encoding.UTF_8]);

Future<String> readAsText([Encoding encoding = Encoding.UTF_8]) #

Read the entire file contents as text using the given encoding.

Returns a Future<String> that completes with the string once the file contents has been read.

Future<String> readAsText([Encoding encoding = Encoding.UTF_8]);

String readAsTextSync([Encoding encoding = Encoding.UTF_8]) #

Synchronously read the entire file contents as text using the given encoding.

String readAsTextSync([Encoding encoding = Encoding.UTF_8]);