17 Input and output

 

This chapter covers

  • Understanding Julia’s I/O system
  • Using the most common functions for reading and writing to files
  • Reading and writing to strings, sockets, and processes
  • Adding code to the rocket example for loading rocket engines from CSV files

Real programs need to be able to read input from users and write out results. In this chapter, you will learn about the Julia I/O system (input and output system). It provides an abstraction for working with files, network communications, and interprocess communications as well as interacting with the console (keyboard and screen).

Julia is very popular in data science, where we work a lot with input data in the form of CSV files (comma separated values). That is why the main code example will center on parsing a CSV file containing data about rocket engines, as well as writing rocket engine data to a CSV file.

17.1 Introducing Julia’s I/O system

Let’s get a bird’s-eye view of the I/O system in Julia. It is centered on the abstract type IO. It has concrete subtypes, such as IOStream, IOBuffer, Process, and TCPSocket. Each type allows you to read and write data from different I/O devices, such as files, text buffers, running processes (programs you started), or network connections.

From the type hierarchy in figure 17.1 you can see that functions such as print, show, read, readline, and write are available for all I/O types. Some functions, such as eof and position, are not available for all I/O types.

17.2 Reading data from a process

17.3 Reading and writing to a socket

17.4 Parsing a CSV file

17.4.1 Loading rocket engine data

17.4.2 Saving rocket engine data

Summary