- binary level file O/O
- TextStream file I/O
- File pointer & offset

open(file_name[, mode]) → IOStream
Alternate syntax for open, where a string-based mode specifier is used instead of the five booleans. The values of mode correspond to those from fopen(3) or Perl open, and are equivalent to setting the following boolean groups:

  r   read
  r+   read, write
  w   write, create, truncate
  w+   read, write, create, truncate
  a   write, create, append
  a+   read, write, create, append

close(stream)
Close an I/O stream. Performs a flush first.

 

- binary level file O/O

write(stream, x)
Write the canonical binary representation of a value to the given stream.

read(stream, type)
Read a value of the given type from a stream, in canonical binary representation.

# Create a file for mmapping
# (you could alternatively use mmap_array to do this step, too)
A = rand(1:20, 5, 30)
s = open("/tmp/mmap.bin", "w+")
# We'll write the dimensions of the array as the first two Ints in the file
write(s, size(A,1))
write(s, size(A,2))
# Now write the data
write(s, A)
close(s)

# Test by reading it back in
s = open("/tmp/mmap.bin")   # default is read-only
m = read(s, Int)
n = read(s, Int)
A2 = mmap_array(Int, (m,n), s)

예제출처 :http://docs.julialang.org/en/release-0.2/stdlib/base/#memory-mapped-i-o

 

- TextStream File I/O

readline(stream)
Read a single line of text, including a trailing newline character (if one is reached before the end of the input).

readlines(stream)
Read all lines as an array.

print(x)
Write (to the default output stream) a canonical (un-decorated) text representation of a value if there is one, otherwise call show. The representation used by print includes minimal formatting and tries to avoid Julia-specific details.

println(x)
Print (using print()) x followed by a newline.

writecsv(filename, array)
Equivalent to writedlm with delim set to comma.

- ex)

# Create a csv file, calculate(+) from the csv file and write on new file.
mat = rand(3, 2)
writecsv("rand.csv",mat)
fout = open("plus_rand.txt","w")

open("rand.csv","r") do f
  for line in eachline(f)
    line = strip(line)
    split_value = split(line, ",")
    val1 = float( split_value[1] )
    val2 = float( split_value[2] )
    println(fout,val1,"+",val2," = ",val1+val2)
  end
end

close(fout)

 

- File pointer & offset

position(s)
Get the current position of a stream.

seek(s, pos)
Seek a stream to the given position.

eof(stream)
Tests whether an I/O stream is at end-of-file. If the stream is not yet exhausted, this function will block to wait for more data if necessary, and then return false. Therefore it is always safe to read one byte after seeing eof return false. eof will return false as long as buffered data is still available, even if the remote end of a connection is closed.

# test.txt
# abcdefghijklmnopqrstuvwxyz
# 
fd = open("test.txt","r")
readline(fd)

position(fd)

seek(fd,17)
readline(fd)


output
"abcdefghijklmnopqrstuvwxyz\n"
27
"rstuvwxyz\n"