-2

I am getting following:

java.io.EOFException
    at java.io.ObjectInputStream$PeekInputStream.readFully(Unknown Source)
    at java.io.ObjectInputStream$BlockDataInputStream.readShort(Unknown Source)
    at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
    at java.io.ObjectInputStream.<init>(Unknown Source)
    at com.Temp.main(Temp.java:62)

Below is the code that I am trying to run:

class Dog implements Serializable {
    private static final long serialVersionUID = 1L;
    int age = 0;
    String color = "Black";
}

public class Temp {
    public static void main(String[] args) {
        FileOutputStream fos;
        ObjectOutputStream oos;
        FileInputStream fis;
        ObjectInputStream ios;
        File doggy;
        try {
            Dog fluffy = new Dog();
            fos = new FileOutputStream("DOG_store.txt");
            oos = new ObjectOutputStream(fos);
            oos.writeObject(fluffy);
            fos.close();
            oos.close();

            doggy = new File("DOG_store.txt");
            FileWriter fw = new FileWriter(doggy); // **This is Causing ISSUE**

            fis = new FileInputStream("DOG_store.txt");
            ios = new ObjectInputStream(fis);
            Dog scrappy = (Dog) ios.readObject();
            fis.close();
            ios.close();
            System.out.println(scrappy.age + " " + scrappy.color);
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } catch (ClassNotFoundException cfne) {
            cfne.printStackTrace();
        }
    }
Ashish Aggarwal
  • 3,018
  • 2
  • 23
  • 46
Twaha Mehmood
  • 737
  • 3
  • 9
  • 26
  • Did you check that `doggy` file is created or not ? – Rookie007 Jan 15 '14 at 06:43
  • "doggy" File object is refering to the already created file "DOG_store.txt".... and yes this file is being created. – Twaha Mehmood Jan 15 '14 at 06:45
  • But you are not even using `FileWriter fw` – Scary Wombat Jan 15 '14 at 06:48
  • `Stacktrace` posted is showing error is on `new ObjectInputStream(fis)`, can you verify this first? also remove `FileWriter fw` construction as it is not in use. – harsh Jan 15 '14 at 06:52
  • @user2310289 : yes... I am not even using it, but this is causing the exception. If i remove this line the code runs fine... I want to understand what this FileWriter constructor does to cause this exception – Twaha Mehmood Jan 15 '14 at 06:53
  • 1
    Well there is your answer isn't it. – Scary Wombat Jan 15 '14 at 06:54
  • 1
    A: Doctor, Doctor, it hurts when I poke myself in the eye. Doctor: Well stop poking yourself in the eye. – Scary Wombat Jan 15 '14 at 06:54
  • Of course the `new FileWriter` line is 'causing ISSUE'. It overwrites the file, so it is now zero length, so when you go to read an object from it you fail. Just remove it. What's the point of it anyway? – user207421 Jan 15 '14 at 07:18

1 Answers1

2

when you do FileWriter fw = new FileWriter(doggy); it opens file in write mode and deletes the previous data of file. Thats why whie reading the file it gives EOFException because there is nothing to read in file.

If you do like this FileWriter fw = new FileWriter(doggy,true); then there will be no error because it does not delete the previous data of file.

AJ.
  • 4,526
  • 5
  • 29
  • 41