File Handling in Java Insert, Update, Delete, Search, Sort and Display with collection in File CRUD

Share

Summary

This tutorial demonstrates how to perform CRUD (Create, Read, Update, Delete), Search, and Sort operations on a collection of employee data, which is then written to and read from a file in Java. The video covers implementing a menu-driven program for these functionalities.

Highlights

Introduction to CRUD Operations and Employee Class Setup
00:00:00

The video starts by introducing the goal: to perform CRUD operations (create, retrieve, update, delete, search, sort) on a collection and write that collection to a file. An `Employee` class is declared with an employee number, name, and salary, along with a parameterized constructor and an overridden `toString()` method for displaying employee details.

Setting Up the Menu-Driven Program
00:01:32

A `EmployeeDemo` class is created with a `main` method to build a menu-driven program. A `do-while` loop is used to present options for 'Insert into file', 'Display from file', and 'Exit'. The `Scanner` class is used to get user input for their choice.

Implementing Insert (Case 1)
00:03:48

Case 1 handles inserting data. The user is prompted for the number of employees they want to enter. The program then iteratively collects employee number, name, and salary. It's recommended to use separate `Scanner` instances for numbers and strings to avoid issues. An `ArrayList` is used to store `Employee` objects. After collecting all employee data, the `ArrayList` is written to a file using `ObjectOutputStream`.

Handling File I/O and Serialization
00:09:30

The video explains that directly initializing `ObjectOutputStream` within the loop would overwrite the file. Instead, the `ArrayList` is filled first, and then the entire collection is written to the file. It also highlights the need to import `java.io.*` and add `throws IOException` to the `main` method. Crucially, the `Employee` class must implement `Serializable` for its objects to be written to a file.

Reading Data from File and Displaying (Case 2)
00:15:11

To read data from a file, an `ObjectInputStream` is used. Before reading, the program checks if the 'employee.txt' file exists (`file.isFile()`). If it exists, the data is read from the file into the `ArrayList`. In Case 2, the `ArrayList` is loaded from the file (if it exists) and then iterated using a `ListIterator` to display each employee's details sequentially.

Implementing Search (Case 3)
00:20:47

Case 3 allows searching for an employee. Similar to display, the `ArrayList` is first loaded from the file. The user is prompted to enter an employee number to search. The program iterates through the `ArrayList` using `ListIterator`, and if an employee with a matching number is found, their details are displayed, and a 'record found' message is shown. If not found, a 'record not found' message is displayed. Proper error handling for a non-existent file is also implemented.

Implementing Delete (Case 4)
00:28:08

Case 4 handles deleting an employee. The `ArrayList` is loaded from the file, and the user enters the employee number to delete. If the employee is found during iteration, `ListIterator.remove()` is called to remove the employee from the collection. After deletion, the modified `ArrayList` is written back to the file to update the persistent data. A 'record deleted successfully' or 'record not found' message is provided.

Implementing Update (Case 5)
00:31:38

Case 5 allows updating employee details. The `ArrayList` is loaded from the file, and the user provides the employee number to update. If found, the user is prompted for the new employee name and salary. The `ListIterator.set()` method is then used to replace the old `Employee` object with a new one containing the updated details. The updated `ArrayList` is then written back to the file.

Implementing Sort on Screen (Case 6)
00:35:12

Case 6 demonstrates sorting the data on the screen by employee number. The `ArrayList` is loaded, and `Collections.sort()` is used with a custom `Comparator` implemented anonymously. The `compare` method defines sorting logic based on the difference between employee numbers (ascending order). This change only affects the display and not the file.

Implementing Sort in File (Case 7)
00:38:26

Case 7 sorts the data by employee number and then writes the sorted collection back to the file, making the changes permanent. After using `Collections.sort()` with the `Comparator`, the entire `ArrayList` is written to the file using `ObjectOutputStream`.

Advanced Sorting Options (Cases 8-11)
00:40:15

The video briefly covers adding more sorting options: by employee name (ascending and descending, both on screen and in file) and by salary (descending on screen, ascending in file). This involves modifying the `compare` method in the `Comparator` to use string comparison for names (`compareTo()`) and numerical differences for salaries, reversing the order for descending sorts.

Recently Summarized Articles

Loading...