我已经解决了很多这些问题但仍然无法弄明白。我有一个文本文件分成行。每行由5个由“,”分隔的数据组成。我正在尝试读取此文件并将信息拆分为以下形式的字符串数组:
String [][] xyz = new String [5][100];
请有人帮我解决一个简单的解决方案!?谢谢!!! :)
数据示例:
John,22,1953,Japan,Green
Anna,18,2012,Mexico,Blue
Sam,34,1976,San Francisco,Pink
示例代码:
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();
}
}
错误
07-24 06:26:56.524:E / AndroidRuntime(27203):致命异乎寻常:主要 07-24 06:26:56.524:E / AndroidRuntime(27203):java.lang.ArrayIndexOutOfBoundsException:length = 10;指数= 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):在android.widget.AdapterView.fireOnSelected(AdapterView.java:895) 07-24 06:26:56.524:E / AndroidRuntime(27203):在android.widget.AdapterView.access $ 200(AdapterView.java:50) 07-24 06:26:56.524:E / AndroidRuntime(27203):在android.widget.AdapterView $ SelectionNotifier.run(AdapterView.java:863) 07-24 06:26:56.524:E / AndroidRuntime(27203):在android.os.Handler.handleCallback(Handler.java:615) 07-24 06:26:56.524:E / AndroidRuntime(27203):在android.os.Handler.dispatchMessage(Handler.java:92) 07-24 06:26:56.524:E / AndroidRuntime(27203):在android.os.Looper.loop(Looper.java:137) 07-24 06:26:56.524:E / AndroidRuntime(27203):在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)
答案 0 :(得分:3)
您可以使用BufferedReader读取每一行,然后使用.split(',');
方法拆分该行。
一些伪代码:
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++;
}
希望这有帮助!
答案 1 :(得分:1)
我假设您了解阅读文件的基本知识。那么让我们从你的String s等于一整行开始:
String s = "John,22,1953,Japan,Green";
你可以调用split()函数来返回一个数组,该数组由某个表达式(在本例中为逗号)细分,如下所示:
String[] row = s.split(","); //Returns a new String array {John,22,1953,Japan,Green}
然后你有行[0] =“约翰”,行[1] = 22,依此类推。您可以使用这些数组执行任何操作,包括将它们存储在多维数组中,如果这是您想要执行的操作。
String[][] xyz = new String[numRows][5];
xyz[0] = row;
System.out.println(xyz[0][0]); //prints "John"
希望这很清楚,我理解你正在努力做的事情。
答案 2 :(得分:0)
有关使用缓冲读取器逐行读取文件的示例,请参阅Using BufferedReader.readLine() in a while loop properly,然后将其与Kon的答案结合起来,我认为您有一个解决方案。
AssetManager manger;
String line = null;
List<String[]> xyzList = new ArrayList<String[]>();
String[][] xyz;
InputStream is;
InputStreamReader isr;
BufferedReader br;
try {
manger = getAssets();
is = manager.open("data.txt");
isr = new InputStreamReader(is);
br = new BufferedReader(isr);
while ((line = br.readLine()) != null) {
xyzList.add(line.split(","));
}
xyz = (String[][])xyzList.toArray();
}catch (IOException e1) {
Toast.makeText(getBaseContext(), "Problem!", Toast.LENGTH_SHORT).show();
}finally{
br.close();
isr.close();
is.close();
}
答案 3 :(得分:0)
/* Assuming you want a 5 by n matrix from this data
John,22,1953,Japan,Green
Anna,18,2012,Mexico,Blue
Sam,34,1976,San Francisco,Pink
.
.
.
.
nth row
String[] row = s.split(","); */
for (int i = 0; i<n i++)
{
for(int j = 0; j<originalString.length; j++)
{
xyz[i] = originalString[j].split(",");
}
}