Storing Output on a File in Julia
Julia provides a vast library to store and save the output in multiple file formats. We can store the output in various forms such as CSV(comma-separated value) or in Excel or just simply a text file.
Storing Data on Text File
Using open() function
In order to store the data in a text file, we need to use open() function with “w” flag which allows us to write output to a file. The open function takes two parameters first the filename and second the mode you need the file to open.
In order to store data in a text file, we need to use the .txt extension after the filename. Also if the file is not present it will create the file for you. The console prints the numbers of characters in the file.
Example:
Julia
# Output in a txt file# opening file with .txt extension and in write mode# let the ans be the output of a questionans = "Hello World"open("geek.txt", "w") do file write(file, ans)end |
Using DelimitedFiles.writedlm() function
Another method to store data is to use DelimitedFiles.writedlm() function. The function takes two parameters the first is the filename and the second parameter is output. In order to use this function, we need to import DelimitedFiles.
Example:
Julia
# importing the moduleusing DelimitedFiles# The number is the output of random numbersnumbers = rand(5)writedlm("geek.txt", numbers) |


Storing Data on CSV File
Using CSV.jl library
We can store data on CSV files by importing a library CSV.jl which provides various functions related to storing reading and writing output into a CSV file. It provides more control than other methods. We also need to install the DataFrame package.
In order to use this, we need to install the package. To install type the following in Julia console.
# importing the Pkg module
using Pkg
Pkg.add("CSV")
# installing the DataFrame
Pkg.add("DataFrames")
Now to store output we just need to import it.
Example:
Julia
# importing the required moduleusing CSV, DataFrames# a 2d array of random numbersnumbers = rand(5, 5)# using write methodCSV.write("geek.csv", DataFrame(numbers), header = false) |
Using DelimitedFiles.writedlm() function
Another way to store data is to use DelimitedFiles.writedlm() function. The function takes two parameters the first is the filename and the second parameter is output. In order to use this function, we need to import DelimitedFiles. Here also file extension must be .csv.
Example:
Julia
# importing DelimitedFilesusing DelimitedFiles# Here numbers is a 2d arraynumbers = rand(5, 5)# Since it is a comma separated file we need to,# to separate into a table of row and colwritedlm("geek.csv", numbers, ", ") |
Storing Data on Excel File
Using XLXS.jl library
We can store data on excel file by importing a library XLXS.jl which provides various functions related to storing reading and writing output into an excel file. It provides more control than other methods.
To use the package we first need to install the package. To install the package just write the following command in Julia terminal.
# importing the Pkg module
using Pkg
Pkg.add("XLSX")Now to store the data we can use its various methods. We need to use the “w” flag to able to write output the xls file.
Example:
Julia
using XLSX# using "w" to write outputXLSX.openxlsx("geek.xls", mode="w") do f sheet = f[1] # Used for renaming the file for convience XLSX.rename!(sheet, "new_sheet") # This will add output along A column sheet["A1"] = "Welcome" sheet["A2"] = "to" sheet["A3"] = "GeeksForGeeks"end |


Using DelimitedFiles.writedlm() function
Another way to store data is to use DelimitedFiles.writedlm() function. The function takes two parameters the first is the filename and the second parameter is output. In order to use this function, we need to import DelimitedFiles. Here also file extension must be .xls.
Example:
Julia
# importing DelimitedFilesusing DelimitedFiles# here numbers is an array# of 5*5 random numbersnumbers = rand(5, 5)# writedlm functionwritedlm("geek.xls", numbers) |










