我需要您的帮助,因为我想在文件中写入对象并读回。

我对以下代码行有疑问:Vehicule result = (Vehicule) read.readObject ();

try {
       List<Vehicule> voiture = new ArrayList<Vehicule>();

       //Write object to file
       FileOutputStream fos = new FileOutputStream("Garage.ser");
       ObjectOutputStream ecriture = new ObjectOutputStream(fos);
       System.out.println("\nIci - Ecriture de mon fichier\n");
       ecriture.writeObject(voiture);
       ecriture.close();
       //read object from file
       FileInputStream fis = new FileInputStream("Garage.ser");
       ObjectInputStream lecture = new ObjectInputStream(fis);
        System.out.println("\nIci - Lecture du fichier\n");
        Vehicule result = (Vehicule) lecture.readObject();
        lecture.close();

   }catch (FileNotFoundException e){
       System.out.println("Aucune voitures sauvegardé !");
   }catch (IOException e) {
       e.printStackTrace();
   }catch (ClassNotFoundException e) {
       e.printStackTrace();
   }


感谢您的帮助。

最佳答案

看来您在写List<Vehicule>,但读了Vehicule

10-04 21:12