Working with Text Files in Julia

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).

Creation of a File

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 

Opening an existing File

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



filter_none

edit
close

play_arrow

link
brightness_4
code

# 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 mode
hij =  open("example.txt","a")

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

open("example.txt") do file
    # modify mile content 
end

chevron_right


Reading Contents of a File

Reading entire file

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

filter_none

edit
close

play_arrow

link
brightness_4
code

# METHOD 1
# reading the content of file as a string
mydata = read(abc, String)  
  
# METHOD 2
mydata = open("example.txt") do file
    read(file, String)
end

chevron_right


File Read 

Reading file line by line

Here we  will use “readlines” method to ready the file as an array with each line of a file as an array element.

Julia

filter_none

edit
close

play_arrow

link
brightness_4
code

# open file 
mni = open("example.txt","r");
  
# reading file content line by line
line_by_line = readlines(mni)

chevron_right


Writing to a File

Here we will change the mode of the file from read to write in order to write file inside “open” method.



 Using write mode 

Julia

filter_none

edit
close

play_arrow

link
brightness_4
code

# open file in write mode
abc = open("example.txt", "w")

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


Using append mode

Julia

filter_none

edit
close

play_arrow

link
brightness_4
code

# open file in append mode
abc = 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)

chevron_right


Modifying the contents of a file

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

filter_none

edit
close

play_arrow

link
brightness_4
code

open("example.txt") do abc 
  
       # change the case of content 
       # from uppercase to lowercase
       lowercase(read(abc, String)) 
end

chevron_right


Closing the file

Here we will use the close method in Julia to close the file after modifying its content.

Julia

filter_none

edit
close

play_arrow

link
brightness_4
code

close(abc) # close method will close the file

chevron_right





My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.