Erlang – File I/O

Erlang file i/o

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
1file_read
Available to allow the reading of all the contents of a file at one time.
2write
Used to write the contents to a file.
3copy
used to make a copy of an existing file.
4delete
This method is used to delete an existing file.
5list_dir
This method is used to list down the contents of a particular directory.
6make_dir
This method is used to create a new directory.
7rename
This method is used to rename an existing file.
8file_size
This method is used to determine the size of the file.
9is_file
This method is used to determine if a file is indeed a file.
10is_dir
This method is used to determine if a directory is indeed a directory.

Next Topic : Click Here

This Post Has One Comment

Leave a Reply