0% found this document useful (0 votes)
9 views4 pages

Java Student Database Management

The document outlines a Java program for managing a student database, allowing users to create, display, clear, modify, and search student records. It defines a 'Student' class with attributes such as student ID, name, roll number, class, marks, and address, and implements file handling to store records in a text file. The program features a menu-driven interface for user interaction and data management.

Uploaded by

adobexexe1
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)
9 views4 pages

Java Student Database Management

The document outlines a Java program for managing a student database, allowing users to create, display, clear, modify, and search student records. It defines a 'Student' class with attributes such as student ID, name, roll number, class, marks, and address, and implements file handling to store records in a text file. The program features a menu-driven interface for user interaction and data management.

Uploaded by

adobexexe1
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

Name:Nikita Kulkarni

roll no : 207A060
bacth:C
DIV: SE-01
Experiment: 08 File handling

import [Link].*;
import [Link].*;

class Student {
String studentId;
String name;
String rollNo;
String className;
String marks;
String address;

public Student(String studentId, String name, String rollNo, String


className, String marks, String address) {
[Link] = studentId;
[Link] = name;
[Link] = rollNo;
[Link] = className;
[Link] = marks;
[Link] = address;
}

@Override
public String toString() {
return studentId + "," + name + "," + rollNo + "," + className +
"," + marks + "," + address;
}
}

public class StudentDatabase {

private static final String FILE_NAME = "student_records.txt";

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


Scanner scanner = new Scanner([Link]);
int choice;

do {
[Link]("Student Database Menu:");
[Link]("1. Create Database");
[Link]("2. Display Database");
[Link]("3. Clear Records");
[Link]("4. Modify Record");
[Link]("5. Search Record");
[Link]("6. Exit");
[Link]("Enter your choice: ");
choice = [Link]();
[Link](); // Consume newline

switch (choice) {
case 1:
createDatabase(scanner);
break;
case 2:
displayDatabase();
break;
case 3:
clearRecords();
break;
case 4:
modifyRecord(scanner);
break;
case 5:
searchRecord(scanner);
break;
case 6:
[Link]("Exiting...");
break;
default:
[Link]("Invalid choice. Please try
again.");
}
} while (choice != 6);
[Link]();
}

private static void createDatabase(Scanner scanner) throws


IOException {
FileWriter fw = new FileWriter(FILE_NAME, true);
[Link]("Enter details for 5 students:");
for (int i = 0; i < 5; i++) {
[Link]("Student ID: ");
String studentId = [Link]();
[Link]("Name: ");
String name = [Link]();
[Link]("Roll No: ");
String rollNo = [Link]();
[Link]("Class: ");
String className = [Link]();
[Link]("Marks: ");
String marks = [Link]();
[Link]("Address: ");
String address = [Link]();

Student student = new Student(studentId, name, rollNo,


className, marks, address);
[Link]([Link]() + "\n");
}
[Link]();
[Link]("Database created successfully.");
}

private static void displayDatabase() throws IOException {


FileReader fr = new FileReader(FILE_NAME);
BufferedReader br = new BufferedReader(fr);
String line;

[Link]("Student Records:");
while ((line = [Link]()) != null) {
String[] data = [Link](",");
[Link]("Student ID: " + data[0]);
[Link]("Name: " + data[1]);
[Link]("Roll No: " + data[2]);
[Link]("Class: " + data[3]);
[Link]("Marks: " + data[4]);
[Link]("Address: " + data[5]);
[Link]("-----------------------------");
}
[Link]();
}

private static void clearRecords() throws IOException {


FileWriter fw = new FileWriter(FILE_NAME);
[Link](""); // Clear the content of the file
[Link]();
[Link]("All records cleared successfully.");
}

private static void modifyRecord(Scanner scanner) throws IOException


{
[Link]("Enter Student ID to modify: ");
String studentIdToModify = [Link]();
List<Student> students = new ArrayList<>();

// Read existing records


try (BufferedReader br = new BufferedReader(new
FileReader(FILE_NAME))) {
String line;
while ((line = [Link]()) != null) {
String[] data = [Link](",");
[Link](new Student(data[0], data[1], data[2],
data[3], data[4], data[5]));
}
}

// Modify the record if found


boolean found = false;
for (Student student : students) {
if ([Link](studentIdToModify)) {
found = true;
[Link]("Current details: " + student);
[Link]("Enter new Name (leave blank to keep
unchanged): ");
String name = [Link]();
if (![Link]()) [Link] = name;

[Link]("Enter new Roll No (leave blank to keep


unchanged): ");
String rollNo = [Link]();
if (![Link]()) [Link] = rollNo;

[Link]("Enter new Class (leave blank to keep


unchanged): ");
String className = [Link]();
if (![Link]()) [Link] = className;

[Link]("Enter new Marks (leave blank to keep


unchanged): ");
String marks = [Link]();
if (![Link]()) [Link] = marks;
[Link]("Enter new Address (leave blank to keep
unchanged): ");
String address = [Link]();
if (![Link]()) [Link] = address;

[Link]("Record updated successfully.");


}
}

// Write back to the file


try (FileWriter fw = new FileWriter(FILE_NAME)) {
for (Student student : students) {
[Link]([Link]() + "\n");
}
}

if (!found) {
[Link]("No record found for Student ID: " +
studentIdToModify);
}
}

private static void searchRecord(Scanner scanner) throws IOException


{
[Link]("Enter Student ID to search: ");
String studentIdToSearch = [Link]();
boolean found = false;

try (BufferedReader br = new BufferedReader(new


FileReader(FILE_NAME))) {
String line;
while ((line = [Link]()) != null) {
String[] data = [Link](",");
if (data[0].equals(studentIdToSearch)) {
found = true;
[Link]("Student Record Found:");
[Link]("Student ID: " + data[0]);
[Link]("Name: " + data[1]);
[Link]("Roll No: " + data[2]);
[Link]("Class: " + data[3]);
[Link]("Marks: " + data[4]);
[Link]("Address: " + data[5]);
break;
}
}
}

if (!found) {
[Link]("No record found for Student ID: " +
studentIdToSearch);
}
}
}

You might also like