-1

I'm trying to write an object on a file, and then to read it from the same file i populated an arraylist of object from a simple text file. i have an EOFException when i'm trying to write on file

public class CaricaTestoScriviOggetti_Copia {

    public static void main(String[] args) {
        File fileIn = new File("src/Lista.txt");
        Scanner input = null;
        try {
            input = new Scanner(fileIn);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        ArrayList<Persona> persone = new ArrayList<Persona>();
        ArrayList<Persona> maggiorenni = new ArrayList<Persona>();
        String nome = null;
        String cognome = null;
        int eta = 0;
        while (input.hasNext()) {
            nome = input.next();
            cognome = input.next();
            eta = input.nextInt();
            if (input.hasNext("\n"))
                input.nextLine();
            persone.add(new Persona(nome, cognome, eta));
        }

        for (Persona p : persone) {
            System.out.println(p);
            if (p.getEta() > 17)
                maggiorenni.add(p);
        }

        input.close();

        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        try {
            fos = new FileOutputStream("src/ListaObj.dat");
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            oos = new ObjectOutputStream(fos);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        for (Persona p : maggiorenni) {
            try {
                oos.writeObject(p);
                oos.flush();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        // END FOR

        try {
            fos.close();
            oos.close();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        FileOutputStream fos2 = null;
        ObjectOutputStream oos2 = null;
        try {
            fos2 = new FileOutputStream("src/Oggetto.dat");
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            oos2 = new ObjectOutputStream(fos);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try {
            oos2.writeObject(maggiorenni.get(1));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try {
            fos2.close();
            oos2.close();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        FileInputStream fis = null;
        ObjectInputStream ois = null;
        try {
            fis = new FileInputStream("src/Oggetto.dat");
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            ois = new ObjectInputStream(fis);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Persona catched = null;

        try {
            catched = (Persona) ois.readObject();
        } catch (ClassNotFoundException | IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // END FOR

        try {
            fis.close();
            ois.close();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        System.out.println("<------------Oggetto Letto------------>");
        System.out.println(catched);

        // END MAIN
    }
    // END CLASS
}
user207421
  • 305,947
  • 44
  • 307
  • 483
  • 1
    'I have an EOFException when i'm trying to write on file.' No you don't. You have an `EOFException` when you try to *read from* the file, and the reason will be that there is nothing *in* the file. NB Don't write code like this. Code that depends on the success of code in a prior `try` block must be inside that `try` block. – user207421 Apr 05 '17 at 09:39
  • Sorry i'have post the wrong portion of code. you think that i have to delete this post and write one with the right code portion? – Apples1986 Apr 05 '17 at 10:36
  • This is the code that give me this error – Apples1986 Apr 05 '17 at 12:27
  • You should edit your question. Remove the wrong code and put in the right code. – user207421 Apr 05 '17 at 13:42

1 Answers1

0
p = (Persona) ois.readObject();
k++;
while (p != null) {
    persone.add(p);
    p = (Persona) ois.readObject();
}

Your read loop is not valid. readObject() doesn't return null at end of stream, so testing for that as the loop termination condition is pointless.

for (;;) {
    try {
        p = (Persona) ois.readObject();
        k++;
        persone.add(p);
    } catch (EOFException exc) {
        break;
    }
}
user207421
  • 305,947
  • 44
  • 307
  • 483