Working with files

To work with files, we need the IOStream type. IOStream is a type with the IO supertype and has the following characteristics:

  • The fields are given by fieldnames(IOStream):
(:handle, :ios, :name, :mark)  
  • The types are given by IOStream.types:
(Ptr{Nothing}, Array{UInt8,1}, AbstractString, Int64)

The file handle is a pointer of the Ptr type, which is a reference to the file object.

Opening and reading a line-oriented file with the example.dat name is very easy:

// code in Chapter 8io.jl 
fname = "example.dat"
f1 = open(fname) 

fname is a string that contains the path to the file, using the escaping of special characters with when necessary. For example, in Windows, when the file is in the test folder on the D: drive, this would become d:\test\example.dat. The f1 variable is now an IOStream(<file example.dat>) object.

To read all lines one after another in an array, use data = readlines(f1), which returns 3-element Array{String,1}:

"this is line 1."
"42; 3.14"
"this is line 3."

For processing line by line, now only a simple loop is needed:

for line in data 
  println(line) # or process line  
end 
close(f1) 

Always close the IOStream object to clean and save resources. If you want to read the file into one string, use readall (for example, see the word_frequency program in Chapter 5, Collection Types). Use this only for relatively small files because of the memory consumption; this can also be a potential problem when using readlines.

There is a convenient shorthand with the do syntax for opening a file, applying a function process, and closing it automatically. This goes as follows (file is the IOStream object in this code):

open(fname) do file 
    process(file) 
end 

As you can recall, in the Map, filter, and list comprehensions section in Chapter 3, Functions, do creates an anonymous function, and passes it to open. Thus, the previous code example would have been equivalent to open(process, fname). Use the same syntax for processing a fname file line by line without the memory overhead of the previous methods, for example:

open(fname) do file
for line in eachline(file) print(line) # or process line end end

Writing a file requires first opening it with a "w" flag, writing strings to it with write, print, or println, and then closing the file handle that flushes the IOStream object to the disk:

fname =   "example2.dat" 
f2 = open(fname, "w")
write(f2, "I write myself to a file ") # returns 24 (bytes written) println(f2, "even with println!") close(f2)

Opening a file with the "w" option will clear the file if it exists. To append to an existing file, use "a".

To process all the files in the current folder (or a given folder as an argument to readdir()), use this for loop:

for file in readdir() 
  # process file 
end

For example, try out this code, which goes up to the parent directory, prints them out, and then comes back:

mydir = pwd() 
cd("..") 
 
for fn in readdir() 
    println(fn) 
end 
cd(mydir) 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.148.104.242