English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Erlang File Write Method

Erlang Files

This method is used to write content to the file.

Syntax

write(FileHandler,text)

Parameter

  • FileHandler- This is the file handle. The handlefile:openIs the handle that will be returned when using this operation.

  • Text - Text that needs to be added to the file.

Return Value

None

-module(helloworld). 
-export([start/0]). 
start() -> 
   {ok, Fd} = file:open("Newfile.txt", [write]), 
   file:write(Fd,"New Line").

Each time the above code is run, the "New Line" line will be written to the file. Please note that since the mode is set to write, any previous content in the file will be overwritten.

To append to the existing content of the file, you need to change the mode to append, as shown in the following program.

-module(helloworld). 
-export([start/0]). 
start() -> 
   {ok, Fd} = file:open("Newfile.txt", [append]), 
   file:write(Fd,"New Line").

Erlang Files