2

I have created an array of objects of type Employee and i am putting the objects in a .txt file. Below is the method that accepts Employee objects a parameter and puts it into the .txt file

public void putDataintoFile(Employee[] obj) {
    File file = new File("employeedetails.txt");
    try {
        FileOutputStream fos = new FileOutputStream(file);
        try {
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(obj);
            oos.close();
            fos.close();
        } 
        catch (IOException ex) {
            Logger.getLogger(EmployeeService1.class.getName()).log(Level.SEVERE, null, ex);
        }
    } 
    catch (FileNotFoundException ex) {
        Logger.getLogger(EmployeeService1.class.getName()).log(Level.SEVERE, null, ex);
    }
}

This above method is invoked from a seperate "Execetor class " which then calls my getDataFromFile() method below

public void getDataFromFile() {
    System.out.println("Reached HERE");
    try {
        FileInputStream fin = new FileInputStream("employeedetails.txt");
        try {
            ObjectInputStream oin= new ObjectInputStream(fin);
            try {
                Employee    e =(Employee)oin.readObject();
                System.out.println("Reached HERE");
                System.out.println(e.toString());
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(EmployeeService1.class.getName()).log(Level.SEVERE, null, ex);
            }
            oin.close();
            fin.close();
        } 
        catch (IOException ex) {
            Logger.getLogger(EmployeeService1.class.getName()).log(Level.SEVERE, null, ex);
        }
    } 
    catch (FileNotFoundException ex) {
        Logger.getLogger(EmployeeService1.class.getName()).log(Level.SEVERE, null, ex);
    }
}

On executign I get an error which says.

Exception in thread "main" java.lang.ClassCastException: [LMypackage.Employee; cannot
be cast to Mypackage.Employee
at Mypackage.EmployeeService1.getDataFromFile(EmployeeService1.java:225)
at Mypackage.Executor.main(Executor.java:71)

I have implemented Serializable interface in my Employee Classs

Can anyone help me ?

1 Answer 1

3

You're writing an array and trying to read a single object.

This is stated in the exception message, which leads to the read/write code, which is fairly obvious despite all the superfluous vertical whitespace.

Also, in general, don't call not-text files .txt.

Sign up to request clarification or add additional context in comments.

2 Comments

code Employee e[] = new Employee[2]; for(int i=0 ;i<2;i++) { e[i] =(Employee)oin.readObject(); System.out.println(e[i]); } code I tried this but still the same error
its working I was not entering the objects into the file using a loop I changed my code putData() tofor(int i=0 ;i<2;i++) { oos.writeObject(obj[i]); } oos.close(); fos.close(); code and my getData method to code Employee e[] = new Employee[2]; for(int i=0 ;i<2;i++) { e[i] =(Employee)oin.readObject(); System.out.println(e[i]); } code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.