Skip to main content
Course/Module 10/Topic 3 of 4Advanced

File I/O & Data Persistence

Add data persistence to the banking app — save accounts and transactions to files, load on startup, and implement proper error handling.

50 minBy Priygop TeamLast updated: Feb 2026

File Operations

  • Save to CSV: try (PrintWriter writer = new PrintWriter(new FileWriter('accounts.csv'))) { for (BankAccount acc : accounts.values()) { writer.printf('%s,%s,%s,%.2f%n', acc.getNumber(), acc.getOwner(), acc.getType(), acc.getBalance()); } } — try-with-resources auto-closes
  • Load from CSV: try (BufferedReader reader = new BufferedReader(new FileReader('accounts.csv'))) { String line; while ((line = reader.readLine()) != null) { String[] parts = line.split(','); createAccount(parts[0], parts[1], parts[2], Double.parseDouble(parts[3])); } }
  • Transaction Log: Append each transaction to transactions.log — try (FileWriter fw = new FileWriter('transactions.log', true)) — second parameter 'true' enables append mode. Never overwrites history
  • Error Handling: catch (FileNotFoundException e) { System.out.println('No saved data found, starting fresh.'); } catch (IOException e) { System.err.println('Error reading file: ' + e.getMessage()); } — different handling per exception type
  • Data Integrity: Always save after each transaction completes successfully — if deposit succeeds but save fails, log the error but don't rollback the in-memory transaction. Inform user of save failure
  • File Path: Use relative paths for portability — Path.of('data', 'accounts.csv'). Create directory if needed: Files.createDirectories(Path.of('data')). Works across Windows, Mac, Linux

Console Menu System

  • Main Menu: while (true) { displayMenu(); int choice = scanner.nextInt(); switch (choice) { case 1 -> createAccount(); case 2 -> deposit(); ... case 0 -> { saveAll(); return; } } } — infinite loop with exit option
  • Input Validation: Use try-catch around scanner.nextDouble() for amount input — catch InputMismatchException when user types letters instead of numbers. Call scanner.nextLine() to clear invalid input
  • Scanner Best Practice: Create one Scanner(System.in) instance and pass it around — never create multiple Scanners on System.in. Close scanner only when program exits to avoid closing System.in
  • Menu Display: System.out.println('\n╔══════════════════════════╗'); System.out.println('║ BANK MANAGEMENT SYSTEM ║'); — formatted menu with box-drawing characters for professional appearance
  • Input Prompts: System.out.print('Enter amount: $'); double amount = scanner.nextDouble(); — use print (not println) for inline prompts. Consistent formatting with currency symbols
  • Graceful Exit: Runtime.getRuntime().addShutdownHook(new Thread(() -> saveAll())) — save data even if user presses Ctrl+C. Shutdown hooks run before JVM exits for cleanup operations
Chat on WhatsApp
Priygop - Leading Professional Development Platform | Expert Courses & Interview Prep