write a program to perform operations On file

write a program to perform operations On file

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.

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
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
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
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
write a program  write a program add 2 no.s without use arithmetic,unary operaters in java
Java program to read a text file and write to another file
Java program to read a text file and write to another file - Creating a simple program for reading text file and writing data into another text file.... Our requirement is to read a text file and then write the content of the text
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 java program
write a java program  write a program to print '*' in a circular form
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
How to write Java Program
How to write Java Program  how to write a program to find average of 5 student marks
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... program to write text to a file:ADS_TO_REPLACE_3
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
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
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
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
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 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
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
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
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
Write a program in java...
Write a program in java...  Hi, friends Please, can you help me? Q1: Write a program in java to simulate a calculator. Your program should take two... to enter an integer number. Write a program in java to find the factorial
write a program - Java Beginners
write a program  1. write a program that reads a string composed of 6 words then generate and print a word composed of the first letters of the 6 words.  Hi friend, Code to solve the problem : class FirstLetter
WRITE A PROGRAM IN JAVA
WRITE A PROGRAM IN JAVA  How do I write a program in Java, have the program display a message with your name in it and a number (Hello john Smith!1) The number must increment from zero to nine, or decrement fron nine to zero. You
Write data in doc file
Write data in doc file  How to write data or string in word file
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...: " + e.getMessage()); } } } If you write the program given above
Java Write To File - Java Tutorial
; write data into new file: ADS_TO_REPLACE_2 out.write(read_the_Buffered_file...For writing data to a file, the class FileWriter and BufferedWriter... of OutputStreamWriter class that is used to write text (as opposed to binary data
write following program
-threading. Explain with examples Q2 Write a java applet that takes your name through a parameter from the HTML file and displays it Q3 Write a short note...write following program  Q1 What is the difference between single
write program - Java Beginners
write program  write a java program that will read the values of matrix A and B give its multiplication in matrix C  Hi friend, Code for Matrix Multiplication : class MatrixMultiplication{ public static void
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.  arrange the natural n umber in 5x5 matrix as 21 22 23 24 25 20 7 8 9 10 19 6 1 2 11 18 5 4 3 12 17 16 15 14 13 i at centerd position and remaining arrange in anticlockwise direction.  
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... be wrapped around any Writer because, some Writer's write() method's operations
how to write servlet program in eclipse?
how to write servlet program in eclipse?  how to write servlet program in eclipse
Write a program to print using for loop
Write a program to print using for loop  Write a program to print using for loop * * * *  
How to write calculator in J2ME program?
How to write calculator in J2ME program?  How to write calculator in J2ME program
write a java program to solve the infix expression
write a java program to solve the infix expression  ==>> solve the below expression using java program: 2+5*2-5+6/2 using stack operations or any other process in java
C file write example
;) opens the file and allow to perform write operations into the file. The library... C file write example       This section demonstrates you to write the data into the file. Here we
Java Write To File BufferedWriter
Java Write To File BufferedWriter In this tutorial you will learn how to write... Writer because, some Writer's write() method's operations may take more...); bw.write("This example demonstrates you how to write in a file using
How to write to file using FileOutputStream
How to write to file using FileOutputStream  Hi friends, Please help me in java program. How to write to file using FileOutputStream? thanks,   Hi, To write a file using FileOutputStream, we have to use
how to write to file at the end of the line
how to write to file at the end of the line  Hi, Where do i learn how to write to file at the end of the line using Java program. Plz suggest... will help you for "How to write the file at the end of line using Java language
write a program to create a user defined
write a program to create a user defined   plz send me code   Hi Friend, Please specify what do you want to create?ADS_TO_REPLACE_1 Thanks
how to write this program? - Java Beginners
how to write this program?  (game:scissor,rock ,paper) A program that plays the sciccor-rock-paper game.Revise the program to let the user continuously play until either the user or the computer wins more than 4 times
How to write to xml file in Java?
How to write to xml file in Java?  Hi Friends, Can anyone help me how to write in xml file in Java programming. Please fill free to give example or reference website for getting example.   Hi, To write in xml file
how to write append file in Java
how to write append file in Java  How to write append file in Java   Hi, For append in the test new file or Appending a text earlier existing file should be retained when you are trying to write new contents

Ads