Julia has an open function using which you can write to file and read from file. Here, we will first create a file named "myfile.txt" and then read from it.
Step 1: Writing to a file:
$ julia
_
_ _ _(_)_ | A fresh approach to technical computing
(_) | (_) (_) | Documentation: https://docs.julialang.org
_ _ _| |_ __ _ | Type "?help" for help.
| | | | | | |/ _` | |
| | |_| | | | (_| | | Version 0.6.0 (2017-06-19 13:05 UTC)
_/ |\__'_|_|_|\__'_| | Official http://julialang.org/ release
|__/ | x86_64-pc-linux-gnu
julia> fopen = open("myfile.txt","w")
IOStream(<file write>)
julia> write(fopen,"Hello World! Welcome to Julia"*"\n")
30
julia> close(fopen)
Step 2: Reading from files:
julia> ### reading file now
julia> fopen = open("myfile.txt","r")
IOStream(<file write>)
julia> fread = readlines(fopen)
1-element Array{String,1}:
"Hello World! Welcome to Julia"
julia> close(fopen)
julia> fopen = open("myfile.txt","r")
IOStream(<file write>)
julia> fread = readstring(fopen)
"Hello World! Welcome to Julia\n"
julia> close(fopen)
Step 3: Let us now check for the presence of the file. We execute OS commands from within Julia:
shell> ls -l
total 8
-rw-r--r-- 1 ubuntu ubuntu 30 Jun 25 20:30 myfile.txt
julia>
We have seen now that, writing of file has been successful. Also, we are able to read from it.
The following are the read and write modes available in Julia Language:
Step 1: Writing to a file:
$ julia
_
_ _ _(_)_ | A fresh approach to technical computing
(_) | (_) (_) | Documentation: https://docs.julialang.org
_ _ _| |_ __ _ | Type "?help" for help.
| | | | | | |/ _` | |
| | |_| | | | (_| | | Version 0.6.0 (2017-06-19 13:05 UTC)
_/ |\__'_|_|_|\__'_| | Official http://julialang.org/ release
|__/ | x86_64-pc-linux-gnu
julia> fopen = open("myfile.txt","w")
IOStream(<file write>)
julia> write(fopen,"Hello World! Welcome to Julia"*"\n")
30
julia> close(fopen)
Step 2: Reading from files:
julia> ### reading file now
julia> fopen = open("myfile.txt","r")
IOStream(<file write>)
julia> fread = readlines(fopen)
1-element Array{String,1}:
"Hello World! Welcome to Julia"
julia> close(fopen)
julia> fopen = open("myfile.txt","r")
IOStream(<file write>)
julia> fread = readstring(fopen)
"Hello World! Welcome to Julia\n"
julia> close(fopen)
Step 3: Let us now check for the presence of the file. We execute OS commands from within Julia:
shell> ls -l
total 8
-rw-r--r-- 1 ubuntu ubuntu 30 Jun 25 20:30 myfile.txt
julia>
We have seen now that, writing of file has been successful. Also, we are able to read from it.
The following are the read and write modes available in Julia Language:
Mode | Description |
r | read |
r+ | read, write |
w | write, create, truncate |
w+ | read, write, create, truncate |
a | write, create, append |
a+ | read, write, create, append |