I deserialize an object from a file:
//Считывает из файла и преобразует данные public class FirstSystem implements Runnable { public FileSystem currentFileSystem; @Override public void run() { System.out.println("Работает первая система"); //Считываем из файла1 //Создаем массив цветов ArrayList<Flower> flowers = new ArrayList<>(); //Десериализация объектов из файла и помещаем их в список FileInputStream fis = null; try { fis = new FileInputStream(currentFileSystem.getFile1()); } catch (FileNotFoundException e) { e.printStackTrace(); } ObjectInputStream oin = null; try { oin = new ObjectInputStream(fis); } catch (IOException e) { e.printStackTrace(); } int i = -1; try { while ((i = fis.read())!= -1) { Flower tmp = (Flower) oin.readObject(); flowers.add(tmp); } } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } System.out.println(flowers.size()); } } Deserializable class:
public class Flower implements Serializable { private String nameFlower; private String colorFlower; private int lengthFlower; public Flower() { this.setNameFlower(null); this.setColorFlower(null); this.setLengthFlower(0); } public Flower(String name, String color, int length) { this.setNameFlower(name); this.setColorFlower(color); this.setLengthFlower(length); } public String getNameFlower() { return nameFlower; } public void setNameFlower(String nameFlower) { this.nameFlower = nameFlower; } public String getColorFlower() { return colorFlower; } public void setColorFlower(String colorFlower) { this.colorFlower = colorFlower; } public int getLengthFlower() { return lengthFlower; } public void setLengthFlower(int lengthFlower) { this.lengthFlower = lengthFlower; } public void showFlower() { System.out.println("Информация о цветке:"); System.out.println("Название цветка:" + this.getNameFlower() + "\n" + "Цвет цветка:" + this.getColorFlower() + "\n" + "Длина цветка:" + this.getLengthFlower() + "\n"); } } Class for serialization:
public class FileSystem { private File file1; private File file2; private File file3; public FileSystem() { setFile1(new File("File1.txt")); setFile2(new File("File2.txt")); setFile3(new File("File3.txt")); } public synchronized File getFile1() { return file1; } public void setFile1(File file1) { this.file1 = file1; } public synchronized File getFile2() { return file2; } public void setFile2(File file2) { this.file2 = file2; } public synchronized File getFile3() { return file3; } public void setFile3(File file3) { this.file3 = file3; } public void addInformationToFile1() throws IOException, ClassNotFoundException { Flower flower1 = new Flower("Роза","Красная",20); Flower flower2 = new Flower("Тюльпан","Желтый",14); Flower flower3 = new Flower("Роза","Белая",18); FileOutputStream fos = new FileOutputStream(this.file1); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(flower1); oos.writeObject(flower2); oos.writeObject(flower3); oos.flush(); oos.close(); return; } } An error occurs:
Exception in thread "main" java.lang.ClassCastException: java.io.ObjectStreamClass cannot be cast to com.bsuir.psp.Flower at com.bsuir.psp.FirstSystem.run(FirstSystem.java:39) at com.bsuir.psp.Start.main(Start.java:13) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
while ((i = fis.read())!= -1), you read from the underlying stream of bytes, soObjectInputStreamsees corrupted data and does not work. The problem is that there is no normal way to find out if there are more objects in the stream (just catch anEOFException), so I would suggest writing a list of objects to a file right away and reading it without a loop. - zRrr