有点困惑
我有一个格式如下的文本文件
FooBoo,男,20,10/04/1988
例如,我知道如何拆分字符串
public loadData() {
String filepath = "G:\\Documents\\MEMove\\XXClients\\data.txt";
BufferedReader bufReader = new BufferedReader(new FileReader(filepath));
String line = bufReader.readLine();
while (line != null) {
// String[] parts = line.split("/");
String[] parts = line.split(",");
String part1 = parts[0];
String part2 = parts[1];
int part3 = Integer.parseInt(parts[3]);
String [] sDOB = line.split("/");
int sDOB1 = Integer.parseInt(sDOB[4]);
People nPeople = new People(part1,part2,part3,sDOB1);
readPeopleList.add(nPeople);
line = bufReader.readLine();
} //end of while
bufReader.close();
for(People per: readPeopleList)
{
System.out.println("Reading.." + per.getFullName());
}
}// end of method
问题是我如何拆分 DOB/它不起作用,因为我收到 NumberFormatException 错误
任何想法
谢谢
最佳答案
首先,使用分隔符","
分割行:
String line = "FooBoo,male,20,10/04/1988";
String[] parts = line.split(",");
然后,使用分隔符“\”
分割最后一部分:
String dob = parts[parts.length - 1];
String[] sDob = dob.split("/");
for (String s : sDob) {
System.out.println(s);
}
编辑:您可以将 sDob
转换为 ArrayList
,如下所示:
ArrayList<String> sDobList = new ArrayList<>(Arrays.asList(sDob));
关于java - 从文本文件读取时如何分割,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46097340/