Android: FileReader unexpectedly throwing FileNotF

2019-09-18 01:55发布

So I'm unexpectedly receiving a FileNotFoundException. As you can see, shortly before I call the FileReader, I call FileInputStream which works fine. I've tried putting the FileReader in its own Try/Catch clause, but receive the same result. I've gutted most of the lines unnecessary to my question from this block. (Ultimately I call LineNumberReader as well, though I removed it from the block because I'm not even getting that far.)

        String FILENAME = "file.txt";
            try {
                    byte[] buffer = new byte[128];
                    String toStr = new String();
                    TextView view = (TextView)findViewById(R.id.textview);
                    FileInputStream fis = openFileInput(FILENAME); /////File is found successfully here/////
                    fis.read(buffer);
                    fis.close();
                    toStr = new String(buffer);
                    view.append(toStr);
                    FileReader fr = new FileReader(FILENAME); /////FileNotFoundExceptionThrownHere/////
                    /////do stuff here/////
                    fr.close();
                } 
             catch (FileNotFoundException e) {
                    TextView view = (TextView)findViewById(R.id.textview);
                    view.append("file not found!");
                } 
             catch (IOException e) {
                    TextView view = (TextView)findViewById(R.id.textview);
                    view.append("IO error!");
            }

Also, please keep in mind when answering that I'm still somewhat of a novice when it comes to java. I have experience in a couple other languages but java is a bit of a different breed of monster to me. Any help would be tremendously appreciated!

1条回答
萌系小妹纸
2楼-- · 2019-09-18 02:14

openFileInput() and new FileReader() do not take the same parameter.

openFileInput("file.txt") is equivalent to new FileReader(new File(getFilesDir(), "file.txt")).

查看更多
登录 后发表回答