有很多可能的方法。其中之一是制作一个对象来保存数据。示例:既然您知道您的数据将始终具有名称、编号、金额和pin,那么您可以创建这样的类:
public class MyData {
private String name;
private String number;
private double amount;
private String pin;
// Add getters and setters below
}
在阅读文本文件时,你可以列出
MyData
try {
BufferedReader reader = new BufferedReader(new FileReader("path\file.txt"));
String line = reader.readLine();
ArrayList myDataList = new ArrayList();
while (line != null) {
String[] dataParts = line.split("|"); // since your delimiter is "|"
MyData myData = new MyData();
myData.setName(dataParts[0]);
myData.setNumber(dataParts[1]);
myData.setAmount(Double.parseDouble(dataParts[2]));
myData.setPin(dataParts[3]);
myDataList.add(myData);
// read next line
line = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
然后您可以使用如下数据:
myDataList.get(0).getName(); // if you want to get the name of line 1
myDataList.get(1).getPin(); // if you want to get the pin of line 2