At attempt to deserialize the data such error takes off. It flies in the following line:

ObjectInputStream ois = new ObjectInputStream(fis); 

But the code of the entire method (bookPrefs - the SharedPreferences, books - ArrayList with objects of the custom, serializable class.):

 void loadBooks(){ StringBuilder bytes = new StringBuilder(); // FIXME: 06.03.2017 Здесь тоже убрать создание объекта. //Файл для десериализации File f = new File(PATH_TO_DESERIALIZABLE_DATA); bookPrefs = getActivity().getPreferences(MODE_PRIVATE); bytes.append(bookPrefs.getString(PREFS, "")); //Запись байтов в файл try (Writer writer = new OutputStreamWriter(new FileOutputStream(PATH_TO_DESERIALIZABLE_DATA))) { writer.write(bytes.toString()); } catch (FileNotFoundException e){ Toast.makeText(getActivity(), "FileNotFound!", Toast.LENGTH_SHORT).show(); e.printStackTrace(); } catch (IOException e){ Toast.makeText(getActivity(), "IOException with load", Toast.LENGTH_SHORT).show(); e.printStackTrace(); } //Десериализация try { FileInputStream fis = new FileInputStream(PATH_TO_DESERIALIZABLE_DATA); // FIXME: 06.03.2017 ДЕСЕРИАЛИЗАЦИЯ ObjectInputStream ois = new ObjectInputStream(fis); books = (ArrayList<Book>) ois.readObject(); } catch (ClassNotFoundException e){ Toast.makeText(getActivity(), "Class nf", Toast.LENGTH_SHORT).show(); e.printStackTrace(); } catch (IOException e){ Toast.makeText(getActivity(), "IOException with deser", Toast.LENGTH_SHORT).show(); e.printStackTrace(); } } 
  • one
    Try to use for writing ObjectOutputStream - Pavel Parshin

1 answer 1

From the documentation:

An ObjectInputStream deserializes primitive data and objects previously written using an ObjectOutputStream.

You write using OutputStreamWriter , this causes an exception, since the header of the read data does not correspond to the expected one.

  • Those. Do I need to use an ObjectOuputStream? But then he swears that I am not giving him the stream - Nick
  • Change my code properly for I do not understand how to fix it. what would be a mistake gone - Nick