Let’s create a simple Java project that demonstrates file operations, including creating a file, writing to it, reading from it, and finally deleting the file. This project could represent a simple note-taking application where users can add notes to a file, read their notes back, and choose to delete the notes file when they are done.
To organize the simple note-taking application more effectively, we can separate each task (create, write, read, delete) into different classes. This approach improves the project’s structure and makes the code more modular and maintainable. Finally, we’ll combine all these operations in the main class.
Step 1: FileCreator.java
Handles file creation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.io.File; import java.io.IOException; public class FileCreator { public static void createFile(String filename) { try { File file = new File(filename); if (file.createNewFile()) { System.out.println("File created: " + file.getName()); } else { System.out.println("File already exists."); } } catch (IOException e) { System.out.println("An error occurred while creating the file."); e.printStackTrace(); } } } |
Step 2: FileWriterUtil.java
Manages writing content to the file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import java.io.FileWriter; import java.io.IOException; public class FileWriterUtil { public static void writeToFile(String filename, String content) { try (FileWriter writer = new FileWriter(filename, true)) { writer.write(content + "\n"); System.out.println("Successfully wrote to the file."); } catch (IOException e) { System.out.println("An error occurred while writing to the file."); e.printStackTrace(); } } } |
Step 3: FileReaderUtil.java
Facilitates reading from the file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.io.File; import java.io.IOException; import java.util.Scanner; public class FileReaderUtil { public static void readFile(String filename) { try (Scanner scanner = new Scanner(new File(filename))) { System.out.println("Reading file contents:"); while (scanner.hasNextLine()) { System.out.println(scanner.nextLine()); } } catch (IOException e) { System.out.println("An error occurred while reading the file."); e.printStackTrace(); } } } |
Step 4: FileDeleter.java
Handles file deletion.
1 2 3 4 5 6 7 8 9 10 11 12 |
import java.io.File; public class FileDeleter { public static void deleteFile(String filename) { File file = new File(filename); if (file.delete()) { System.out.println("Deleted the file: " + file.getName()); } else { System.out.println("Failed to delete the file."); } } } |
Main Class: SimpleNoteApp
Combines all operations.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
import java.util.Scanner; public class SimpleNoteApp { private static final String FILENAME = "notes.txt"; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); FileCreator.createFile(FILENAME); System.out.println("Enter a note (type 'exit' to stop):"); String input = scanner.nextLine(); while (!input.equalsIgnoreCase("exit")) { FileWriterUtil.writeToFile(FILENAME, input); System.out.println("Enter another note (or 'exit' to finish):"); input = scanner.nextLine(); } FileReaderUtil.readFile(FILENAME); System.out.println("Do you want to delete the notes file? (yes/no)"); input = scanner.nextLine(); if (input.equalsIgnoreCase("yes")) { FileDeleter.deleteFile(FILENAME); } else { System.out.println("Your notes are saved in " + FILENAME); } scanner.close(); } } |
This simple note-taking application demonstrates the core file operations in Java. It allows the user to add notes, read all notes back, and decide whether to delete the notes file at the end of the session, showcasing practical usage of creating, writing to, reading from, and deleting files.
The note-taking application divides responsibilities into separate classes, each handling a specific aspect of file management. This modular approach not only makes the code cleaner and more organized but also demonstrates a basic principle of object-oriented programming: separation of concerns.