write a program to perform operations On file. Create a file named "emp.txt" and stores the details of employee like emp_no, emp _name, and esal. And provide the facility to create, delete and view. You have to perform only 3 operations : 1. Create, 2. Delete, 3. View.
The given code allow the user to create, view and delete employee data from the file. The file stores employee number, employee name and employee salary.
import java.io.*;
import java.util.*;
class EmployeeRecord{
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
File file = new File("c:/emp.txt");
boolean exit=false;
do{
System.out.println("1 Create");
System.out.println("2 Delete");
System.out.println("3 View Employee");
System.out.println("4 View All Employees");
System.out.println("5 Exit");
System.out.print("Enter your choice: ");
int choice=input.nextInt();
switch(choice){
case 1:
System.out.print("Enter Employee No: ");
String no=input.nextLine();
System.out.print("Enter Employee Name: ");
String name=input.nextLine();
System.out.print("Enter Employee Salary: ");
String sal=input.nextLine();
try {
BufferedWriter output = new BufferedWriter(new FileWriter(file,true));
output.write(no + " " + name + " " + sal);
output.newLine();
output.close();
System.out.println("Record is added successfully!");
}
catch(Exception e){}
break;
case 2:
System.out.print("Enter Employee No to delete: ");
String eno=input.next();
File temp = new File("c:/temp.txt");
try {
BufferedWriter output = new BufferedWriter(new FileWriter(temp));
BufferedReader freader = new BufferedReader(new FileReader(file));
String s;
while ((s = freader.readLine()) != null) {
String[] f = s.split(" ");
String empno = f[0];
String ename = f[1];
String esalary = f[2];
if (!empno.equals(eno)) {
output.write(s);
output.newLine();
}
}
System.out.println("Record is deleted successfully!");
freader.close();
output.close();
}
catch (Exception e) {
}
file.delete();
temp.renameTo(file);
break;
continue..
case 3:
System.out.print("Enter Employee No to view record: ");
String empno=input.next();
try {
BufferedReader freader = new BufferedReader(new FileReader(file));
String s;
while ((s = freader.readLine()) != null) {
String[] st = s.split(" ");
String emplno = st[0];
String ename = st[1];
String esalary = st[2];
if(emplno.equals(empno)){
System.out.println("Employee Name: "+ename);
System.out.println("Employee Salary: "+esalary);
}
}
}
catch(Exception e){
System.out.print(e);
}
break;
case 4:
try {
BufferedReader freader = new BufferedReader(new FileReader(file));
String s;
while ((s = freader.readLine()) != null) {
String[] st = s.split(" ");
String emplno = st[0];
String ename = st[1];
String esalary = st[2];
System.out.println(emplno+"\t"+ename+"\t"+esalary);
}
}
catch(Exception e){
System.out.print(e);
}
break;
case 5:
exit=true;
System.exit(0);
break;
}
}
while(!exit);
}
}
For more information, visit the following link:
http://www.roseindia.net/java/example/java/swing/addeditanddeleteemployee_inf.shtml