Android写入/读取抛出FileNotFoundException

时间:2015-05-18 18:23:47

标签: android android-intent filenotfoundexception

我是Android新手,我使用的是Android Studio 1.2。

所以我使用此代码在一个类上写入文件:

 try {
            //registry is input by user when logging in..
                FileOutputStream fOut = openFileOutput(registry+ "d", MODE_APPEND);
                Toast.makeText(getApplicationContext(), "writing in " + registry+"d the value" + disciplina2, Toast.LENGTH_SHORT).show();
               //toast tells me it's writing properly on the correctly named file
                fOut.write(disciplina2.getBytes());
            } catch (FileNotFoundException e) {
                Toast.makeText(getApplicationContext(), "**not found**", Toast.LENGTH_SHORT).show();
            } catch (IOException e) {
                Toast.makeText(getApplicationContext(), "**io exception**", Toast.LENGTH_SHORT).show();
            }

然后在我应该访问的其他课程上,阅读并填写我的列表:

try {
        InputStream inputstream = this.getAssets().open(registry + "d");
        BufferedReader buffer = new BufferedReader(new InputStreamReader(inputstream));

        while(buffer.readLine()!=null) {
            line = buffer.readLine();
            listaDisciplina.add(line);
            Toast.makeText(getApplicationContext(), "discipline " + line, Toast.LENGTH_SHORT).show();
        }
    }
    catch (FileNotFoundException e) {
        Toast.makeText(getApplicationContext(), "**not found** " + registry+"d", Toast.LENGTH_SHORT).show();
    } catch (IOException e) {
        Toast.makeText(getApplicationContext(), "**io excep D**", Toast.LENGTH_SHORT).show();
    }

即使文件名匹配且写入肯定发生在读取之前,它直接进入FileNotFoundException。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您确定要引用正确的文件来读取写操作添加的数据吗?

this.getAssets().open

getAssets()将从您的应用程序的资产中检索文件'夹

我认为您应该使用相应的输入方法

this.openFileInput()