0

I have a problem with reading objects from file Java.

file is anarraylist<projet>

This is the code of saving objects :

try {
    FileOutputStream fileOut = new FileOutputStream("les projets.txt", true);
    ObjectOutputStream out = new ObjectOutputStream(fileOut);

    for (projet a : file) {
        out.writeObject(a);
    }
    out.close();
} catch (Exception e) {
    e.printStackTrace();
}

And this is the code of reading objects from file ::

try {
    FileInputStream fileIn = new FileInputStream("les projets.txt");
    ObjectInputStream in = new ObjectInputStream(fileIn);

    while (in.available() > 0){
        projet c = (projet) in.readObject();

        b.add(c);
    }

    choisir = new JList(b.toArray());
    in.close();
} catch (Exception e) {
    e.printStackTrace();
}

Writing is working properly. The problem is the reading... it does not read any object (projet) What could be the problem?

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Hi, did you get any exception message printed by the `e.printStackTrace()` line of your code? If so, could you paste the trace to the question, please. – mico Feb 11 '14 at 20:55
  • sry I have editing my question cz any object was readen !!! – user3285843 Feb 11 '14 at 21:03
  • @mico noo any exception message was printed – user3285843 Feb 11 '14 at 21:04
  • 3
    Don't use available() like that. It isn't a valid test for end of stream. See the Javadoc. The correct way to detect end of an object stream is to catch EOFException. And serialized files are not text, so don't call them ".txt". And why not just serialize and deserialize the ArrayList, and avoid the loops? – user207421 Feb 11 '14 at 21:05
  • @EJP now hava eliminated the avalaible () and changed .txt to .data now it read just one object – user3285843 Feb 11 '14 at 21:16
  • 1
    Then you must be getting an exception, contrary to your statement above. NB you can't append to object output streams, at least not without special code. – user207421 Feb 11 '14 at 22:24

1 Answers1

0

As mentioned by EJP in comment and this SO post . if you are planning to write multiple objects in a single file you should write custom ObjectOutputStream , because the while writing second or nth object header information the file will get corrupt.
As suggested by EJP write as ArrayList , since ArrayList is already Serializable you should not have issue. as

out.writeObject(file) and read it back as ArrayList b = (ArrayList) in.readObject();
for some reason if you cant write it as ArrayList. create custome ObjectOutStream as

class MyObjectOutputStream extends ObjectOutputStream {

public MyObjectOutputStream(OutputStream os) throws IOException {
    super(os);
}

@Override
protected void writeStreamHeader() {}

}

and change your writeObject as

try {
        FileOutputStream fileOut= new FileOutputStream("les_projets.txt",true);
        MyObjectOutputStream out = new MyObjectOutputStream(fileOut );

         for (projet a : file) {
    out.writeObject(a);
}
        out.close();
    }

    catch(Exception e)
    {e.printStackTrace();

}

and change your readObject as

ObjectInputStream in = null;
    try {
        FileInputStream fileIn = new FileInputStream("C:\\temp\\les_projets1.txt");
        in = new ObjectInputStream(fileIn );

        while(true) {
            try{
                projet c = (projet) in.readObject();
                b.add(c);
            }catch(EOFException ex){
                // end of file case
                break;
            }

        }

    }catch (Exception ex){
        ex.printStackTrace();
    }finally{
        try {
            in.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
Community
  • 1
  • 1
Mani
  • 3,274
  • 2
  • 17
  • 27
  • Error with close() potentiel.java:263: error: unreported exception IOException; must be caught or declared to be thrown ex.printStackTrace(); }finally{ in.close(); } – user3285843 Feb 11 '14 at 21:51
  • in read object's finally block try catch should be added. i have modified the code. – Mani Feb 11 '14 at 21:57
  • Use try-with-resources to handle closing streams. – Pshemo Feb 11 '14 at 21:57
  • @Pshemo Thanks for the info. i am not sure the OP wants the code in 1.7 – Mani Feb 11 '14 at 22:03
  • now I have got new error ! java.io.StreamCorruptedException: invalid stream header: 73720013 with this line : in = new ObjectInputStream(fileIn ); – user3285843 Feb 12 '14 at 12:25
  • post your updated code. have you written file using new method ? – Mani Feb 12 '14 at 14:39
  • You must only use this extended object output stream when you are appending. When creating the initial file you must use a normal ObjectOutputStream. Just one of many reasons why I dislike the technique. – user207421 Feb 12 '14 at 17:57