无法打开Android文本文件

时间:2015-10-22 19:48:48

标签: android file

我收到错误消息,说我尝试打开的文件不存在。

以下是我用来进行读写的代码

private void writeToFile(String file,String data) {
    try {
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(openFileOutput("saved.txt", Context.MODE_PRIVATE));
        outputStreamWriter.write(data);
        outputStreamWriter.close();
    }
    catch (IOException e) {
        Log.e("Exception", "File write failed: " + e.toString());

    }
}

private String readFromFile(String file) {

    String ret = "";

    try {
        InputStream inputStream = openFileInput(file);

        if ( inputStream != null ) {
            InputStreamReader inputStreamReader = new   InputStreamReader(inputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String receiveString = "";
            StringBuilder stringBuilder = new StringBuilder();

            while ( (receiveString = bufferedReader.readLine()) != null ) {
                stringBuilder.append(receiveString);
            }

            inputStream.close();
            ret = stringBuilder.toString();
        }
    }
    catch (FileNotFoundException e) {
        Log.e("login activity", "File not found: " + e.toString());
    } catch (IOException e) {
        Log.e("login activity", "Can not read file: " + e.toString());
    }

    return ret;
}

我得到的错误是没有找到文件:java.io.FileNotFoundException:/data/data/com.erikkniaz.myapp.myapplication/files/checked.txt:打开失败:ENOENT(没有这样的文件或目录)< / p>

1 个答案:

答案 0 :(得分:1)

错误是:

我得到的错误是找不到文件:java.io.FileNotFoundException:/data/data/com.erikkniaz.myapp.myapplication/files/ checked.txt :open failed:ENOENT(没有这样的文件或目录)

必须使用 saved.txt