In this guide, we will discuss 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
File Operation Methods in Erlang
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.
Reading the Contents of a File One Line at a Time
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.
Syntax
- Opening a file β Open(File,Mode)
- Reading a file β read(FileHandler,NumberofBytes)
Parameters
- 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.
Following are some of the available modes β
- 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.
- 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}.
- 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.
Return Value
- 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.
For example
-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 β
Sr.No. | Method & Description |
---|---|
1 | file_read Available to allow the reading of all the contents of a file at one time. |
2 | write Used to write the contents to a file. |
3 | copy used to make a copy of an existing file. |
4 | delete This method is used to delete an existing file. |
5 | list_dir This method is used to list down the contents of a particular directory. |
6 | make_dir This method is used to create a new directory. |
7 | rename This method is used to rename an existing file. |
8 | file_size This method is used to determine the size of the file. |
9 | is_file This method is used to determine if a file is indeed a file. |
10 | is_dir This method is used to determine if a directory is indeed a directory. |
Next Topic : Click Here
Pingback: Erlang - sum | Adglob Infosystem Pvt Ltd