Working with Text Files in Julia - GeeksforGeeks

25 Aug, 2020
Julia is Programming Language that is fast and dynamic in nature (most suitable for performing numerical and scientific applications) and it is optionally typed (the rich language of descriptive type and type declarations which is used to solidify our program) and general-purpose and open-sourced. Julia supports file handling like other programming languages with much higher efficiency and associated methods i.e reading, writing, and closing a file. It is a combination of  C language ( powerful performance) and Python( simplicity).
In order to work with a file in Julia, firstly we need to create a new file using “touch”(used to create a new empty file) method, “pwd”(used for checking the present working directory of the system) method and “cd”(used to change directory and create the file where we want) method.
touch("example.txt")

File Created 
To open the file in Julia we have “open” method. The open method takes in two arguments i.e filename (file to open) and mode of operation (read, write or append).
For read mode we use (“r”), write mode(“w”), and append mode(“a”).
Julia
# here we open file in read mode abc = open("example.txt","r")   # here we open file in write mode efg =  open("example.txt","w")   # here we open file in append modehij =  open("example.txt","a")
file open in different mode
Just like Python, Julia also has a “do“ method that helps us to avoid the mode of operation and prevent us from the issue of closing the file every time after modifying it.
Julia
open("example.txt") do file    # modify mile content end
Here we will use “read” method to read the content of a file and it takes in two arguments i.e file to read and method to read the file as (string, integer, etc).
Julia
# METHOD 1# reading the content of file as a stringmydata = read(abc, String)    # METHOD 2mydata = open("example.txt") do file    read(file, String)end
File Read 
Here we  will use “readlines” method to ready the file as an array with each line of a file as an array element.
Julia
# open file mni = open("example.txt","r");  # reading file content line by lineline_by_line = readlines(mni)
Here we will change the mode of the file from read to write in order to write file inside “open” method.
Julia
# open file in write modeabc = open("example.txt", "w")
Now , we will use write method in Julia to write content to the “example.txt ” file, and write method will delete the previous content from file or will create the file if it does not exist.
Julia
# writing to a file using write() method write(abc, " Hello World , Julia welcomes you")     # We need to close the file in order # to write the content from the disk to file close(abc)
Julia
# open file in append modeabc = open("example.txt", "a")   # writing to a file using write() method write(abc, " Hello World , Julia welcomes you")     # We need to close the file in order to write the content from the disk to file close(abc)
Here we will learn to modify the content of a file i.e we will change the case of content in a file from uppercase to lowercase using the lowercase method.
Julia
open("example.txt") do abc          # change the case of content        # from uppercase to lowercase       lowercase(read(abc, String)) end
Here we will use the close method in Julia to close the file after modifying its content.
Julia
close(abc) # close method will close the file
julia-FileHandling
Picked
Julia
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Decision Making in Julia (if, if-else, Nested-if, if-elseif-else ladder)
Get array dimensions and size of a dimension in Julia - size() Method
Exception handling in Julia
Searching in Array for a given element in Julia
Find maximum element along with its index in Julia - findmax() Method
Get number of elements of array in Julia - length() Method
Join an array of strings into a single string in Julia - join() Method
Working with Excel Files in Julia
File Handling in Julia
Getting last element of an array in Julia - last() Method