0

And my exception just became:

 java.io.EOFException
at java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2598)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1318)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:370)
at service.TemplateService.findAll(TemplateService.java:46)
at Main.main(Main.java:33)

I know this question has been asked a few times but i couldn't get an answer from other people code, though. I'm trying to read all binary files from a folder given as parameter,each file having an object of the same type written inside. Here's the code:\

  public Set<T> findAll(String fileToSearch) throws ClassNotFoundException, FileNotFoundException {

    Object a = new Object();
    Set<T> set = new HashSet<T>();
    File[] files = new File(fileToSearch).listFiles();
    for (File file : files) {
        if (file.isFile()) {
            FileInputStream fis = new FileInputStream(file);
            try{
                ObjectInputStream ois = new ObjectInputStream(fis);
                a=(T) ois.readObject();
                System.out.println(a);
                set.add((T)a);
                ois.close();
                fis.close();
            }catch (IOException e) {
                e.printStackTrace();}

            }
        }
    return set;
}

And this is the function i used to create those items:

public void add(T item) throws IOException {
    String file="";
    if(item instanceof User){
        file="users/user"+((User)item).getId();
    }

    FileOutputStream fis = null;
    try {
        fis = new FileOutputStream(file,true);
    } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    try {

        AppObjectOutputStream aois = new AppObjectOutputStream(item,fis);
        aois.close();

    }
    catch(EOFException e){fis.close();}
    catch (IOException e) {
        System.out.println("Eroare la adaugare item :: " + this);
    }
    try{
        FileOutputStream fos=new FileOutputStream("numberfile");
        ObjectOutputStream oos=new ObjectOutputStream(fos);
        oos.writeInt(User.no);
        oos.close();
        fos.close();
    }catch(Exception e){}
}
hallelujah
  • 13
  • 7

0 Answers0