我有一个看起来像这样的文本文件:
Aaaaa 0.55 2
bbb 2.1 0.25
ccccc 71 21
..... ... ...
ZzZz 0.725 42.92
Total: 746.54
我正在尝试获取每一列并将其放在每列的 String[] 中。
readprod[]
将是第一列,readcant[]
第二列和readpret[]
第三列。
String[] readprod;
String[] readcant;
String[] readpret;
int nrz;
int lineCount;
String[] columns;
nrz = 0;
File root = android.os.Environment
.getExternalStorageDirectory();
File dir = new File(root.getAbsolutePath() + "/.InterSRL");
dir.mkdirs();
final File file = new File(dir, str);
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
lineCount = 0;
while ((line = br.readLine()) != null) {
lineCount++;
}
} catch (IOException e) {
// You'll need to add proper error handling here
}
readprod = new String[lineCount];
readpret = new String[lineCount];
readcant = new String[lineCount];
// Read text from file
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
text.append('\n');
columns = line.split("\\s+");
readprod[nrz] = columns[0];
readcant[nrz] = columns[1];
readpret[nrz] = columns[2];
nrz++;
}
问题是我明白java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
了readcant[nrz] = columns[1];
。
有什么问题 ?有没有更好的方法从文本文件中读取列并将它们放在每列的不同字符串或数组列表中?
另外:当它们之间有超过3个空格时,有没有办法分割它们?我刚刚意识到第一列,带有名称的列可以有一个值,例如,Fish Sticks 200g
..damn ...
编辑:我这样做了:
columns = line.split("\\s+");
readprod[nrz] = columns[0];
Log.d("Listener", "Columns[0]= "+columns[0]);
readcant[nrz] = columns[1];
Log.d("Listener", "Columns[1]= "+columns[1]);
readpret[nrz] = columns[2];
Log.d("Listener", "Columns[2]= "+columns[2]);
nrz++;
这就是我得到的:
07-13 01:55:43.570: D/Listener(15805): Columns[0]= Bulion
07-13 01:55:43.570: D/Listener(15805): Columns[1]= 1
07-13 01:55:43.570: D/Listener(15805): Columns[2]= 2.25
07-13 01:55:43.570: D/Listener(15805): Columns[0]=
07-13 01:55:43.570: D/AndroidRuntime(15805): Shutting down VM
07-13 01:55:43.570: W/dalvikvm(15805): threadid=1: thread exiting with uncaught exception (group=0x40d42930)
07-13 01:55:43.575: E/AndroidRuntime(15805): FATAL EXCEPTION: main
07-13 01:55:43.575: E/AndroidRuntime(15805): java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
07-13 01:55:43.575: E/AndroidRuntime(15805): at com.example.testlayout.Administrare$1.onItemClick(Administrare.java:161)
07-13 01:55:43.575: E/AndroidRuntime(15805): at android.widget.AdapterView.performItemClick(AdapterView.java:298)
07-13 01:55:43.575: E/AndroidRuntime(15805): at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
07-13 01:55:43.575: E/AndroidRuntime(15805): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
07-13 01:55:43.575: E/AndroidRuntime(15805): at android.widget.AbsListView$1.run(AbsListView.java:3423)
07-13 01:55:43.575: E/AndroidRuntime(15805): at android.os.Handler.handleCallback(Handler.java:725)
07-13 01:55:43.575: E/AndroidRuntime(15805): at android.os.Handler.dispatchMessage(Handler.java:92)
07-13 01:55:43.575: E/AndroidRuntime(15805): at android.os.Looper.loop(Looper.java:137)
07-13 01:55:43.575: E/AndroidRuntime(15805): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-13 01:55:43.575: E/AndroidRuntime(15805): at java.lang.reflect.Method.invokeNative(Native Method)
07-13 01:55:43.575: E/AndroidRuntime(15805): at java.lang.reflect.Method.invoke(Method.java:511)
07-13 01:55:43.575: E/AndroidRuntime(15805): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-13 01:55:43.575: E/AndroidRuntime(15805): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-13 01:55:43.575: E/AndroidRuntime(15805): at dalvik.system.NativeStart.main(Native Method)
所以问题是当他到达第二排时..