我很确定我只是在问一个愚蠢的问题,但是,他说有一个txt文件,上面有这个数据
UniPub; 112 Binara St; ACT
MooseHeads; 54 Cohen St; ACT
Cube; 24 Mawson St; ACT
我使用以下代码在我的应用程序中阅读:
package au.edu.canberra.g30813706;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import android.app.Activity;
import android.os.Environment;
public class FileReader extends Activity{{
ArrayList<String> sInfo = new ArrayList<String>();
String txtName = "AccomodationTxt.txt";
File root = Environment.getExternalStorageDirectory();
File path = new File(root, "CanberraTourism/" + txtName);
try {
BufferedReader br = new BufferedReader (
new InputStreamReader(
new FileInputStream(path)));
String line;
String[] saLineElements;
while ((line = br.readLine()) != null)
{
//The information is split into segments and stored into the array
saLineElements = line.split(";");
for (int i = 0; i < saLineElements.length; i++)
sInfo.add(saLineElements[i]);
//sInfo.addAll(Arrays.asList(saLineElements[0], saLineElements[1], saLineElements[3]));
}
br.close();
}
catch (FileNotFoundException e) {
System.err.println("FileNotFoundException: " + e.getMessage());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}}
}
我如何区分数组中的行?
例如
我想在一个页面上显示名称的文字,所以只有
UniPub
MooseHeads
立方体
答案 0 :(得分:1)
如何将String数组添加到ArrayList而不是单个元素 像这样:
ArrayList<String[]> sInfo = new ArrayList<String[]>();
String line;
String[] saLineElements;
while ((line = br.readLine()) != null)
{
//The information is split into segments and stored into the array
saLineElements = line.split(";");
sInfo.add(saLineElements);
}
然后你可以遍历sInfo并使用sInfo中每个数组中的第一个元素。