读取/写入getFilesDir()中的子文件夹

时间:2018-06-24 20:16:01

标签: java android file read-write

我正在尝试在getFilesDir()创建一个文件夹,并向其中写入/读取文件。由于某些原因,当我尝试读取文件时,没有任何显示。我已经尝试了几种不同的方法,但最终还是可以读写,但是由于某种原因,该文件必须存在于getFilesDir()和子文件夹中才能显示(或从getFilesDir()中删除)从子文件夹):/我不知道是什么原因导致了这种现象,但是任何帮助都会很棒!在此先感谢:)

保存文件

FileOutputStream stream;
                    try {
                        File filesDir = getDir("mynotes", Context.MODE_PRIVATE);
                        File file = new File(filesDir, each_filename);
                        Toast.makeText(getApplicationContext(), file.toString(), Toast.LENGTH_SHORT).show();

                        stream = new FileOutputStream(file);
                        stream.write(bytes);

                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

读取文件

File file = context.getDir("mynotes", Context.MODE_PRIVATE);
    File newFile = new File(file, filename);

    Note note;

    if (newFile.exists()) {
        FileInputStream fis;
        ObjectInputStream ois;

        try {
            fis = context.openFileInput(filename);
            ois = new ObjectInputStream(fis);

            note = (Note) ois.readObject();

            fis.close();
            ois.close();

        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
            return null;
        }

        return note;
    }

0 个答案:

没有答案
相关问题