0% found this document useful (0 votes)
45 views10 pages

23BCP422 Dbms

Uploaded by

yourboirobo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views10 pages

23BCP422 Dbms

Uploaded by

yourboirobo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Experiment 1

TITLE: Dataset Creation and Updating using File Handling Program.


Objective: How to store and retrieve dataset in table format using
file handling programming.
Tasks to be carried out in LAB
1. Create a Record:

Add a new student record to the file. Ensure that the roll number is unique (no

duplicates).

2. Read Records:

Display all student records stored in the file in a tabular format. Allow

searching for a record by roll number.

3. Update a Record:

Update the details of a student by their roll number. Ensure the updated data is

saved in the file.

4. Delete a Record:

Delete a student record using their roll number. Ensure the file reflects the

changes after deletion.

5. Exit Program:

Provide an option to exit the program.

CODE:-
import java.io.*;

import java.util.*;

public class file {

static String FILE_NAME = "students.txt";

public static void main(String[] args) throws IOException {

Scanner scanner = new Scanner(System.in);


System.out.println("Welcome to the Student Record Management System!");

String csvFile = "ss.csv";

try (BufferedReader csvReader = new BufferedReader(new FileReader(csvFile));

BufferedWriter writer = new BufferedWriter(new FileWriter(FILE_NAME, true))) {

String line;

while ((line = csvReader.readLine()) != null) {

String[] data = line.split(",");

writer.write(data[0] + "," + data[1] + "," + data[2] + "," + data[3]);

writer.newLine();

System.out.println("CSV data successfully imported into student records.\n");

while (true) {

System.out.println("""

\nMenu:

1. Add a Student Record

2. View All Records

3. Search a Record by Roll Number

4. Update a Record

5. Delete a Record

6. Exit

Enter your choice:""");

try {

int choice = Integer.parseInt(scanner.nextLine());

switch (choice) {

case 1 -> addStudent(scanner);

case 2 -> viewAllRecords();


case 3 -> searchRecord(scanner);

case 4 -> updateRecord(scanner);

case 5 -> deleteRecord(scanner);

case 6 -> {

System.out.println("Exiting program. Goodbye!");

return;

default -> System.out.println("Invalid choice! Please enter a number between 1 and 6.");

} catch (NumberFormatException e) {

System.out.println("Invalid input! Please enter a number.");

private static boolean isRollNumberUnique(String rollNumber) throws IOException {

try (BufferedReader reader = new BufferedReader(new FileReader(FILE_NAME))) {

String line;

while ((line = reader.readLine()) != null) {

String[] data = line.split(",");

if (data[0].equals(rollNumber)) {

return false;

return true;

private static void addStudent(Scanner scanner) throws IOException {

System.out.print("Enter Roll Number: ");

String rollNumber = scanner.nextLine();


if (!isRollNumberUnique(rollNumber)) {

System.out.println("Error: Roll number already exists!\n");

return;

System.out.print("Enter Name: ");

String name = scanner.nextLine();

System.out.print("Enter Age: ");

String age = scanner.nextLine();

System.out.print("Enter Department: ");

String department = scanner.nextLine();

try (BufferedWriter writer = new BufferedWriter(new FileWriter(FILE_NAME, true))) {

writer.write(rollNumber + "," + name + "," + age + "," + department);

writer.newLine();

System.out.println("Student record added successfully!\n");

private static void viewAllRecords() throws IOException {

System.out.println("\nRoll Number\tName\tAge\tDepartment");

System.out.println("-".repeat(40));

try (BufferedReader reader = new BufferedReader(new FileReader(FILE_NAME))) {

String line;

boolean recordsFound = false;

while ((line = reader.readLine()) != null) {

String[] data = line.split(",");


System.out.printf("%s\t\t%s\t%s\t%s%n", data[0], data[1], data[2], data[3]);

recordsFound = true;

if (!recordsFound) {

System.out.println("No records found!\n");

// Searches for a student record by roll number

private static void searchRecord(Scanner scanner) throws IOException {

System.out.print("Enter Roll Number to search: ");

String rollNumber = scanner.nextLine();

try (BufferedReader reader = new BufferedReader(new FileReader(FILE_NAME))) {

String line;

while ((line = reader.readLine()) != null) {

String[] data = line.split(",");

if (data[0].equals(rollNumber)) {

System.out.println("\nRecord Found:");

System.out.println("Roll Number\tName\tAge\tDepartment");

System.out.println("-".repeat(40));

System.out.printf("%s\t\t%s\t%s\t%s%n\n", data[0], data[1], data[2], data[3]);

return;

System.out.println("Record not found!\n");

}
// Updates a student record by roll number

private static void updateRecord(Scanner scanner) throws IOException {

System.out.print("Enter Roll Number to update: ");

String rollNumber = scanner.nextLine();

List<String> records = new ArrayList<>();

boolean recordUpdated = false;

try (BufferedReader reader = new BufferedReader(new FileReader(FILE_NAME))) {

String line;

while ((line = reader.readLine()) != null) {

String[] data = line.split(",");

if (data[0].equals(rollNumber)) {

System.out.println("\nCurrent Record:");

System.out.printf("Roll Number: %s, Name: %s, Age: %s, Department: %s%n", data[0],
data[1], data[2], data[3]);

System.out.print("Enter new Name: ");

String name = scanner.nextLine();

System.out.print("Enter new Age: ");

String age = scanner.nextLine();

System.out.print("Enter new Department: ");

String department = scanner.nextLine();

records.add(rollNumber + "," + name + "," + age + "," + department);

recordUpdated = true;

} else {

records.add(line);
}

if (recordUpdated) {

try (BufferedWriter writer = new BufferedWriter(new FileWriter(FILE_NAME))) {

for (String record : records) {

writer.write(record);

writer.newLine();

System.out.println("Record updated successfully!\n");

} else {

System.out.println("Error: Record not found!\n");

// Deletes a student record by roll number

private static void deleteRecord(Scanner scanner) throws IOException {

System.out.print("Enter Roll Number to delete: ");

String rollNumber = scanner.nextLine();

List<String> records = new ArrayList<>();

boolean recordDeleted = false;

try (BufferedReader reader = new BufferedReader(new FileReader(FILE_NAME))) {

String line;

while ((line = reader.readLine()) != null) {

String[] data = line.split(",");

if (data[0].equals(rollNumber)) {
recordDeleted = true;

} else {

records.add(line);

if (recordDeleted) {

try (BufferedWriter writer = new BufferedWriter(new FileWriter(FILE_NAME))) {

for (String record : records) {

writer.write(record);

writer.newLine();

System.out.println("Record deleted successfully!\n");

} else {

System.out.println("Error: Record not found!\n");

OUTPUT:-

TXTFile
TERMINAL:-

You might also like