IO Streams (File IO)
In Java, programs often need to interact with files and external data sources. A program may need to read data from a file, write reports into a file, copy images, save logs, store user input, or load configuration values. All of these tasks come under Input and Output, commonly called I/O.
Java provides a rich I/O API to handle such operations using streams. Understanding streams and file I/O is essential because almost every real-world application works with external data in some form.
What is I/O in Java?
I/O stands for Input and Output.
- Input means taking data into the program
- Output means sending data out of the program
Examples:
- reading text from a file
- writing content into a file
- reading keyboard input
- sending output to console or printer
What is a Stream?
A stream is a flow of data between a program and a data source or destination.
In simple words:
- if data is coming into the program → input stream
- if data is going out from the program → output stream
Java treats files, keyboard, network, and memory data as streams.
Why Streams are Used
- to provide a uniform way to handle data input and output
- to support files, console, network, and memory operations
- to make data reading and writing structured and reusable
Java I/O Packages
The main package for traditional I/O in Java is:
Some file-related and modern I/O utilities are also found in:
In this tutorial, the main focus is on the traditional I/O stream model from java.io.
Types of Streams in Java
Streams in Java are broadly divided into:
- Byte Streams
- Character Streams
1. Byte Streams
Byte streams are used to read and write raw binary data. They are suitable for files like:
- images
- audio files
- videos
- PDFs
- binary data
Main parent classes:
InputStreamOutputStream
2. Character Streams
Character streams are used to read and write text data. They are suitable for:
- text files
- source code files
- configuration files
- CSV and log files
Main parent classes:
ReaderWriter
Difference Between Byte Streams and Character Streams
| Point | Byte Streams | Character Streams |
|---|---|---|
| Data type | Raw bytes | Characters / text |
| Use case | Binary files | Text files |
| Main classes | InputStream / OutputStream | Reader / Writer |
File I/O in Java
File I/O means reading data from a file and writing data into a file.
Java provides many classes for file handling, such as:
FileFileInputStreamFileOutputStreamFileReaderFileWriterBufferedReaderBufferedWriterPrintWriter
File Class
The File class is used to represent a file or directory path. It does not itself read or write content, but it helps in checking, creating, or managing file-related information.
Example
Creating a File
The createNewFile() method can be used to create a new file.
Reading File Using FileInputStream
FileInputStream is a byte stream class used to read binary data from a file.
Example
Here:
read()returns one byte at a time-1means end of file
Writing File Using FileOutputStream
FileOutputStream is used to write binary data into a file.
Example
Reading Text File Using FileReader
FileReader is a character stream class used for reading text files.
Writing Text File Using FileWriter
FileWriter is a character stream class used to write text data into files.
Appending Data to File
By default, FileWriter overwrites file content. To append data, pass true as the second argument.
Buffered Streams
Buffered streams improve performance by reducing the number of direct disk access operations. They read and write data in larger chunks.
Common buffered classes:
BufferedInputStreamBufferedOutputStreamBufferedReaderBufferedWriter
Reading File Using BufferedReader
BufferedReader is commonly used to read text line by line.
This is more efficient and convenient than reading one character at a time.
Writing File Using BufferedWriter
Using PrintWriter
PrintWriter is useful for writing formatted text data to a file.
Copying a File
A common practical use of file I/O is copying content from one file to another.
try-with-resources in File I/O
Java provides try-with-resources to automatically close resources like streams and readers.
This is one of the best practices in file handling.
The file is automatically closed after the block ends.
IOException
File operations may fail due to reasons such as:
- file not found
- permission denied
- invalid path
- disk issues
These are represented through checked exceptions such as IOException.
Example
File Path Types
Java can work with two types of file paths:
- relative path
- absolute path
Relative Path
Absolute Path
Relative paths depend on the current working directory, while absolute paths point to the exact location.
Character Encoding Note
Text files depend on character encoding. Traditional classes like FileReader use the default platform encoding. In advanced applications, developers often prefer more explicit control over encoding.
For basic learning, FileReader and FileWriter are enough, but in professional projects, encoding awareness is very important.
Real-World File I/O Examples
1. Saving User Registration Logs
2. Reading Configuration File
3. Copying Report File
Common Mistakes
- forgetting to close streams
- using byte streams for text when character streams are more suitable
- not handling file exceptions properly
- confusing relative and absolute file paths
- overwriting file content when append mode was intended
Best Practices
- use character streams for text data
- use byte streams for binary data
- prefer buffered streams for efficiency
- use try-with-resources for automatic cleanup
- handle exceptions meaningfully
- avoid hardcoding paths when possible
Interview-Oriented Points
- I/O stands for Input and Output
- Streams are flows of data between program and source/destination
- Byte streams handle binary data, character streams handle text
FileInputStreamandFileOutputStreamare byte stream classesFileReaderandFileWriterare character stream classesBufferedReaderreads text line by line efficientlytry-with-resourcesautomatically closes streams- File-related exceptions are generally checked exceptions
Conclusion
I/O streams and file handling are core parts of Java programming because almost every real application works with external data. Java provides a flexible and powerful stream-based model for reading and writing both text and binary files.
Understanding when to use byte streams, character streams, buffered streams, and resource management practices is essential for writing correct and efficient Java programs.

