我在从文本文件读取到数组列表时遇到问题。问题是我不知道如何读取多种类型,因为在我的数组列表中有点、字符串、布尔值,因此线拆分不起作用。我检查了所有主题,但没有找到解决方案。
编辑:Elrendezes 类看起来像
class Elrendezes {
protected Point Po;
protected String hely;
protected String foglalo;
protected boolean foglalt;
}
这是我的文件的样子:
java.awt.Point[x=16,y=13], 1, name1, false
阅读方法是
public static ArrayList<Elrendezes> readDataFromFile(){
ArrayList<Elrendezes> ElrList = new ArrayList<Elrendezes>();
FileInputStream fstream = null;
try
{
fstream = new FileInputStream("src/files/DataFile.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String strLine = null ;
String tokens[] = strLine.split(", ");
while ((strLine = br.readLine()) != null) {
tokens = strLine.split(", ");
// THIS DOES NOT WORK: ElrList.add(new Elrendezes(tokens[0], tokens[1], tokens[2], tokens[3]));
}
}
catch (IOException e) {
e.printStackTrace();
}
finally {
try { fstream.close(); } catch ( Exception ignore ) {}
}
return ElrList;
}