Introduction
In this tutorial, we will create a simple Java-based diary application that allows users to write and save daily diary entries.
This application will enable the user to input their thoughts or experiences for the day, save those entries in a text file,
and later retrieve them whenever needed. This application can be extended with additional features, but for now, we will focus
on the basic functionalities.
Objective
The primary goal is to create a console-based diary application using Java. Users will be able to input a diary entry,
choose to save it, and later view all their entries by reading from a text file. This application will help you understand file
handling and basic Java programming concepts.
Java Code
import java.io.*; import java.util.Scanner; public class DiaryApp { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String diaryEntry; File diaryFile = new File("diary_entries.txt"); // Display menu System.out.println("Welcome to your Diary Application!"); System.out.println("Please select an option:"); System.out.println("1. Write a new entry"); System.out.println("2. View all entries"); System.out.println("3. Exit"); int choice = scanner.nextInt(); scanner.nextLine(); // consume the newline character switch (choice) { case 1: // Write a new diary entry System.out.println("Enter your diary entry for today:"); diaryEntry = scanner.nextLine(); saveEntry(diaryFile, diaryEntry); break; case 2: // View all diary entries viewEntries(diaryFile); break; case 3: // Exit the application System.out.println("Thank you for using the Diary Application. Goodbye!"); break; default: System.out.println("Invalid choice. Please try again."); } scanner.close(); } // Method to save the diary entry to a file private static void saveEntry(File file, String entry) { try (BufferedWriter writer = new BufferedWriter(new FileWriter(file, true))) { writer.write("Date: " + java.time.LocalDate.now() + "\n"); writer.write(entry + "\n"); writer.write("---------------------------------------------------\n"); System.out.println("Your entry has been saved successfully!"); } catch (IOException e) { System.out.println("An error occurred while saving your entry."); e.printStackTrace(); } } // Method to view all saved diary entries private static void viewEntries(File file) { if (file.exists()) { try (BufferedReader reader = new BufferedReader(new FileReader(file))) { String line; while ((line = reader.readLine()) != null) { System.out.println(line); } } catch (IOException e) { System.out.println("An error occurred while reading your entries."); e.printStackTrace(); } } else { System.out.println("No diary entries found."); } } }
Explanation of the Program Structure
The program is organized into the following key components:
- Scanner: This is used to read user input from the console.
- Diary File: The diary entries are stored in a text file called
diary_entries.txt
. - Switch Case: The switch statement presents the user with three choices: writing a new entry, viewing all entries, or exiting the application.
- Save Entry: The
saveEntry
method appends the new diary entry to the file, along with the current date and a separator line. - View Entries: The
viewEntries
method reads and displays all saved diary entries from the text file.
How to Run the Program
To run the diary application, follow these steps:
- Make sure you have the latest version of Java installed on your machine.
- Create a new Java file and copy the code provided above into that file. Save it as
DiaryApp.java
. - Open your terminal or command prompt and navigate to the directory where
DiaryApp.java
is saved. - Compile the program by running the command:
javac DiaryApp.java
. - Run the compiled program using the command:
java DiaryApp
. - Follow the on-screen prompts to write or view diary entries.
When you write a new entry, it will be saved to a file named diary_entries.txt</>. You can view all entries by choosing
the "View all entries" option from the main menu.