The module
On operating systems with thread support, it is possible to let
file operations be performed in threads of their own, allowing
other Erlang processes to continue executing in parallel with
the file operations. See the command line flag
The Erlang VM supports file names in Unicode to a limited
extent. Depending on how the VM is started (with the parameter
The default behavior for Unicode character translation depends on to what extent the underlying OS/filesystem enforces consistent naming. On OSes where all file names are ensured to be in one or another encoding, Unicode is the default (currently this holds for Windows and MacOSX). On OSes with completely transparent file naming (i.e. all Unixes except MacOSX), ISO-latin-1 file naming is the default. The reason for the ISO-latin-1 default is that file names are not guaranteed to be possible to interpret according to the Unicode encoding expected (i.e. UTF-8), and file names that cannot be decoded will only be accessible by using "raw file names", in other word file names given as binaries.
As file names are traditionally not binaries in Erlang, applications that need to handle raw file names need to be converted, why the Unicode mode for file names is not default on systems having completely transparent file naming.
As of R14B01, the most basic file handling modules
(
Raw file names is a new feature in OTP R14B01, which allows the
user to supply completely uninterpreted file names to the
underlying OS/filesystem. They are supplied as binaries, where it
is up to the user to supply a correct encoding for the
environment. The function
Note that on Windows,
As returned by
If VM is in Unicode filename mode,
An atom which is named from the POSIX error codes used in Unix, and in the runtime libraries of most C compilers.
Must denote a valid date and time.
On some platforms, this function might have no effect.
This function only succeeds in platforms that implement this feature. When it succeeds, space is preallocated for the file but the file size might not be updated. This behaviour depends on the preallocation implementation. To guarantee the file size is updated one must truncate the file to the new size.
Changes group of a file. See
Changes permissions of a file. See
Changes owner of a file. See
Changes owner and group of a file. See
Changes the modification and access times of a file. See
Changes the modification and last access times of a file. See
Closes the file referenced by
Note that if the option
Reads Erlang terms, separated by '.', from
The file was successfully read.
An error occurred when opening the file or reading it.
See
An error occurred when interpreting the Erlang terms in
the file. Use
Example:
f.txt: {person, "kalle", 25}.
{person, "pelle", 30}.
1> file:consult("f.txt"). {ok,[{person,"kalle",25},{person,"pelle",30}]}
The encoding of of
Copies
The argument
If both
If
If
Returns
Typical error reasons: As for
Tries to delete the directory
Typical error reasons are:
Missing search or write permissions for the parent
directories of
The directory is not empty.
The directory does not exist.
A component of
Attempt to delete the current directory. On some
platforms,
Tries to delete the file
Typical error reasons are:
The file does not exist.
Missing permission for the file or one of its parents.
The file is a directory and the user is not super-user.
A component of the file name is not a directory. On some
platforms,
In a future release, a bad type for the
Reads and evaluates Erlang expressions, separated by '.' (or
',', a sequence of expressions is also an expression), from
The file was read and evaluated.
An error occurred when opening the file or reading it.
See
An error occurred when interpreting the Erlang
expressions in the file. Use
The encoding of of
The same as
This function is obsolete. Use
Given the error reason returned by any function in this module, returns a descriptive string of the error in English.
Returns
In rare circumstances, this function can fail on Unix. It may happen if read permission does not exist for the parent directories of the current directory.
Typical error reasons are:
Missing read permission for one of the parents of the current directory.
This function returns
Typical error reasons are:
The operating system has no concept of drives.
The drive does not exist.
The format of
Lists all the files in a directory. Returns
Typical error reasons are:
Missing search or write permissions for
The directory does not exist.
Tries to create the directory
Typical error reasons are:
Missing search or write permissions for the parent
directories of
There is already a file or directory named
A component of
There is a no space left on the device.
A component of
Makes a hard link from
Typical error reasons:
Missing read or write permissions for the parent
directories of
Hard links are not supported on this platform.
This function creates a symbolic link
Typical error reasons:
Missing read or write permissions for the parent directories
of
Symbolic links are not supported on this platform.
This function returns the configured default file name encoding to use for raw file names. Generally an application supplying file names raw (as binaries), should obey the character encoding returned by this function.
By default, the VM uses ISO-latin-1 file name encoding on filesystems and/or OSes that use completely transparent file naming. This includes all Unix versions except MacOSX, where the vfs layer enforces UTF-8 file naming. By giving the experimental option
On Windows, this function also returns
Opens the file
The file, which must exist, is opened for reading.
The file is opened for writing. It is created if it does
not exist. If the file exists, and if
The file will be opened for writing, and it will be
created if it does not exist. Every write operation to a
file opened with
The file, when opened for writing, is created if it
does not exist. If the file exists, open will return
This option does not guarantee exclusiveness on file systems that do not support O_EXCL properly, such as NFS. Do not depend on this option unless you know that the file system supports it (in general, local file systems should be safe).
The
When this option has been given, read operations on the file will return binaries rather than lists.
If this option is used, the data in subsequent
The purpose of this option is to increase performance
by reducing the number of operating system calls, so the
When this option is used, the result of
For example, when
The same as
This option activates read data buffering. If
The
If
The same as
Makes it possible to read or write gzip compressed
files. The
Makes the file perform automatic translation of characters to and from a specific (Unicode) encoding. Note that the data supplied to file:write or returned by file:read still is byte oriented, this option only denotes how data is actually stored in the disk file.
Depending on the encoding, different methods of reading and writing data is preferred. The default encoding of
If data is sent to an
The allowed values for
The default encoding. Bytes supplied to i.e. file:write are written as is on the file, likewise bytes read from the file are returned to i.e. file:read as is. If the
Characters are translated to and from the UTF-8 encoding before being written to or read from the file. A file opened in this way might be readable using the file:read function, as long as no data stored on the file lies beyond the ISO-latin-1 range (0..255), but failure will occur if the data contains Unicode codepoints beyond that range. The file is best read with the functions in the Unicode aware
Bytes written to the file by any means are translated to UTF-8 encoding before actually being stored on the disk file.
Works like
Works like
Works like
Works like
The Encoding can be changed for a file "on the fly" by using the
This option is not allowed on
Returns:
The file has been opened in the requested mode.
The file could not be opened.
In previous versions of
Typical error reasons:
The file does not exist.
Missing permission for reading the file or searching one of the parent directories.
The named file is not a regular file. It may be a directory, a fifo, or a device.
A component of the file name is not a directory. On some
platforms,
There is a no space left on the device (if
Searches the path
The file was successfully read.
The file could not be found in any of the directories in
An error occurred when opening the file or reading it.
See
An error occurred when interpreting the Erlang terms in
the file. Use
The encoding of of
Searches the path
The file was read and evaluated.
The file could not be found in any of the directories in
An error occurred when opening the file or reading it.
See
An error occurred when interpreting the Erlang
expressions in the file. Use
The encoding of of
Searches the path
The file has been opened in the requested mode.
The file could not be found in any of the directories in
The file could not be opened.
Searches the path
The file was read and evaluated.
The file could not be found in any of the directories in
An error occurred when opening the file or reading it.
See
An error occurred when interpreting the Erlang
expressions in the file. Use
The encoding of of
The same as
If
If this node's file server is not a slave, the file was
opened by this node's file server, (this implies that
In all other cases.
This function is intended for debugging only.
Sets the position of the file referenced by
The same as
Absolute offset.
Offset from the current position.
Offset from the end of file.
The same as above with
Note that offsets are counted in bytes, not in characters. If the file is opened using some other
Typical error reasons are:
Either
Performs a sequence of
As the position is given as a byte-offset, special caution has to be taken when working with files where
Combines
As the position is given as a byte-offset, special caution has to be taken when working with files where
Performs a sequence of
When positioning in a file with other
Combines
When positioning in a file with other
Reads
For files where
Also if
The function returns:
If the file was opened in binary mode, the read bytes are returned in a binary, otherwise in a list. The list or binary will be shorter than the number of bytes requested if end of file was reached.
Returned if
An error occurred.
Typical error reasons:
The file is not opened for reading.
The file was opened with another
Returns
Typical error reasons:
The file does not exist.
Missing permission for reading the file, or for searching one of the parent directories.
The named file is a directory.
A component of the file name is not a directory. On some
platforms,
There is not enough memory for the contents of the file.
Retrieves information about a file. Returns
-include_lib("kernel/include/file.hrl").
The time type returned in
Since file times is stored in posix time on most OS it is
faster to query file information with the
The record
Size of file in bytes.
The type of the file.
The current system access to the file.
The last time the file was read.
The last time the file was written.
The interpretation of this time field depends on the operating system. On Unix, it is the last time the file or the inode was changed. In Windows, it is the create time.
The file permissions as the sum of the following bit values:
On Unix platforms, other bits than those listed above may be set.
Number of links to the file (this will always be 1 for file systems which have no concept of links).
Identifies the file system where the file is located. In Windows, the number indicates a drive as follows: 0 means A:, 1 means B:, and so on.
Only valid for character devices on Unix. In all other cases, this field is zero.
Gives the
Indicates the owner of the file. Will be zero for non-Unix file systems.
Gives the group that the owner of the file belongs to. Will be zero for non-Unix file systems.
Typical error reasons:
Missing search permission for one of the parent directories of the file.
The file does not exist.
A component of the file name is not a directory. On some
platforms,
Reads a line of bytes/characters from the file referenced by
The function can be used on files opened in
If
The function returns:
One line from the file is returned, including the trailing LF, but with CRLF sequences replaced by a single LF (see above).
If the file was opened in binary mode, the read bytes are returned in a binary, otherwise in a list.
Returned if end of file was reached before anything at all could be read.
An error occurred.
Typical error reasons:
The file is not opened for reading.
The file is was opened with another
This function returns
Typical error reasons:
The file does not exist.
Symbolic links are not supported on this platform.
This function works like
If
Tries to rename the file
Renaming of open files is not allowed on most platforms
(see
Typical error reasons:
Missing read or write permissions for the parent
directories of
Reads and evaluates Erlang expressions, separated by '.' (or ',', a sequence of expressions is also an expression), from the file. Returns one of the following:
The file was read and evaluated.
An error occurred when opening the file or reading it.
See
An error occurred when interpreting the Erlang
expressions in the file. Use
The encoding of of
The same as
Sets the current working directory of the file server to
Typical error reasons are:
The directory does not exist.
A component of
Missing permission for the directory or one of its parents.
In a future release, a bad type for the
Makes sure that any buffers kept by the operating system (not by the Erlang runtime system) are written to disk. On some platforms, this function might have no effect.
Typical error reasons are:
Not enough space left to write the file.
Makes sure that any buffers kept by the operating system (not by the Erlang runtime system) are written to disk. In many ways it's resembles fsync but it not requires to update some of file's metadata such as the access time. On some platforms, this function might have no effect.
Applications that access databases or log files often write a tiny data fragment (e.g., one line in a log file) and then call fsync() immediately in order to ensure that the written data is physically stored on the harddisk. Unfortunately, fsync() will always initiate two write operations: one for the newly written data and another one in order to update the modification time stored in the inode. If the modification time is not a part of the transaction concept fdatasync() can be used to avoid unnecessary inode disk write operations.
Available only in some POSIX systems. This call results in a call to fsync(), or has no effect, in systems not implementing the fdatasync syscall.
Truncates the file referenced by
Sends the file
Sends
The file used must be opened using the raw flag, and the process
calling sendfile must be the controlling process of the socket.
See
If the OS used does not support sendfile, an Erlang fallback using file:read and gen_tcp:send is used.
The option list can contain the following options:
On operating systems with thread support, it is recommended to use
async threads. See the command line flag
Writes
If the file is opened with
Typical error reasons are:
The file is not opened for writing.
There is a no space left on the device.
Writes the contents of the iodata term
Typical error reasons are:
A component of the file name does not exist.
A component of the file name is not a directory. On some
platforms,
There is a no space left on the device.
Missing permission for writing the file or searching one of the parent directories.
The named file is a directory.
Same as
Change file information. Returns
-include_lib("kernel/include/file.hrl").
The time type set in
The following fields are used from the record, if they are given.
The last time the file was read.
The last time the file was written.
On Unix, any value give for this field will be ignored (the "ctime" for the file will be set to the current time). On Windows, this field is the new creation time to set for the file.
The file permissions as the sum of the following bit values:
On Unix platforms, other bits than those listed above may be set.
Indicates the owner of the file. Ignored for non-Unix file systems.
Gives the group that the owner of the file belongs to. Ignored non-Unix file systems.
Typical error reasons:
Missing search permission for one of the parent directories of the file.
The file does not exist.
A component of the file name is not a directory. On some
platforms,
Some operating system file operations, for example a
If the device driver thread pool is active, file operations are done through those threads instead, so the emulator can go on executing Erlang processes. Unfortunately, the time for serving a file operation increases due to the extra scheduling required from the operating system.
If the device driver thread pool is disabled or of size 0, large
file reads and writes are segmented into several smaller, which
enables the emulator so server other processes during the file
operation. This gives the same effect as when using the thread
pool, but with larger overhead. Other file operations, for
example
For increased performance, raw files are recommended. Raw files uses the file system of the node's host machine. For normal files (non-raw), the file server is used to find the files, and if the node is running its file server as slave to another node's, and the other node runs on some other host machine, they may have different file systems. This is seldom a problem, but you have now been warned.
A normal file is really a process so it can be used as an IO
device (see
Caching data to reduce the number of file operations, or rather the number of calls to the file driver, will generally increase performance. The following function writes 4 MBytes in 23 seconds when tested:
= 0 ->
{ok, FD} = file:open(Name, [raw, write, delayed_write, binary]),
ok = create_file_slow(FD, 0, N),
ok = ?FILE_MODULE:close(FD),
ok.
create_file_slow(FD, M, M) ->
ok;
create_file_slow(FD, M, N) ->
ok = file:write(FD, <>),
create_file_slow(FD, M+1, N).]]>
The following, functionally equivalent, function collects 1024
entries into a list of 128 32-byte binaries before each call to
= 0 ->
{ok, FD} = file:open(Name, [raw, write, delayed_write, binary]),
ok = create_file(FD, 0, N),
ok = ?FILE_MODULE:close(FD),
ok.
create_file(FD, M, M) ->
ok;
create_file(FD, M, N) when M + 1024 =< N ->
create_file(FD, M, M + 1024, []),
create_file(FD, M + 1024, N);
create_file(FD, M, N) ->
create_file(FD, M, N, []).
create_file(FD, M, M, R) ->
ok = file:write(FD, R);
create_file(FD, M, N0, R) when M + 8 =< N0 ->
N1 = N0-1, N2 = N0-2, N3 = N0-3, N4 = N0-4,
N5 = N0-5, N6 = N0-6, N7 = N0-7, N8 = N0-8,
create_file(FD, M, N8,
[<> | R]);
create_file(FD, M, N0, R) ->
N1 = N0-1,
create_file(FD, M, N1, [<> | R]).]]>
Trust only your own benchmarks. If the list length in
If the size of each binary is increased to 64 bytes, it will also run slightly faster, but the code will be twice as clumsy. In the current implementation are binaries larger than 64 bytes stored in memory common to all processes and not copied when sent between processes, while these smaller binaries are stored on the process heap and copied when sent like any other term.
So, with a binary size of 68 bytes
A raw file is really a port. When writing data to a port, it is
efficient to write a list of binaries. There is no need to
flatten a deep list before writing. On Unix hosts, scatter output,
which writes a set of buffers in one operation, is used when
possible. In this way
For raw files,
The options
And, as a really bad example,
If an error occurs when accessing an open file with the