CSE215L: Programming Language II Lab
Faculty: Dr. Mohammad Shifat E Rabbi (MSRb)
Lab Instructor: Kazi Tanvir Akter
Lab 16: File I/O
Objective:
• To understand File Input Output in java
• To learn the uses of FileWriter, FileReader, BufferedWriter, BufferedReader for file
handling
• To learn the uses of different files in Java
File Input
We need total 3 things for the file input operation:
1. File path and name
2. FileWriter Object (it will take the file path inside the parameter of constructor invocation)
3. BufferedWriter (it will take the FileWriter object inside the parameter of constructor
invocation)
Now, to write on a file, we need to use the BufferedWriter objects write method. The FileWriter and
BufferedWriter objects must be created inside a try-catch block to ignore any IOException.
Finally, we need to close the BufferedWriter object to terminate the file writing.
String filePath = "C:\\Users\\Lenovo\\Desktop\\output.txt";
String[] languages = {"java", "c++", "python"};
try {
FileWriter fw = new FileWriter(filePath);
BufferedWriter writer = new BufferedWriter(fw);
writer.write("Hello File!");
writer.write("\Another File!");
System.out.println("Writing done!");
//to write array elements
for(String language:languages){
writer.write("\n"+language);
}
writer.close();
} catch (IOException ex) {
System.out.println(ex);
}
File Output
As like the File Input operation, we need total 3 things for the file output operation:
1. File path and name
2. FileReader Object (it will take the file path inside the parameter of constructor invocation)
3. BufferedReader (it will take the FileReader object inside the parameter of constructor
invocation)
Now, to write from a file, we need to use the BufferedReader objects readLine method. This method
can read file in a line-by-line manner. So we need to use a loop to run the reading task up to the end line
of a file. The FileReader and BufferedReader objects must be created inside a try-catch block to ignore
any IOException.
Finally, we need to close the BufferedReader object to terminate the file reading.
String filePath = "C:\\Users\\Lenovo\\Desktop\\output.txt";
String[] languages = {"java", "c++", "python"};
try {
FileReader fr = new FileReader(fileName);
BufferedReader reader = new BufferedReader(fr);
String line;
System.out.println("Reading from file:");
while((line = reader.readLine())!=null){
System.out.println(line);
}
reader.close();
//to write array elements
for(String language:languages){
writer.write("\n"+language);
}
writer.close();
} catch (IOException ex) {
System.out.println(ex);
}
More File Operations:
1. CSV file (https://www.youtube.com/watch?v=zKDmzKaAQro)
2. Image File (https://www.youtube.com/watch?v=lGX0Gc6d51s)
Tasks
1. Write a program that takes integers from user and writes them into a file until user inputs a
negative number. The program should then read the file and print sum and average of the
numbers.
2. Create a Quiz class with ID and marks. Now write a program that reads a file containing
records of Quiz objects and initialize an array. The program should then print all the objects in
the Quiz array and print the id of the student who obtained the highest mark.
Sample File:
113098 20
115089 15
345678 12
234566 18
Program Output:
ID:113098
mark:20
ID:115089
mark:15
ID:345678
mark:12
ID:234566
mark:18
Highest mark obtained by ID:113098