- Erlang - Web Programming
- Erlang - Drivers
- Erlang - Performance
- Erlang - Concurrency
- Erlang - OTP
- Erlang - Distributed Programming
- Erlang - Ports
- Erlang - Databases
- Erlang - Emails
- Erlang - Processes
- Erlang - Funs
- Erlang - Binaries
- Erlang - BIFS
- Erlang - Guards
- Erlang - Pattern Matching
- Erlang - Preprocessors
- Erlang - Header Files
- Erlang - Macros
- Erlang - Exceptions
- Erlang - Records
- Erlang - Tuples
- Erlang - Maps
- Erlang - Atoms
- Erlang - File I/O
- Erlang - Lists
- Erlang - Strings
- Erlang - Numbers
- Erlang - Recursion
- Erlang - Modules
- Erlang - Functions
- Erlang - Decision Making
- Erlang - Loops
- Erlang - Operators
- Erlang - Variables
- Erlang - Data Types
- Erlang - Shell
- Erlang - Basic Syntax
- Erlang - Environment
- Erlang - Overview
- Erlang - Home
Erlang Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Erlang - File I/O
Erlang provides a number of methods when working with I/O. It has easier classes to provide the following functionapties 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 pnes 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 pbrary. 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 pbrary. 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 |
Available to allow the reading of all the contents of a file at one time. |
2 |
Used to write the contents to a file. |
3 |
used to make a copy of an existing file. |
4 |
This method is used to delete an existing file. |
5 |
This method is used to pst down the contents of a particular directory. |
6 |
This method is used to create a new directory. |
7 |
This method is used to rename an existing file. |
8 |
This method is used to determine the size of the file. |
9 |
This method is used to determine if a file is indeed a file. |
10 |
This method is used to determine if a directory is indeed a directory. |