Android: FileReader unexpectedly throwing FileNotFoundException

ぐ巨炮叔叔 提交于 2019-12-25 07:50:08

问题


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:


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

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



来源:https://stackoverflow.com/questions/5452545/android-filereader-unexpectedly-throwing-filenotfoundexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!