File I/O (Input/Output) allows your Java programs to read data from files and write data to files. This is essential for creating programs that can save and load information, process data files, or generate reports.
Most real-world programs need to:
-
Save user data between program runs
-
Read configuration settings
-
Process data from spreadsheets or databases
-
Generate reports or logs
-
Import/export information
The easiest way to read text files is using the Scanner class we already learned about:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ZipCode {
void compute() {
try {
File file = new File("data.txt");
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
System.out.println(line);
}
scanner.close();
} catch (FileNotFoundException e) {
System.out.println("File not found: " + e.getMessage());
}
}
public static void main(String[] args) { new ZipCode().compute(); }
}import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ZipCode {
void compute() {
try {
File file = new File("scores.txt");
Scanner scanner = new Scanner(file);
while (scanner.hasNext()) {
String name = scanner.next(); // Read name
int score = scanner.nextInt(); // Read score
System.out.println(name + ": " + score);
}
scanner.close();
} catch (FileNotFoundException e) {
System.out.println("Could not find the file!");
}
}
public static void main(String[] args) { new ZipCode().compute(); }
}import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.IOException;
public class ZipCode {
void compute() {
try {
PrintWriter writer = new PrintWriter(new FileWriter("output.txt"));
writer.println("Hello, World!");
writer.println("This is line 2");
writer.print("This is on the same line ");
writer.println("as this text.");
writer.close(); // Important: Always close the writer!
System.out.println("File written successfully!");
} catch (IOException e) {
System.out.println("Error writing to file: " + e.getMessage());
}
}
public static void main(String[] args) { new ZipCode().compute(); }
}import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.IOException;
public class ZipCode {
void compute() {
try {
PrintWriter writer = new PrintWriter(new FileWriter("students.txt"));
// Write header
writer.println("Student Grade Report");
writer.println("====================");
writer.println();
// Write student data
String[] names = {"Alice", "Bob", "Charlie"};
int[] grades = {85, 92, 78};
for (int i = 0; i < names.length; i++) {
writer.printf("%-10s: %3d%%\n", names[i], grades[i]);
}
writer.close();
System.out.println("Report saved to students.txt");
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
}
}
public static void main(String[] args) { new ZipCode().compute(); }
}File operations can fail for many reasons (file doesn’t exist, no permission, disk full, etc.), so we must handle exceptions:
import java.io.*;
import java.util.Scanner;
public class ZipCode {
void compute() {
String filename = "data.txt";
try {
// Attempt to read the file
Scanner scanner = new Scanner(new File(filename));
while (scanner.hasNextLine()) {
System.out.println(scanner.nextLine());
}
scanner.close();
} catch (FileNotFoundException e) {
System.out.println("Sorry, could not find file: " + filename);
} catch (Exception e) {
System.out.println("An unexpected error occurred: " + e.getMessage());
}
}
public static void main(String[] args) { new ZipCode().compute(); }
}Java provides a cleaner way to handle file resources with automatic cleanup:
import java.io.*;
import java.util.Scanner;
public class ZipCode {
void compute() {
// Try-with-resources automatically closes the Scanner
try (Scanner scanner = new Scanner(new File("data.txt"))) {
while (scanner.hasNextLine()) {
System.out.println(scanner.nextLine());
}
// scanner.close() is called automatically!
} catch (FileNotFoundException e) {
System.out.println("File not found: " + e.getMessage());
}
}
public static void main(String[] args) { new ZipCode().compute(); }
}Here’s a practical example that reads student data from a file, processes it, and writes a report:
import java.io.*;
import java.util.Scanner;
public class ZipCode {
void compute() {
readAndProcessGrades();
}
public static void readAndProcessGrades() {
// Read grades from input file
try (Scanner input = new Scanner(new File("grades_input.txt"));
PrintWriter output = new PrintWriter(new FileWriter("grade_report.txt"))) {
output.println("GRADE REPORT");
output.println("============");
output.println();
double totalGrades = 0;
int studentCount = 0;
while (input.hasNextLine()) {
String line = input.nextLine();
String[] parts = line.split(","); // Split line at comma: "Name,Grade" becomes ["Name", "Grade"]
if (parts.length == 2) {
String name = parts[0].trim();
int grade = Integer.parseInt(parts[1].trim());
// Write student info
output.printf("%-15s: %3d%% ", name, grade);
// Add letter grade
if (grade >= 90) {
output.println("(A)");
} else if (grade >= 80) {
output.println("(B)");
} else if (grade >= 70) {
output.println("(C)");
} else if (grade >= 60) {
output.println("(D)");
} else {
output.println("(F)");
}
totalGrades += grade;
studentCount++;
}
}
// Calculate and write average
if (studentCount > 0) {
double average = totalGrades / studentCount;
output.println();
output.printf("Class Average: %.1f%%\n", average);
}
System.out.println("Grade report generated successfully!");
} catch (FileNotFoundException e) {
System.out.println("Input file not found: " + e.getMessage());
} catch (IOException e) {
System.out.println("Error writing report: " + e.getMessage());
} catch (NumberFormatException e) {
System.out.println("Error: Invalid grade format in file");
}
}
public static void main(String[] args) { new ZipCode().compute(); }
}// Relative path (relative to where your program runs)
File file1 = new File("data.txt"); // In current directory
File file2 = new File("data/students.txt"); // In data subfolder
// Absolute path (complete path from root)
File file3 = new File("/Users/student/documents/grades.txt"); // Mac/Linux
File file4 = new File("C:\\Users\\student\\Documents\\grades.txt"); // Windowsimport java.io.File;
public class ZipCode {
void compute() {
File file = new File("data.txt");
if (file.exists()) {
System.out.println("File exists!");
System.out.println("File size: " + file.length() + " bytes");
System.out.println("Can read: " + file.canRead());
System.out.println("Can write: " + file.canWrite());
} else {
System.out.println("File does not exist.");
}
}
public static void main(String[] args) { new ZipCode().compute(); }
}import java.io.*;
import java.util.*;
public static List<String> readAllLines(String filename) {
List<String> lines = new ArrayList<>();
try (Scanner scanner = new Scanner(new File(filename))) {
while (scanner.hasNextLine()) {
lines.add(scanner.nextLine());
}
} catch (FileNotFoundException e) {
System.out.println("File not found: " + filename);
}
return lines;
}import java.io.*;
public static void appendToFile(String filename, String text) {
try (PrintWriter writer = new PrintWriter(new FileWriter(filename, true))) {
writer.println(text); // true parameter means "append mode"
} catch (IOException e) {
System.out.println("Error appending to file: " + e.getMessage());
}
}-
Always use try-catch blocks - File operations can fail
-
Close your resources - Use try-with-resources when possible
-
Check if files exist before trying to read them
-
Use meaningful error messages to help debug problems
-
Handle different types of exceptions appropriately
-
Use absolute paths when you need to be sure of file location
-
Validate file data before processing it
| Exception | When it Occurs |
|---|---|
|
When trying to read a file that doesn’t exist |
|
General I/O errors (permission denied, disk full, etc.) |
|
When your program doesn’t have permission to access the file |
|
When parsing text as numbers fails |
Exercise 1: File Copy Program Create a program that reads a text file and writes an exact copy to a new file:
// Try implementing this:
// 1. Read each line from "input.txt"
// 2. Write each line to "copy.txt"
// 3. Handle FileNotFoundExceptionExercise 2: Word Counter Count the number of words in a text file:
// Hint: Use scanner.next() in a loop
// Count how many times you can read a word
// Print the total countExercise 3: Simple Data File Read a file with names and ages, then create a report:
// Input file format:
// Alice 25
// Bob 30
// Carol 28
// Your program should:
// 1. Read each name and age
// 2. Calculate average age
// 3. Write a summary reportFile I/O is a fundamental skill that opens up many possibilities for your Java programs. Master these basics and you’ll be able to create programs that work with real-world data!