尝试读取文件java时失败

时间:2014-03-17 08:39:04

标签: java

你可以帮我解决这个问题: 我正在开发一个Android应用程序,我需要一个文件从类中读取。它的代码如下:

                FileInputStream fis = null;
                String collected = null;

                try {
                        fis = openFileInput(title);
                        Log.d("FilePATH", path + "/" + title);
                        byte[] dataArray = new byte[fis.available()];
                        while(fis.read(dataArray) != -1){
                                collected = new String(dataArray);
                        }
                } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } finally {
                        try {

                                fis.close();
                                textData.setText(collected);
                        } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }
                }

它无法进入try,然后在fis.close()处抛出异常。 我能解决这个问题吗?

堆栈跟踪

03-17 10:59:12.888: E/AndroidRuntime(15142): FATAL EXCEPTION: main
03-17 10:59:12.888: E/AndroidRuntime(15142): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.victwo/com.example.victwo.NewNoteActivity}: java.lang.NullPointerException
03-17 10:59:12.888: E/AndroidRuntime(15142):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
03-17 10:59:12.888: E/AndroidRuntime(15142):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-17 10:59:12.888: E/AndroidRuntime(15142):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-17 10:59:12.888: E/AndroidRuntime(15142):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-17 10:59:12.888: E/AndroidRuntime(15142):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-17 10:59:12.888: E/AndroidRuntime(15142):    at android.os.Looper.loop(Looper.java:123)
03-17 10:59:12.888: E/AndroidRuntime(15142):    at android.app.ActivityThread.main(ActivityThread.java:3683)
03-17 10:59:12.888: E/AndroidRuntime(15142):    at java.lang.reflect.Method.invokeNative(Native Method)
03-17 10:59:12.888: E/AndroidRuntime(15142):    at java.lang.reflect.Method.invoke(Method.java:507)
03-17 10:59:12.888: E/AndroidRuntime(15142):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-17 10:59:12.888: E/AndroidRuntime(15142):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-17 10:59:12.888: E/AndroidRuntime(15142):    at dalvik.system.NativeStart.main(Native Method)
03-17 10:59:12.888: E/AndroidRuntime(15142): Caused by: java.lang.NullPointerException
03-17 10:59:12.888: E/AndroidRuntime(15142):    at com.example.victwo.NewNoteActivity.onCreate(NewNoteActivity.java:66)
03-17 10:59:12.888: E/AndroidRuntime(15142):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-17 10:59:12.888: E/AndroidRuntime(15142):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
03-17 10:59:12.888: E/AndroidRuntime(15142):    ... 11 more

1 个答案:

答案 0 :(得分:0)

首次例外(内部尝试)

只显示代码,它不会无法进入尝试,它可能会在发生任何事情之前在尝试中抛出异常,即

fis = openFileInput(title);

可能会抛出exception然后跳进catch然后finally

第二例外(最终内部)

你看到的最终内容是由fis null造成的,只是在尝试用它做某事之前检查它

 if (fis != null)
 {
   fis.close();
 }

这是因为close()是对象fis的一种方法,但由于在创建fis时存在异常,因此它不存在,因此在某事物上调用close()不存在会引发NullPointerException

<强>另外

如果您使用的是Java 7,我建议您最终删除您的资源并使用try-with-resources,这样您就不需要处理它导致的关闭和异常。

try (FileInputStream fis = openFileInput(title))
{
//your code and catches
}
//no finally block required

然后设置你的文字

textData.setText(collected);