> For the complete documentation index, see [llms.txt](https://syl1.gitbook.io/julia-language-a-concise-tutorial/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://syl1.gitbook.io/julia-language-a-concise-tutorial/language-core/input-output.md).

# 6 - Input - Output

## File reading/writing

File reading/writing is similar to other languages where you first `open` the file, specify the modality (`r` read, `w` write or `a` append) and bind the file to an object, and finally operate on this object and `close()` it when you are done.

A better alternative is however to encapsulate the file operations in a `do` block that closes the file automatically when the block ends:

Write:

```julia
open("afile.txt", "w") do f  # "w" for writing
  write(f, "test\n")         # \n for newline
end
```

Read the whole file in a single operation:

```julia
open("afile.txt", "r") do f   # "r" for reading
  filecontent = read(f,String) # attention that it can be used only once. The second time, without reopening the file, read() would return an empty string
  print(filecontent)
end
```

or, reading line by line:

```julia
open("afile.txt", "r") do f
  for ln in eachline(f)
    println(ln)
  end
end
```

or, if you want to keep track of the line numbers:

```julia
open("afile.txt", "r") do f
   for (i,ln) in enumerate(eachline(f))
     println("$i $ln")
   end
end
```

## Other IO

Some packages that deals with IO are:

* CSV: [CSV.jl](https://github.com/JuliaData/CSV.jl)
* Web stream: [HTTP.jl](https://github.com/JuliaWeb/HTTP.jl)
* Spreadsheets (OpenDocument): [OdsIO.jl](https://github.com/sylvaticus/OdsIO.jl)
* HDF5: [HDF5.jl](https://github.com/JuliaIO/HDF5.jl)

Some basic examples that use them are available in the [DataFrame](/julia-language-a-concise-tutorial/useful-packages/dataframes.md) section.

*While an updated, expanded and revised version of this chapter is available in "Chapter 5 - Input/Output" of* [*Antonello Lobianco (2019), "Julia Quick Syntax Reference", Apress*](https://julia-book.com)*, this tutorial remains in active development.*


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://syl1.gitbook.io/julia-language-a-concise-tutorial/language-core/input-output.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
