java将文件读取到具有多个数据类型的arraylist
我无法从文本文件读取到arraylist。问题是我不知道如何读取多种类型,因为在我的arraylist中有点、字符串、布尔,因此linesplit不起作用。我检查了所有的主题,没有找到解决方案
编辑: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;
}
# 1 楼答案
由于您可能不知道正则表达式,我将使用:
获取
x
和y
:断点表示法与其他表示法不同
secondpart
李>使用Integer.parseInt()等转换字符串表示形式
重新构造对象
注意:写得匆忙,检查用于x、y和secondpart的索引是否正确。 我还假设输入是正确的