The File
module includes many useful functions to manipulate files using Elixir.
Let's explore how to perform the most common functions while using built-in Elixir functions:
Create and write a new file
Read an existing file
Stream the content of an existing file
Rename a preexisting file
Delete a file
We'll focus on creating and filling a text file in this scenario. The syntax of the write command is as follows:
File.write(path,content,options)
This command writes the content
to the file specified in the path
. The options
are optional parameters specifying the encoding of the resulting file content. The file is automatically created if it does not exist. When running successfully, this command returns:ok
. A tuple {:error, reason}
is returned if a problem occurs.
#Define the file contentfile_content = "Hello World"#Run the commandoutput = File.write("/usercode/output/test.txt", file_content)#Display the outputIO.puts output
Let's explain the code above.
Line 2: Define the file content.
Line 4: Execute the File.write
command to create a text file and write the specified content into it. By default, the already existing file content is overwritten when running this command.
Line 6: Display the output status of the command.
Within the same context, let's now try to append some text to an existing file while using the :append
option.
#Define the file contentfile_content = "Hello World"#Run the command_output = File.write("/usercode/output/test.txt", file_content,[:append])output = File.write("/usercode/output/test.txt", "\nappended info",[:append])#Display the outputIO.puts output#Display the content of the resulting fileIO.puts File.read!("/usercode/output/test.txt")
Let's explain the code above.
Line 2: Define the file content.
Line 4: Execute the File.write
command to create a text file and write the specified content into it.
Line 5: Execute the File.write
command with the :append
option in order to write additional content to the text file created.
Line 7: Display the output status of the command.
Line 9: Read and display the content of the resulting file.
In this scenario, we will describe two variants of the read command.
The syntax of the first variant command is as follows:
File.read(path)
The path maps the file being read. When executed successfully, this command returns a tuple {:ok, content}
where content is a binary data object containing the file content. If a problem occurs, a tuple {:error, reason}
is returned.
#Read the sample file "test.txt"{:ok,content} = File.read("test.txt")#Display the file contentIO.puts content
Let's explain the code above:
Line 2: Read a sample file named test.txt
.
Line 4: Display the content of the given file.
The syntax of the other variant is as follows:
File.read!(path)
This command returns directly the binary content of a given filename.
If an error occurs a File.Error
exception is returned.
#Read the sample file "test.txt"content = File.read!("test.txt")#Display the file contentIO.puts content
Let's explain the code above:
Line 2: The File.read!
command returned the content of the sample file test.txt
.
Line 4: Display the content of the given file.
The streaming function comes into play when reading large files to offload resources like network resources. The chosen file will be streamed line by line instead of being loaded entirely in the memory.
The syntax of this command is:
File.stream(path,modes,line_or_bytes)
The path maps the file being read line by line. The modes parameter refers to the file modes (i.e.: raw
, read_ahead
, binary
). The line_or_bytes
parameter indicates whether to read a line or a given number of bytes. When executed successfully, this command returns a stream as a result.
Let's see how it works.
File.stream!("test.txt",line_or_bytes: :line)|> Enum.each(&IO.puts /1)
Let's explain the code above:
Line 1: Stream the content of a sample file named test.txt
line by line.
Line 2: Display the content of the streamed line.
This command serves for renaming a file in the file system.
The syntax of this command is as follows:
File.rename(source, destination)
This function may serve also to move files (and directories) between directories. When running successfully, this command returns :ok
. If a problem occurs, a tuple {:error, reason}
is returned.
Let's look at the command below.
Test dataTesting data
We executed the following steps in the code widget above:
Line 1: Rename the file test.txt
to testing.txt
.
Line 2: Display the output status of this command.
To remove a file, which is no longer needed from the file system, we may use the File.rm
function. The syntax of this command is as follows:
File.rm(path)
When running successfully, this command returns :ok
. If a problem occurs, a tuple {:error, reason}
is returned.
Let's test it below.
output = File.rm("test.txt")IO.puts output
We performed the following steps in the code widget above:
Line 1: Remove the file test.txt
permanently.
Line 2: Display the output status of this command.
Free Resources