








Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!









import java.io.FileWriter; import java.io.IOException; public class FileWriteExample { public static void main(String[] args) { try { FileWriter writer = new FileWriter("output.txt"); writer.write("Hello, this is a file writing example in Java!"); writer.close(); System.out.println("File written successfully."); } catch (IOException e) { System.out.println("Error writing to file: " + e.getMessage()); } } }
import java.io.FileReader; import java.io.BufferedReader; import java.io.IOException; public class FileReadExample { public static void main(String[] args) { try { BufferedReader reader = new BufferedReader(new FileReader("output.txt")); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } reader.close(); } catch (IOException e) { System.out.println("Error reading from file: " + e.getMessage()); }
import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; public class MultiLineFileWrite { public static void main(String[] args) { try { BufferedWriter writer = new BufferedWriter(new FileWriter("multi_output.txt")); writer.write("Line 1: Java File Handling"); writer.newLine(); writer.write("Line 2: Writing multiple lines."); writer.newLine(); writer.close(); System.out.println("File written successfully."); } catch (IOException e) { System.out.println("Error writing to file: " + e.getMessage()); } } }
import java.io.File; public class FileCheckExample { public static void main(String[] args) { File file = new File("output.txt"); if (file.exists()) { System.out.println("File exists."); } else { System.out.println("File does not exist."); } } }
An application program using file input and output allows for persistent data storage. Here’s an example: a simple Student Management System that reads and writes student records to a file. Objective: Create a Java application that:
Implementation:
Updating a student record in a file involves reading the file, modifying the necessary data, and writing it back. Here’s a Java program to achieve this: How It Works:
} catch (IOException e) { System.out.println("Error processing the file: " + e.getMessage()); } scanner.close(); } } How to Use:
A menu-driven program using file I/O allows users to interact with stored data through different options. Here’s how we can structure a Student Management System with a menu, enabling users to add, view, search, update, and delete student records. How It Works:
found = true; break; } } if (!found) { System.out.println("Student record not found."); } } catch (IOException e) { System.out.println("Error reading file: " + e.getMessage()); } } // Update a student record static void updateStudent(Scanner scanner) { System.out.print("Enter student name to update: "); String searchName = scanner.nextLine().trim(); List studentRecords = new ArrayList<>(); boolean updated = false; try (BufferedReader reader = new BufferedReader(new FileReader(FILE_NAME))) { String line; while ((line = reader.readLine()) != null) { String[] data = line.split(","); if (data[0].equalsIgnoreCase(searchName)) { System.out.print("Enter new age: "); String newAge = scanner.nextLine(); System.out.print("Enter new grade: "); String newGrade = scanner.nextLine(); line = data[0] + "," + newAge + "," + newGrade; updated = true; } studentRecords.add(line); } } catch (IOException e) { System.out.println("Error processing the file: " + e.getMessage()); } if (updated) { try (BufferedWriter writer = new BufferedWriter(new FileWriter(FILE_NAME))) { for (String record : studentRecords) { writer.write(record); writer.newLine(); } System.out.println("Student record updated successfully."); } catch (IOException e) { System.out.println("Error writing file: " + e.getMessage()); } } else { System.out.println("Student record not found."); } } // Delete a student record
static void deleteStudent(Scanner scanner) { System.out.print("Enter student name to delete: "); String searchName = scanner.nextLine().trim(); List studentRecords = new ArrayList<>(); boolean deleted = false; try (BufferedReader reader = new BufferedReader(new FileReader(FILE_NAME))) { String line; while ((line = reader.readLine()) != null) { String[] data = line.split(","); if (data[0].equalsIgnoreCase(searchName)) { deleted = true; continue; } studentRecords.add(line); } } catch (IOException e) { System.out.println("Error processing the file: " + e.getMessage()); } if (deleted) { try (BufferedWriter writer = new BufferedWriter(new FileWriter(FILE_NAME))) { for (String record : studentRecords) { writer.write(record); writer.newLine(); } System.out.println("Student record deleted successfully."); } catch (IOException e) { System.out.println("Error writing file: " + e.getMessage()); } } else { System.out.println("Student record not found."); } } }