Julia is a high-level and dynamic programming language that supports file handling, that is, reading and writing files.
We can achieve this with the following standard functions:
touch()
open()
read()
close()
The touch
function is used to create a file. The pwd()
function can be used to check the current working directory, and the cd path
function can be used to provide the path where the file is created.
# creating a file
julia> touch("demo.txt")
The open()
function is used to open a file. The function takes in two arguments: filename
and mode. The mode is r
for reading and w
for writing.
# opening a file
julia> book = open("demo.txt", "r")
This function can also be used to write to a file.
# opening file in write mode
julia> x = open("demo.txt", "w")
julia> write(x, "Hello, World!")
The read()
function is used to read the entire content from a file. Again, from our previous example:
# reading a file
julia> data = read(book, String)
To close the file, the close()
function is used. It closes the connection. Here, we close our demo.txt
file:
# closing a file
julia> close(book)
We have a terminal below to test all the above commands: