Share on Google+Share on Google+

Azim Ajmeri
write a program to perform operations On file
2 Answer(s)      4 years and 11 months ago
Posted in : JavaScript Questions

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.

Ads

View Answers

May 8, 2012 at 1:05 PM


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;


May 8, 2012 at 1:06 PM


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










Related Tutorials/Questions & Answers:
Write an event driven program to perform the arithmetic operations as shown in the interface
Write an event driven program to perform the arithmetic operations as shown... the java programming code for that question. my question is here: Q. Write an event driven program to perform the arithmetic operations as shown
Java JMenuItem to perform file operations
Java JMenuItem to perform file operations In this tutorial, you will learn how to use JMenuItem class to perform file operations. Java Swing introduced... example in java swing through which you can perform open and save operations
Advertisements
File Operations
File Operations  Please i would like to know if it is possible to read from a file and write to the same file at the same time
file operations
file operations  everytime we run the program the data in the already file is overridding ,so,how to append the data to the already existing files .so please send the function required and also source code
How to perform CRUD operations using gwt on Restlet server 2.0
How to perform CRUD operations using gwt on Restlet server 2.0  I want to perform CRUD operation using gwt on restlet server 2.0. The CRUD operations are like create, read, update, delete operations.Any generic code
write a program in java to read a text file and write the output to an excel file using filereader and filewriter?
write a program in java to read a text file and write the output to an excel file using filereader and filewriter?  write a program in java to read a text file and write the output to an excel file using filereader and filewriter
Write a program in Java to append content at the end of an already existing file.
Write a program in Java to append content at the end of an already existing file.  Write a program in Java to append content at the end of an already existing file
Retrive data from database and perform binary tree operations on that data in jsp or java
Retrive data from database and perform binary tree operations on that data in jsp or java  To develop code for MLM project. I want to retrieve data from database dynamically and perform operations on that. Just like calculate
without writing web.xml file we can write servlet program
without writing web.xml file we can write servlet program  Sir Morning... Sir I have one Question "without writing web.xml file we can write servlet program". if yes which way? if no why? but without use Annotation........ Plz
program that uses while loops to perform the following steps :
program that uses while loops to perform the following steps :  Write a program that uses while loops to perform the following steps : a. Prompt the use to input two integers : FirstNum and secondNum.( FirstNum must be less
write a program
write a program  write a program add 2 no.s without use arithmetic,unary operaters in java
How to write in File in Java
How to write in File in Java  Hi, How to write in File in Java. Please suggest and give example of this program. thanks
Read and write file
Read and write file  HI, How to read and write file from Java program? Thanks   Hi, See the following tutorials: Java Write To File Read File Thanks
Java Write To File - Java Tutorial
on a FileOutputStream classes to write data to a file from Java program... Tutorial you will learn how to write java program to write to a file. We will use...?": Here is the code of java program to write text to a file
How to write in File in Java
bufferedWriter object to write in the file. For more details and examples related to How to write in File of Java Program...How to write in File in Java  Hi, How to write in File in Java
write a java program
write a java program  write a program to print '*' in a circular form
Java write to file
files. You can easily use the the FileWriter and BufferedWriter to write data to a text file. Here is the examples: Example program to write to file. File...Java write to file  How to write to a file in Java? Is there any
How to read the data in text file seperated by by ',' in java using IO Operations
how to write the program...How to read the data in text file seperated by by ',' in java using IO Operations  in Text file data like raju 45,56,67 ramu 46,65,78 raji
how to write this program
how to write this program  Write a program in java which calculates... or numerical grade. You will be submitting TWO versions of this program. Write one...?Ęs GPA. This program should take a student√Ę‚?¨‚?Ęs First Name, Last Name a GPA
How to write Java Program
How to write Java Program  how to write a program to find average of 5 student marks
How to Write to a File in Java
to a file from Java program. To write the Java Write To File we will use two...In this Java tutorial, we will demonstrate how to write to a file in Java...()); } } } If you write the program given above and execute it, this will write "Hello
How to Write to file in Java?
How to Write to file in Java?  How to Write to file in Java Program... the Examples of How to write to File in Java Program: WriteToFileExample.java import... how to write in a file."); br.close(); } catch (Exception e) { System.err.println
WRITE A PROGRAM IN JAVA
WRITE A PROGRAM IN JAVA  Write program which accepts five students id,name,age,department,courses, taken and its grade. a. calculate its GPA . display all student information in ascending order
write the java program?
write the java program?  1 11 121 1331 14641 15101051
write the java program?
write the java program?  1 11 121 1331 14641 15101051
Write java program?
Write java program?  1 11 121 1331 14641 15101051
Write java program?
Write java program?  1 11 121 1331 14641 15101051
Write data in doc file
Write data in doc file  How to write data or string in word file
write a program to print
write a program to print   write a program to print following 1 2 3 4 5 4 3 2 1 1 2 3 4 4 3 2 1 1 2 3 3 2 1 1 2 2 1 1 1
Write a java application program........?
Write a java application program........?  Welcome every One : I have Q in Java? Q : Write a java application program that reads 20 integer numbers input by the user and prints how many numbers > 100 and how many numbers
Java Write to File
Java Write to File In this tutorial you will learn how to write to file in java. Write to a file in java we are required to use some classes of java.io... Writer because, some Writer's write() method's operations may take more
Tutorials   
Java Spring Hibernate Struts Training code for programming How can I dynamically load user-entered data as content of jquery dialog? java webcam integration question java coding Fix Scrollbar position in a checkboxList after postback Information Save Alert Country wise redirection to files Interface in javascript connect database with javascript Zip Code ZipCode how to validate the email login How to play FLV video files on Ipad using HTML code or javascript code? Nitobi Grid is not working in IE10/IE11 jquery issue plese help RegEx Tree structure from json whose parents can be dragged and dropped from child newyorklife insurance getElementById not working javascript code for search in html file Implement The Skeleton Code Implement This Skeleton Code Mobile Automation using Appium Computer forensic application Java: Searching a string from doc files java linked list urgent !!! java linked list urgent!!! library management library management library management library management question Html form using JavaScript to display the table content java how to prevent no from unroundin off on clicking text box again in JSP and Jquery graph generation from xml design file display data from database in textbox when id entered in textbox Javascript Code for all fields how to get HTTP GET request value using javascript array Javascript Querry autocomplete(): Spring mvc with jquery: I am not getting correct value in the text filed. Please help me Create SpreadSheet same as excel using java applet Create SpreadSheet same as excel using java applet How to change focus among multiple select boxes? Write JQUERY Code Write JQUERY Code Converting jsp variable to java variable java

Ads

 
Advertisement null

Ads