Contents [hide]
Aim
To write a program for demonstrating file IO using Java.
Theory
Almost all programs in Java require some input which is processed by the program and generates some output. Java provides an API in the form of packages namely java.io, and java.nio for dealing with I/O.
Both these packages contain various classes which can be used to read and write data to various sources like a console, file, peripheral device, or a socket.
Java handles transfer of data in the form of streams. A stream is a flow of data which is composed of bytes. So, the data is transferred continuously as a flow. Java creates three streams automatically which are attached to the console. They are:
- out: Standard output stream
- in: Standard input stream
- err: Standard error stream
Reading data from a file
There are different ways for reading data from a text file which can be used for different types of applications. The multiple ways for reading data from a text file include using BufferedReader, FileReader, Scanner, etc.
Using BufferedReader class
This way of reading text from a file reads it as character input stream. It can buffer the text for efficient reading of lines, arrays, and characters. The buffer size can be left as default or can be changed base on our need.
Let’s see a program which uses BufferedReader class for reading and displaying the text in a file. In the below example, the text file name is “sample.txt”.
Writing data to a file
There are different ways for reading data from a text file which can be used for different types of applications. The multiple ways for reading data from a text file include using BufferedWriter, FileWriter, FileOutputStream, etc.
Using BufferedWriter class
This way of writing text to a file writes it as a character output stream. It can buffer the text for efficient writing of lines, arrays, and characters. The buffer size can be left as default or can be changed base on our need.
Let’s see a program which uses BufferedWriter class for writing data to a file. In the below example, the text file name is “output.txt”.
Pseudo code / Steps
Program 1
- Create a class to read the text from a file named “data.txt” and display the text on the screen.
- Use StringTokenizer class available in util package to find out number of words in the text read from the file in previous step.
- Display the count of words on the screen.
Program 2
- Create a class for storing the text into a file named “details.txt”.
- Create a class Student with appropriate variables and constructor.
- Read the student details of 5 students like name, age, and branch from keyboard and store this in the file on a single line by separating the details using commas.
- Finally, after storing the student details in the text file, display a message “data stored successfully” on the screen.
Programs
Program 1
Input and Output for program 1:
Program 2
Input and Output for program 2:
Contents of the file details.txt
Result
The concepts related to file I/O were understood and implemented successfully.

Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.
He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.
He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.
Leave a Reply