liveBook · Manning

IO

As explained in Julia as a Second Language

IO is Julia's abstract supertype for values that provide input and output operations.

A function that writes CSV rows need not decide whether they go to the terminal or a file. Give it an IO object and pass that object as the first argument to println. For example, println(io, "name,size,price") writes to the destination represented by io; println("hello") is shorthand for println(stdout, "hello"). Passing an ordinary string instead does not change the destination—it prints the string. section 5.3.1

IO is Julia’s abstract type for input and output objects. Its concrete subtypes include IOStream for files, IOBuffer for text buffers, Process for running processes, and TCPSocket for network connections. Once an object exists, familiar operations such as read, readline, write, and print work across many of these destinations. section 17.1

Choose an object for the job:

  • For a file, open("pizza-sales.csv", "w") creates an IO object for writing. Pass it to the CSV-writing function, then close it; closing a writer matters because otherwise data may be lost. section 5.3.1 section 17.1
  • For text already in memory, IOBuffer(s) lets readuntil, position, and eof process a string through I/O calls. section 17.1
  • For a command, open(`grep "Rocket Lab" rocket-engines.csv`) starts a process whose output can be read like a file. Not every operation transfers: position(io) fails for this Process object. section 17.2
  • For a network connection, connect returns a TCPSocket. Read from it with readuntil or readline, and send a line with println. section 17.3
Listing 17.1 Reading a CSV file line by line to demonstrate basic I/O functionality
io = open("rocket-engines.csv")
while !eof(io)
   line = readline(io)
   words = split(line, ',')
   println(join(words, '\t'))
end
close(io)

A file-reading loop uses eof to stop, readline to fetch each CSV row, and close when finished. The same line-oriented approach can be reused with other suitable IO objects. section 17.1

Listing 17.5 An io.jl file with added save_engines code
function save_engines(path::AbstractString, engines)
   open(path, "w") do io
       println(io, "name,company,mass,thrust,Isp")
       for (name, egn) in engines
           row = [name, "", egn.mass, egn.thrust, egn.Isp]
           join(io, row, ',')
           println(io)
       end
   end
end

For output, open(path, "w") do io passes the file object into a block that writes a header and rows. This form leaves closing the object to Julia. section 17.4.2

How Julia as a Second Language covers it

How deep it goes
Defines the abstract type and works through reading, writing, closing, and a method limitation without implementing concrete IO subtypes.
Examples
Uses pizza-sales CSV output, rocket-engine CSV reading and writing, grep process output, and a netcat socket exchange.
What it assumes
Assumes familiarity with basic Julia function calls and loops while introducing the IO hierarchy through concrete operations.
What you can do after
Apply it via Julia Base functions to read or write through an appropriate IO object and close it when finished.
How it connects
Connects IO to abstract types, CSV parsing, standard output, process execution, and TCP sockets.

Also explained in

Compare how 6 books explain IO →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Julia as a Second Language. The section links point to the parts of the book it draws on.