阅读和分割文本文件到一个数组 - 机器人 [英] Read and split text file into an array - Android
问题描述
我已经经历了这些问题负载走了,但仍不能似乎弄明白。我有一个文本文件分割成行。每排由5个由一个,分离的数据的。我试图读取该文件,并分裂成信息字符串数组形式如下:
I have gone through loads of these questions but still cant seem to figure it out. I have a text file split into rows. Each row consists of 5 pieces of data separated by a ",". I am trying to read this file and split the information into a string array in this form:
String [][] xyz = new String [5][100];
请能有人帮我出一个简单的解决方案!?谢谢!!! :)
Please could someone help me out with a simple solution!? Thanks!!! :)
数据举例:
John,22,1953,Japan,Green
Anna,18,2012,Mexico,Blue
Sam,34,1976,San Francisco,Pink
样品code:
Sample Code:
公共无效READFILE(){
public void readFile(){
AssetManager manger;
String line = null;
try {
manger = getAssets();
InputStream is = manger.open("data.txt");
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
while ((line = br.readLine()) != null) {
values.add(line.split(","));
//System.out.print(value);
}
br.close();
String[] array = values.toArray(new String[20];
} catch (IOException e1) {
Toast.makeText(getBaseContext(), "Problem!", Toast.LENGTH_SHORT).show();
}
}
错误
六月7日至24日:26:56.524:E / AndroidRuntime(27203):致命异常:主要 六月7日至24日:26:56.524:E / AndroidRuntime(27203):java.lang.ArrayIndexOutOfBoundsException:长度= 10;指数= 10 六月7日至24日:26:56.524:E / AndroidRuntime(27203):在com.testingReadArray.weebly.testingReadArray.MainActivity.readFile(MainActivity.java:182) 六月7日至24日:26:56.524:E / AndroidRuntime(27203):在com.testingReadArray.weebly.testingReadArray.MainActivity$planOnClickListener.onItemSelected(MainActivity.java:148) 六月7日至24日:26:56.524:E / AndroidRuntime(27203):在android.widget.AdapterView.fireOnSelected(AdapterView.java:895) 六月7日至24日:26:56.524:E / AndroidRuntime(27203):在android.widget.AdapterView.access $ 200(AdapterView.java:50) 六月7日至24日:26:56.524:E / AndroidRuntime(27203):在android.widget.AdapterView $ SelectionNotifier.run(AdapterView.java:863) 六月7日至24日:26:56.524:E / AndroidRuntime(27203):在android.os.Handler.handleCallback(Handler.java:615) 六月7日至24日:26:56.524:E / AndroidRuntime(27203):在android.os.Handler.dispatchMessage(Handler.java:92) 六月7日至24日:26:56.524:E / AndroidRuntime(27203):在android.os.Looper.loop(Looper.java:137) 六月7日至24日:26:56.524:E / AndroidRuntime(27203):在android.app.ActivityThread.main(ActivityThread.java:4867) 六月7日至24日:26:56.524:E / AndroidRuntime(27203):在java.lang.reflect.Method.invokeNative(本机方法) 六月7日至24日:26:56.524:E / AndroidRuntime(27203):在java.lang.reflect.Method.invoke(Method.java:511) 六月7日至24日:26:56.524:E / AndroidRuntime(27203):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1007) 六月7日至24日:26:56.524:E / AndroidRuntime(27203):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774) 六月7日至24日:26:56.524:E / AndroidRuntime(27203):在dalvik.system.NativeStart.main(本机方法)
07-24 06:26:56.524: E/AndroidRuntime(27203): FATAL EXCEPTION: main 07-24 06:26:56.524: E/AndroidRuntime(27203): java.lang.ArrayIndexOutOfBoundsException: length=10; index=10 07-24 06:26:56.524: E/AndroidRuntime(27203): at com.testingReadArray.weebly.testingReadArray.MainActivity.readFile(MainActivity.java:182) 07-24 06:26:56.524: E/AndroidRuntime(27203): at com.testingReadArray.weebly.testingReadArray.MainActivity$planOnClickListener.onItemSelected(MainActivity.java:148) 07-24 06:26:56.524: E/AndroidRuntime(27203): at android.widget.AdapterView.fireOnSelected(AdapterView.java:895) 07-24 06:26:56.524: E/AndroidRuntime(27203): at android.widget.AdapterView.access$200(AdapterView.java:50) 07-24 06:26:56.524: E/AndroidRuntime(27203): at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:863) 07-24 06:26:56.524: E/AndroidRuntime(27203): at android.os.Handler.handleCallback(Handler.java:615) 07-24 06:26:56.524: E/AndroidRuntime(27203): at android.os.Handler.dispatchMessage(Handler.java:92) 07-24 06:26:56.524: E/AndroidRuntime(27203): at android.os.Looper.loop(Looper.java:137) 07-24 06:26:56.524: E/AndroidRuntime(27203): at android.app.ActivityThread.main(ActivityThread.java:4867) 07-24 06:26:56.524: E/AndroidRuntime(27203): at java.lang.reflect.Method.invokeNative(Native Method) 07-24 06:26:56.524: E/AndroidRuntime(27203): at java.lang.reflect.Method.invoke(Method.java:511) 07-24 06:26:56.524: E/AndroidRuntime(27203): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007) 07-24 06:26:56.524: E/AndroidRuntime(27203): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774) 07-24 06:26:56.524: E/AndroidRuntime(27203): at dalvik.system.NativeStart.main(Native Method)
推荐答案
您可以使用一个BufferedReader读取每个行,然后拆分使用就行了 .split('');
方法。
You could use a BufferedReader to read each line and then split the line using the .split(',');
method.
一些伪code:
BufferedReader bfr = new BufferedReader(InputStreamReader/FileReader...);
String line = null;
int index = 0;
String [][] xyz = new String [100][5];
while ( (line = bfr.readLine()) != null) {
if (index < xyz.length) xyz[index] = line.split(",");
index++;
}
希望这有助于!
Hope this helped!
这篇关于阅读和分割文本文件到一个数组 - 机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!