显然的是不是,首先我们需要读取这个文件里的内容,这里每一行都是一条数据,我们可以用bufferedreader去读取行
其次,读完之后还要对每一条数据(行)处理一下,这个用string.split,根据空格分离字符串就行了
那么之后就是根据这得到的string[]封装成Model对象了
我们大概实现如下:
// 读取文件
public static List<String> readLines(String filePath) throws Exception {
File file = new File(filePath);
BufferedReader reader = new BufferedReader(new FileReader(file));
String lineStr;
List<String> strList = new ArrayList<String>();
while (null != (lineStr = reader.readLine())) {
strList.add(lineStr);
}
reader.close();
return strList;
}// 封装数据
public static Model convertToModel(String s) throws Exception{
String[] strings = s.split(" "); // 以空格分离
try {
Model model = new Model();
model.first = strings[0];
model.second = strings[1];
model.third = strings[2];
model.fourth = new SimpleDateFormat("yyyy-MM-dd").parse(strings[3]);
return model;
} catch (Exception e) {
throw e;
}
}// 测试函数,无异常即读取成功
public static void main(String[] args) {
String filePath = "C:/Users/YSS36/Desktop/test.dat";
try {
List<String> strs= readLines(filePath);
List<Model> models = new ArrayList<Model>();
for (String string : strs) {
models.add(convertToModel(string));
}
System.out.println(models);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}// Model
static class Model{
public String first;
public String second;
public String third;
public Date fourth;
}