SoFunction
Updated on 2025-04-08

Summary of common methods for file directory operation in Erlang IO programming

Erlang's modules used to manipulate file I/O are:

File module: The methods to open, read, write, and close the file operation directory are basically here.

filename module: provides platform-independent way to manipulate file names

filelib module: an extension of the file module, providing more practical tools, built on the basis of the file module

io module: a series of methods for manipulating open files, parsing formats, formatting output, etc.

1. Open the file:

Copy the codeThe code is as follows:

{ok,F}=file:open("",read). %read mode is turned on
{ok,F}=file:open("",write). %Write mode
{ok,F}=file:open("",[read,write]). %Read, write, binary mode

All supported modes are found in the documentation.

2. Read:
(1) If you read in an Erlang term, use:

Copy the codeThe code is as follows:

io:read(F,'').

The second parameter is a prompt, used as a prompt when standard input.
This method has a deformation read/3
Copy the codeThe code is as follows:

read(IoDevice, Prompt, StartLine)

The third parameter is used to specify the number of start lines.

(2) If it is read by bytes, the file must be opened in raw mode:

Copy the codeThe code is as follows:

{ok, Data}=file:read(F,100).

(3) Read by line:
Copy the codeThe code is as follows:

io:get_line(F, '').

(4) Read the contents of the entire file:
Copy the codeThe code is as follows:

{ok,Binary}=file:read_file("").

Note that the returned binary type

(5) Random reading:

Copy the codeThe code is as follows:

{ok,Binary}=file:pread(F, 22, 46).

The second parameter is the start position, the third parameter is the read length, and the returned binary type is also type.

3. Write to the file:
(1) Use the io:format/3 method:

Copy the codeThe code is as follows:

{ok, S} = file:open("", write).
io:format(S, "~s~n", ["Hello readers"]).
io:format(S, "~w~n", [123]).

The characters starting with ~ are formatting commands, such as common ones:
~c   ascii code
~f   Floating point number
~s   String
~w   Erlang term
~p   Similar to ~w, but will automatically wrap when multiple lines are
~n   Apparently, line breaks

(2) Write to the entire file:

Copy the codeThe code is as follows:

file:write_file(File, IO)

Where IO can be of list, integer or binary type

(3) Random write:

Copy the codeThe code is as follows:

file:pwrite(F, 10, <<"new">>)

4. Close the file:
Copy the codeThe code is as follows:

file:close(F).

5. Directory operation:=
All are Linux command-style operations.
Copy the codeThe code is as follows:

cd("/home/dennis/"). % Enter the directory
file:list_dir(".").   % List the current directory file
file:make_dir("test"). %Create the test directory
file:del_dir("test").   %Delete the test directory

6. Obtain file information, such as file size, last modification time, etc. Call the file:read_file_info/1 method, which returns a file_info record type, which contains the specific information of the file, such as type, size, etc.

Copy the codeThe code is as follows:

{ok, Facts} =file:read_file_info(File).
io:format("~s~n",{Facts#file_info.type, Facts#file_info.size}).

7. Copy and delete files:
Copy the codeThe code is as follows:

file:copy(Source, Destination).
file:delete(File).

This note only records some commonly used methods, and some advanced tool methods are not involved. For details, please refer to Erlang's documentation.