FileNotFoundException :(没有这样的文件或目录)FileOutputStream

时间:2018-01-31 17:14:56

标签: android file object save fileoutputstream

我正在尝试使用FileOutputStream

将对象保存到文件中

不断收到FileNotFoundException错误

这是我的代码 -

    public static Library getFavoritesLibrary() throws IOException, ClassNotFoundException {

    // read object from file
    FileInputStream fis = new FileInputStream("favlibrary.ser");
    ObjectInputStream ois = new ObjectInputStream(fis);
    Library result = (Library) ois.readObject();
    ois.close();

    return result;
}

public static void setFavoritesLibrary(Station station) throws IOException, ClassNotFoundException {


    Library beforeLibrary = getFavoritesLibrary();
    ArrayList<Station> favStations = beforeLibrary.getStations();
    if (favStations.contains(station)){
        favStations.remove(station);
    } else {
        favStations.add(station);
    }


    // write object to file
    FileOutputStream fos = new FileOutputStream("favlibrary.ser");
    ObjectOutputStream oos = new ObjectOutputStream(fos);
    oos.writeObject(beforeLibrary);
    oos.close();

}

尝试过mkdirs() - 这总是被忽略......

也尝试了这个 -

        File file = new File(getApplicationContext().getFilesDir(), "favlibrary.ser");

我也要求获得权限......

我该怎么办?

0 个答案:

没有答案