如何读取的txt文件某一行某一列
时间:2011-05-05 来源:lucky.dai
info.txt内容
HOUSING_1_Y,8,0c50ea59-60f8-11e0-ab60-001aa02db7e0,1
HOUSING_1_Y,0c50ea59-60f8-11e0-ab60-001aa02db7e0,2
HOUSING_1_Y,0c50ea59-60f8-11e0-ab60-001aa02db7e0,3
HOUSING_1_Y,0c50ea59-60f8-11e0-ab60-001aa02db7e0,4
HOUSING_1_Y,0c50ea59-60f8-11e0-ab60-001aa02db7e0,5
HOUSING_1_Y,0c50ea59-60f8-11e0-ab60-001aa02db7e0,6
HOUSING_1_Y,0c50ea59-60f8-11e0-ab60-001aa02db7e0,7
public static void main(String[] args) {
{
String fileName = "c:\\temp\\info.txt";// 要读取的txt文件
String sId;
List<String[]> list = getFileContent(fileName);// 将所有读到的文件放到数组里面
String[] ary = list.get(0);// 获取数组的第1行HOUSING_1_Y,8,0c50ea59-60f8-11e0-ab60-001aa02db7e0,1
for (String str : ary) {
System.out.println(str);输出数组的第1行HOUSING_1_Y,8,0c50ea59-60f8-11e0-ab60-001aa02db7e0,1
System.out.println(str.split(","));
/*输出的值如下:HOUSING_1_Y 8 0c50ea59-60f8-11e0-ab60-001aa02db7e0 1*/
ary=str.split(",");
sId=ary[1];//输出数组的第1行第二列8;
System.out.println(ary[1]);//输出数组的第1行第二列8;
}
//request.setAttribute("id",listForm.s);
return null;
}
private static List<String[]> getFileContent(String fileName)
throws FileNotFoundException, IOException {
File file = new File(fileName);
BufferedReader bf = new BufferedReader(new FileReader(file));
String content = "";
List<String[]> contentList = new ArrayList<String[]>();
while (content != null) {
content = bf.readLine();
System.out.println(content);
if (content == null) {
break;
}
if (!content.trim().equals("")) {
contentList.add(content.trim().split("\\s+"));
}
}
bf.close();
return contentList;
}
}