Julia IO Tutorial
last modified November 18, 2026
Input and output in Julia is built around streams: objects that can
produce or consume bytes. A stream may be connected to a file, to the
terminal, or to a region of memory. Because all of them support the same
read and write operations, the same functions work
with every kind of stream.
This tutorial introduces streams, the in-memory IOBuffer, the
standard streams, the different ways of printing values, formatted output with
Printf, and custom show methods. The examples were
verified with Julia 1.13.4. The AbstractIO interface is described
briefly in Julia interfaces.
Streams and IOBuffer
An IOStream represents an open file, while an IOBuffer
is a stream backed by memory. Writing to an IOBuffer and reading
the result back makes output easy to inspect without touching the filesystem
or depending on a terminal.
io = IOBuffer() print(io, "hello") print(io, " world") println(String(take!(io)))
The print function writes its arguments to the stream given as the
first argument. The take! function returns all bytes accumulated
so far and empties the buffer; converting them to a String gives
the text that was written. Note that print does not add a newline.
Working with a buffer in this way keeps the examples deterministic.
$ julia main.jl hello world
print and println
When no stream is supplied, print and println write to
the default output stream. The only difference between them is that
println appends a newline after its arguments.
io = IOBuffer() println(io, "hello") println(io, "world") print(String(take!(io)))
Two calls to println produce two lines, with a newline after each
one. The final print displays the captured content exactly. Both
functions accept any number of arguments and concatenate their textual forms
without separators.
$ julia main.jl hello world
show and print
The show function writes a representation that is meant to be
unambiguous and often machine-readable, while print writes a
human-friendly form. For a string the difference is visible immediately.
io = IOBuffer() x = "hi" print(io, x) println(String(take!(io))) io = IOBuffer() show(io, x) println(String(take!(io)))
Printing the string produces the bare text hi, while showing it
produces "hi" with the quotes included. The quoted form makes the
value's type clear and could be pasted back into Julia as a literal. Numeric
and array values show similar differences in more complex cases.
$ julia main.jl hi "hi"
The repr Function
The repr function is a convenient wrapper around
show: instead of writing to a stream, it returns the
representation as a string.
println(repr("hi"))
println(repr([1, 2, 3]))
println(repr('a'))
Each call returns the same text that show would write. Strings are
quoted, arrays include their element separators, and characters keep their
single quotes. This is useful for logging and for building messages that must
display values precisely.
$ julia main.jl "hi" [1, 2, 3] 'a'
The display Function
The display function is designed for interactive use. It chooses a
rich representation for a value based on the available output formats, which
allows environments such as notebooks to show images, tables and other media.
display(42)
In a plain script there is no rich display available, so the number is shown as
text. The important distinction is that print produces a plain
string form, show produces a canonical representation on a stream,
and display selects the richest representation the current
environment can handle.
$ julia main.jl 42
stdin, stdout and stderr
Julia defines three standard streams. The stdin stream is the
default source for input functions, stdout is the default target
for print and println, and stderr is
intended for diagnostics and error messages. They are normally connected to
the terminal that launched the program.
Functions such as readline read from stdin when no
stream is given, and warnings and errors go to stderr. Because
these streams depend on how the program is launched, the examples in this
tutorial write to an IOBuffer instead, so their output is always
the same regardless of the terminal.
Writing Bytes with write
The write function operates on raw bytes rather than formatted
text. It returns the number of bytes written and works with strings, individual
bytes and arrays of bytes.
io = IOBuffer() write(io, "abc") write(io, UInt8(10)) println(String(take!(io)))
The three letters occupy three bytes, and UInt8(10) is the byte
value for a newline. Converting the buffer back to a string shows the three
characters followed by a line break, which is why an empty line appears in the
output.
$ julia main.jl abc
Formatted Output with Printf
The Printf standard library provides the @printf
macro for C-style formatted output. A format string contains literal text and
placeholders such as %s for strings, %d for integers
and %f for floating-point numbers.
using Printf io = IOBuffer() @printf(io, "%s = %d (%.2f)\n", "pi", 3, 3.14159) print(String(take!(io)))
The first argument after the format string is the stream to write to. The
placeholders are replaced in order: the string pi, the integer
3, and the floating-point value rounded to two decimal places. The
\n at the end of the format string supplies the newline.
$ julia main.jl pi = 3 (3.14)
Aligning Columns
Width and precision fields in a format string make it easy to produce aligned columns. A number before the decimal point sets the minimum field width, and a minus sign can be used for left alignment.
using Printf io = IOBuffer() @printf(io, "%5s | %5d | %8.3f\n", "x", 42, 3.14159) @printf(io, "%5s | %5d | %8.3f\n", "y", 7, 2.5) print(String(take!(io)))
Each placeholder reserves a fixed width, so the two rows line up even though the values have different lengths. The floating-point field reserves eight characters and shows three digits after the decimal point.
$ julia main.jl
x | 42 | 3.142
y | 7 | 2.500
Custom show Methods
You can control how your own types are displayed by extending
Base.show. This is how Julia's own types produce their readable
printed forms, and it makes custom values display naturally inside arrays and
other containers.
struct Point
x::Int
y::Int
end
Base.show(io::IO, p::Point) = print(io, "Point(", p.x, ", ", p.y, ")")
io = IOBuffer()
show(io, Point(3, 4))
println(String(take!(io)))
The new method writes a compact representation of a Point to the
stream it receives. Any code that shows a Point now uses this
method, which is the recommended way to define custom output. Composite types
are explained in Julia structs.
$ julia main.jl Point(3, 4)
Reading from a Buffer
An IOBuffer can be created from a string and then read like an
open file. The read function returns a requested number of bytes,
and readline returns the next line.
io = IOBuffer("hello world")
s = read(io, 5)
println(String(s))
println(readline(io))
Reading five bytes yields "hello". The stream position now points
at the following space, so readline returns the rest of the text
starting there, including the leading space. This is the same behaviour you
would get from a file containing that text.
$ julia main.jl hello world
Reading Line by Line
The eachline function iterates over the lines of a stream, which
is convenient for processing text one line at a time without loading
everything at once.
io = IOBuffer("a\nb\nc\n")
for line in eachline(io)
println(line)
end
Each line is delivered without its trailing newline, and the loop prints it
with println, restoring one newline per line. The buffer contains
three lines, so three lines appear in the output.
$ julia main.jl a b c
Best Practices for IO
A few rules keep input and output code clear and portable.
- Capture output in a buffer when testing: an
IOBuffermakes results deterministic and avoids terminal dependencies. - Choose the right printing function: use
printfor plain text,showfor a canonical form, anddisplayfor rich interactive output. - Use
@printffor fixed formats: formatting placeholders are clearer than manual string concatenation. - Define
showfor your types: this gives useful output everywhere a value is displayed.
Source
Julia Networking and Streams documentation
Julia I/O and Network documentation
In this article we have explored Julia input and output: streams and buffers,
the standard streams, print versus show versus
display, formatted output with Printf, and custom
show methods. File handling is covered in
Julia files, and the underlying interface in
Julia interfaces.
Author
List all Julia tutorials.