Erlang - File I/O

Erlang provides a number of methods when working with I/O. It has easier classes to provide the following functionalities for files −
Reading files
Writing to files
Seeing whether a file is a file or directory
Let’s explore some of the file operations Erlang has to offer. For the purposes of these examples, we are going to assume that there is a file called NewFile.txt which contains the following lines of text
Example1
Example2
Example3
This file will be used for the read and write operations in the following examples.
The general operations on files are carried out by using the methods available in the file library. For the reading of files, we would need to first use the open operation and then use the read operation which is available as a part of the file library. Following is the syntax for both of these methods.
Opening a file – Open(File,Mode)
Reading a file – read(FileHandler,NumberofBytes)
File − This is the location of the file which needs to be opened.
File − This is the location of the file which needs to be opened.
Mode − This is the mode in which the file needs to be opened in.
Mode − This is the mode in which the file needs to be opened in.
Following are some of the available modes −
Read − The file, which must exist, is opened for reading.
Read − The file, which must exist, is opened for reading.
Write − The file is opened for writing. It is created if it does not exist. If the file exists, and if write is not combined with read, the file will be truncated.
Write − The file is opened for writing. It is created if it does not exist. If the file exists, and if write is not combined with read, the file will be truncated.
Append − 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 append will take place at the end of the file.
Append − 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 append will take place at the end of the file.
Exclusive − The file, when opened for writing, is created if it does not exist. If the file exists, open will return {error, exist}.
Exclusive − The file, when opened for writing, is created if it does not exist. If the file exists, open will return {error, exist}.
FileHandler − This is the handle to a file. This handle is the one that would be returned when the file:open operation is used.
FileHandler − This is the handle to a file. This handle is the one that would be returned when the file:open operation is used.
NumberofByte − This is the number of bytes of information that needs to be read from the file.
NumberofByte − This is the number of bytes of information that needs to be read from the file.
Open(File,Mode) − Returns a handle to the file, if the operation is successful.
Open(File,Mode) − Returns a handle to the file, if the operation is successful.
read(FileHandler,NumberofBytes) − Returns the requested read information from the file.
read(FileHandler,NumberofBytes) − Returns the requested read information from the file.
-module(helloworld). 
-export([start/0]). 

start() -> 
   {ok, File} = file:open("Newfile.txt",[read]),
   Txt = file:read(File,1024 * 1024), 
   io:fwrite("~p~n",[Txt]).
Output − When we run the above program, we will get the following result.
Example1

Let us now discuss some other methods available for file operations −
file_read
Available to allow the reading of all the contents of a file at one time.
write
Used to write the contents to a file.
copy
used to make a copy of an existing file.
delete
This method is used to delete an existing file.
list_dir
This method is used to list down the contents of a particular directory.
make_dir
This method is used to create a new directory.
rename
This method is used to rename an existing file.
file_size
This method is used to determine the size of the file.
is_file
This method is used to determine if a file is indeed a file.
is_dir
This method is used to determine if a directory is indeed a directory.
 Print
 Add Notes
 Bookmark this page