Wednesday, 9 December 2015

Simple Code To Write Data in File And Read Data to File

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;

public class File implements Serializable {
    int no;
    String Name;
    double Sal;

    public File() {

        this.no = 100;
        Name = "Amol";
        Sal = 5000;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

    public double getSal() {
        return Sal;
    }

    public void setSal(double sal) {
        Sal = sal;
    }

    public File(int no, String name, double sal) {
        super();
        this.no = no;
        Name = name;
        Sal = sal;
    };

    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return "Information is : " + no + "   " + Name + "   " + Sal;
    }

    void display() {
        System.out.println("Information is : " + no + "   " + Name + "   "
                + Sal);
    }

}

class C {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Scanner s = new Scanner(System.in);
        ArrayList a = new ArrayList();

        int ch1;

        do {
            System.out.println("1) Write");
            System.out.println("2) Read");
            System.out.println("Enter Your Choice");
            ch1 = s.nextInt();

            switch (ch1) {

            case 1: {
                FileInputStream fie = new FileInputStream(
                        "D:\\Amol\\khushi\\amu.txt");
                ObjectInputStream ob = new ObjectInputStream(fie);
                ArrayList a2 = (ArrayList) ob.readObject();
                System.out.println("How Many Details You want to Enter");
                int ch = s.nextInt();
                File f1[] = new File[ch];
                for (int i = 0; i < f1.length; i++) {

                    System.out.println("Enter the id : ");
                    int aaa = s.nextInt();
                    System.out.println("Enter the Name : ");
                    String aa = s.next();
                    System.out.println("Enter The Salary : ");
                    double ss = s.nextDouble();
                    f1[i] = new File(aaa, aa, ss);
                    a2.add(f1[i]);
                    try {
                        FileOutputStream fio = new FileOutputStream(
                                "D:\\Amol\\khushi\\amu.txt");
                        ObjectOutputStream ob1 = new ObjectOutputStream(fio);
                        ob1.writeObject(a2);
                        System.out.println("Write Sucessfully");
                    } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    f1[i].display();
                }

                break;

            }
            case 2: {
                try {
                    FileInputStream fie = new FileInputStream(
                            "D:\\Amol\\khushi\\amu.txt");
                    ObjectInputStream ob = new ObjectInputStream(fie);
                    ArrayList f2 = (ArrayList) ob.readObject();
                    Iterator it = f2.iterator();
                    while (it.hasNext()) {
                        System.out.println(it.next());
                    }

                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (ClassNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
                break;
            }
        } while (ch1 != 10);
    }
}

No comments:

Post a Comment