我想从文件中逐行读取字符串
每行是由两个标签分隔的两个整数,行尾为\n
字符,如notepad++
以下是我从此文件中读取的代码:
public void btnReadInSituValsCmprAct_click(View view){
String[] arr = new String[2];
String rejoinAfterSplit = "";
try {
instream = new FileInputStream(mRcordFilePath);
if (instream != null) {
// prepare the file for reading
InputStreamReader inputreader = new InputStreamReader(instream);
BufferedReader buffreader = new BufferedReader(inputreader);
String line;
// read every line of the file into the line-variable, on line at the time
do {
line = buffreader.readLine();
// arr = line.split(" ");
arr = line.trim().split("\\s+");
rejoinAfterSplit += arr[0] +"\t\t" + arr[1] + "\n";
// rejoinAfterSplit += line + "\n" ;
} while (line != null);
textViewMiscDispCmprAct.setText(rejoinAfterSplit);
// textViewMiscDispCmprAct.setText("TEST");
}
instream.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} // btnReadInSituValsCmprAct_click CLOSED
执行此函数时,出现以下错误:
01-30 17:19:51.981: E/AndroidRuntime(14211): FATAL EXCEPTION: main
01-30 17:19:51.981: E/AndroidRuntime(14211): Process: com.example.fourcommandmenu, PID: 14211
01-30 17:19:51.981: E/AndroidRuntime(14211): java.lang.IllegalStateException: Could not execute method of the activity
01-30 17:19:51.981: E/AndroidRuntime(14211): at android.view.View$1.onClick(View.java:3823)
01-30 17:19:51.981: E/AndroidRuntime(14211): at android.view.View.performClick(View.java:4438)
01-30 17:19:51.981: E/AndroidRuntime(14211): at android.view.View$PerformClick.run(View.java:18422)
01-30 17:19:51.981: E/AndroidRuntime(14211): at android.os.Handler.handleCallback(Handler.java:733)
01-30 17:19:51.981: E/AndroidRuntime(14211): at android.os.Handler.dispatchMessage(Handler.java:95)
01-30 17:19:51.981: E/AndroidRuntime(14211): at android.os.Looper.loop(Looper.java:136)
01-30 17:19:51.981: E/AndroidRuntime(14211): at android.app.ActivityThread.main(ActivityThread.java:5001)
01-30 17:19:51.981: E/AndroidRuntime(14211): at java.lang.reflect.Method.invokeNative(Native Method)
01-30 17:19:51.981: E/AndroidRuntime(14211): at java.lang.reflect.Method.invoke(Method.java:515)
01-30 17:19:51.981: E/AndroidRuntime(14211): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
01-30 17:19:51.981: E/AndroidRuntime(14211): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
01-30 17:19:51.981: E/AndroidRuntime(14211): at dalvik.system.NativeStart.main(Native Method)
01-30 17:19:51.981: E/AndroidRuntime(14211): Caused by: java.lang.reflect.InvocationTargetException
01-30 17:19:51.981: E/AndroidRuntime(14211): at java.lang.reflect.Method.invokeNative(Native Method)
01-30 17:19:51.981: E/AndroidRuntime(14211): at java.lang.reflect.Method.invoke(Method.java:515)
01-30 17:19:51.981: E/AndroidRuntime(14211): at android.view.View$1.onClick(View.java:3818)
01-30 17:19:51.981: E/AndroidRuntime(14211): ... 11 more
01-30 17:19:51.981: E/AndroidRuntime(14211): Caused by: java.lang.NullPointerException
01-30 17:19:51.981: E/AndroidRuntime(14211): at com.example.fourcommandmenu.CompressionActivity.btnReadInSituValsCmprAct_click(CompressionActivity.java:92)
01-30 17:19:51.981: E/AndroidRuntime(14211): ... 14 more
主要错误是
01-30 17:19:51.981: E/AndroidRuntime(14211): at android.view.View$1.onClick(View.java:3818)
01-30 17:19:51.981: E/AndroidRuntime(14211): ... 11 more
01-30 17:19:51.981: E/AndroidRuntime(14211): Caused by: java.lang.NullPointerException
01-30 17:19:51.981: E/AndroidRuntime(14211): at com.example.fourcommandmenu.CompressionActivity.btnReadInSituValsCmprAct_click(CompressionActivity.java:92)
有问题的行是
arr = line.trim().split("\\s+");
我尝试过这一行的变体,例如arr = line.split(" ");
但它们不起作用。我已经验证该程序最多line = buffreader.readLine();
,按预期工作。
我该如何解决这个问题?请帮忙。谢谢。
--- EDIT1 --- :
这是更新的功能:
public void btnReadInSituValsCmprAct_click(View view){
String[] arr = new String[2];
String rejoinAfterSplit = "";
try {
instream = new FileInputStream(mRcordFilePath);
if (instream != null) {
// prepare the file for reading
InputStreamReader inputreader = new InputStreamReader(instream);
BufferedReader buffreader = new BufferedReader(inputreader);
String line ="";
// read every line of the file into the line-variable, on line at the time
/*
do {
line = buffreader.readLine();
arr = line.split(" ");
// arr = line.split("\\t\\t");
// arr = line.trim().split("\\t\\t");
rejoinAfterSplit += arr[0] +"\t\t" + arr[1] + "\n";
// rejoinAfterSplit += line + "\n" ;
} while (line != null);
*/
while (line != null){
line = buffreader.readLine();
arr = line.split(" ");
// arr = line.split("\\t\\t");
// arr = line.trim().split("\\t\\t");
rejoinAfterSplit = rejoinAfterSplit + (arr[0] +"\t\t" + arr[1] + "\n");
// rejoinAfterSplit += line + "\n" ;
}
textViewMiscDispCmprAct.setText(rejoinAfterSplit);
// textViewMiscDispCmprAct.setText("TEST");
}
instream.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} // btnReadInSituValsCmprAct_click CLOSED
} // CompressionActivity extends ActionBarActivity CLOSED
现在我收到一个ArrayIndexOutOfBounds错误:
01-30 17:39:03.661: E/AndroidRuntime(15324): FATAL EXCEPTION: main
01-30 17:39:03.661: E/AndroidRuntime(15324): Process: com.example.fourcommandmenu, PID: 15324
01-30 17:39:03.661: E/AndroidRuntime(15324): java.lang.IllegalStateException: Could not execute method of the activity
01-30 17:39:03.661: E/AndroidRuntime(15324): at android.view.View$1.onClick(View.java:3823)
01-30 17:39:03.661: E/AndroidRuntime(15324): at android.view.View.performClick(View.java:4438)
01-30 17:39:03.661: E/AndroidRuntime(15324): at android.view.View$PerformClick.run(View.java:18422)
01-30 17:39:03.661: E/AndroidRuntime(15324): at android.os.Handler.handleCallback(Handler.java:733)
01-30 17:39:03.661: E/AndroidRuntime(15324): at android.os.Handler.dispatchMessage(Handler.java:95)
01-30 17:39:03.661: E/AndroidRuntime(15324): at android.os.Looper.loop(Looper.java:136)
01-30 17:39:03.661: E/AndroidRuntime(15324): at android.app.ActivityThread.main(ActivityThread.java:5001)
01-30 17:39:03.661: E/AndroidRuntime(15324): at java.lang.reflect.Method.invokeNative(Native Method)
01-30 17:39:03.661: E/AndroidRuntime(15324): at java.lang.reflect.Method.invoke(Method.java:515)
01-30 17:39:03.661: E/AndroidRuntime(15324): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
01-30 17:39:03.661: E/AndroidRuntime(15324): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
01-30 17:39:03.661: E/AndroidRuntime(15324): at dalvik.system.NativeStart.main(Native Method)
01-30 17:39:03.661: E/AndroidRuntime(15324): Caused by: java.lang.reflect.InvocationTargetException
01-30 17:39:03.661: E/AndroidRuntime(15324): at java.lang.reflect.Method.invokeNative(Native Method)
01-30 17:39:03.661: E/AndroidRuntime(15324): at java.lang.reflect.Method.invoke(Method.java:515)
01-30 17:39:03.661: E/AndroidRuntime(15324): at android.view.View$1.onClick(View.java:3818)
01-30 17:39:03.661: E/AndroidRuntime(15324): ... 11 more
01-30 17:39:03.661: E/AndroidRuntime(15324): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
01-30 17:39:03.661: E/AndroidRuntime(15324): at com.example.fourcommandmenu.CompressionActivity.btnReadInSituValsCmprAct_click(CompressionActivity.java:107)
01-30 17:39:03.661: E/AndroidRuntime(15324): ... 14 more
数组索引错误位于
行 rejoinAfterSplit = rejoinAfterSplit + (arr[0] +"\t\t" + arr[1] + "\n");
在Edit1之后的代码中。
--- EDIT2 --- :
这是测试line = buffreader.readLine();
是否正常工作的代码
public void btnReadInSituValsCmprAct_click(View view){
String[] arr = new String[2];
String rejoinAfterSplit = "";
int counter = 0;
try {
instream = new FileInputStream(mRcordFilePath);
if (instream != null) {
// prepare the file for reading
InputStreamReader inputreader = new InputStreamReader(instream);
BufferedReader buffreader = new BufferedReader(inputreader);
String line ="";
// read every line of the file into the line-variable, on line at the time
// /*
do {
line = buffreader.readLine();
// arr = line.split(" ");
// arr = line.split("\\t\\t");
// arr = line.trim().split("\\t\\t");
// rejoinAfterSplit += arr[0] +"\t\t" + arr[1] + "\n";
rejoinAfterSplit += line + "\n" ;
} while (line != null);
// */
textViewMiscDispCmprAct.setText(rejoinAfterSplit);
// textViewMiscDispCmprAct.setText("TEST");
}
instream.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} // btnReadInSituValsCmprAct_click CLOSED
从rejoinAfterSplit
的值我可以看到readline()
本身正常工作。但是,如果我取消注释下一行arr = line.split(" ");
,则此行的活动与NullPointerException崩溃。
答案 0 :(得分:0)
使用以下代码:
do {
line = buffreader.readLine();
String[] arr = line.trim().split("\t\t");
for (int i = 0; i < arr.length; i++) {
rejoinAfterSplit += arr[i] + "\n";
}
} while (line != null);
// */
textViewMiscDispCmprAct.setText(rejoinAfterSplit);