Last Updated: January 3, 2026
Understanding how to handle input and output (I/O) in Java is fundamental for any developer. Whether you're building console applications, working with files, or interacting with users, mastering I/O can significantly enhance your programs' functionality.
In this chapter, we'll dive into the different techniques for reading from and writing to various sources, while also discussing some common pitfalls and best practices.
When we talk about I/O in Java, we're essentially referring to how our program communicates with the outside world. This can involve reading data entered by users, writing output to the console, or handling files on the disk.
The core of I/O in Java revolves around the java.io package, which provides a rich set of classes and methods for reading and writing data. At its simplest, I/O operations can be divided into two categories: input and output.
For console applications, standard input (stdin) and output (stdout) are your primary channels. You typically use System.in for input and System.out for output.
In this example, we use Scanner to read user input. The nextLine() method captures the entire line entered by the user until they hit Enter.
Using Scanner provides a simple and efficient way to parse primitive types and strings. It handles various input formats, such as integers, doubles, and even strings. However, it's essential to remember to close the Scanner once you’re done to free resources.
While Scanner is great for basic input, there are situations where you might want to read from the console differently. For example, you might need to handle input more manually or read multiple values at once. Let's explore a couple of these scenarios.
If you need to read lines of text efficiently, BufferedReader is an excellent choice. It buffers the input, making it faster for reading large amounts of data.
Notice how we've wrapped our input reading in a try-catch block. This is crucial for dealing with potential exceptions, such as IOException when there's a problem reading input or NumberFormatException if the input is not a valid integer.
Writing output in Java is straightforward, thanks to System.out. However, there are techniques and nuances that can enhance your output formatting, making it more user-friendly.
Java's PrintStream class, which System.out is an instance of, provides several methods for printing formatted text. For example, you can use printf for formatted output.
While console I/O is essential, working with files is where Java's I/O capabilities really shine. Let's look at how to read from and write to files using FileReader, FileWriter, and BufferedReader.
To create and write to a file, you can use FileWriter along with BufferedWriter for efficiency.
Similarly, reading from a file can be done using FileReader and BufferedReader.
Understanding how to perform I/O operations opens up a world of possibilities. Here are some practical applications where I/O is essential:
Imagine creating a program that requires user configuration. By reading from the console or a configuration file, you can customize how your application runs.
Saving user data to files is crucial for applications that need to remember state between sessions. Whether it's user preferences or game saves, file I/O is the key.
Implementing logging in your applications allows you to write important runtime information to a file, which is invaluable for debugging.
In the next chapter, we will look at how to effectively document your code, which can help you and others understand your work better.