我需要将文本行写入文件,然后逐行读取。 因此我使用这些代码写入文件:
String string = "Hello world55555555555!" + "\r\n";
FileOutputStream outputStream;
try {
outputStream = getActivity().openFileOutput(filename, Context.MODE_PRIVATE);
outputStream.write(string.getBytes());
outputStream.flush();
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
return true;
它似乎正常工作。但是当我正在阅读它时会出错!我试图通过这些代码来阅读它:
FileInputStream inputStream = null;
BufferedReader reader;
String[] text = new String[0];
List < String > words = new ArrayList < String > ();
String line;
try {
inputStream = getActivity().openFileInput(filename);
reader = new BufferedReader(new InputStreamReader(inputStream));
while ((line = reader.readLine()) != null) {
words.add(line);
}
text = new String[words.size()];
text = words.toArray(text);
} catch (IOException e) {
//You'll need to add proper error handling here
}
tv.setText(text[0]);
Toast.makeText(getActivity(), text[1],
Toast.LENGTH_SHORT).show();
}
任何评论!请!
答案 0 :(得分:0)
感谢您的考虑,我将阅读代码更新为:
FileInputStream inputStream = null;
BufferedReader reader;
StringBuilder[] total = new StringBuilder[]{};
String line;
try{
inputStream = new FileInputStream(filename);
reader = new BufferedReader(new InputStreamReader(inputStream));
int j =0;
while ((line = reader.readLine()) != null) {
total[j].append(line);
j++;
}
}
catch(IOException e){
//You'll need to add proper error handling here
}
tv.setText(total[0]);
Toast.makeText(getActivity(),total[1],
Toast.LENGTH_SHORT).show();
}
但我收到错误并且app关闭了:
10-18 10:36:50.700 3087-3087/? D/AndroidRuntime﹕ Shutting down VM
10-18 10:36:50.700 3087-3087/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: ir.siavashon.sunshine, PID: 3087
java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
at ir.siavashon.sunshine.MainActivityFragment.onClick(MainActivityFragment.java:140)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
10-18 10:36:50.700 1342-2903/? W/ActivityManager﹕ Force finishing activity ir.siavashon.sunshine/.MainActivity
10-18 10:36:50.710 993-993/? D/gralloc﹕ Registering a buffer in the process that created it. This may cause memory ordering problems.
10-18 10:36:50.710 993-993/? E/libEGL﹕ called unimplemented OpenGL ES API
10-18 10:36:50.710 993-993/? E/SurfaceFlinger﹕ glCheckFramebufferStatusOES error -906983389
10-18 10:36:50.710 993-993/? E/SurfaceFlinger﹕ got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot
10-18 10:36:50.730 1342-3103/? W/DropBoxManagerService﹕ Dropping: data_app_crash (848 > 0 bytes)
10-18 10:36:51.220 1342-1356/? W/ActivityManager﹕ Activity pause timeout for ActivityRecord{a21f483 u0 ir.siavashon.sunshine/.MainActivity t897 f}
10-18 10:36:52.190 1656-1656/? I/Choreographer﹕ Skipped 55 frames! The application may be doing too much work on its main thread.
10-18 10:36:53.330 3087-3087/? I/Process﹕ Sending signal. PID: 3087 SIG: 9
10-18 10:36:53.430 1342-1372/? W/InputDispatcher﹕ channel 'cb217a9 ir.siavashon.sunshine/ir.siavashon.sunshine.MainActivity (server)' ~ Consumer closed input channel or an error occurred. events=0x9
10-18 10:36:53.430 1342-1372/? E/InputDispatcher﹕ channel 'cb217a9 ir.siavashon.sunshine/ir.siavashon.sunshine.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
10-18 10:36:53.430 1342-2903/? I/ActivityManager﹕ Process ir.siavashon.sunshine (pid 3087) has died
10-18 10:36:53.590 1342-1361/? W/AppOps﹕ Finishing op nesting under-run: uid 1000 pkg android code 24 time=0 duration=0 nesting=0
10-18 10:36:53.600 1342-1490/? I/WindowState﹕ WIN DEATH: Window{cb217a9 u0 ir.siavashon.sunshine/ir.siavashon.sunshine.MainActivity}
10-18 10:36:53.600 1342-1490/? W/InputDispatcher﹕ Attempted to unregister already unregistered input channel 'cb217a9 ir.siavashon.sunshine/ir.siavashon.sunshine.MainActivity (server)'