java - 如何从 .txt 存储用户数据以进行处理?
问题描述
假设我有一个文本文件,其中包含
1个学生证
2-名字
3- 姓氏
4- 城市
5-性别
它们用逗号分隔,每一行信息的顺序都与其他信息相同。
例如,我需要单独存储城市,这样我就可以知道他们是否适合旅行(他们指定了特定的城市)。
文本文件中的示例:
1234567890,莎拉,约翰逊,女,俄勒冈州
1029384756,约翰·皮特,男,坦帕
我使用哪种类型的列表?以及如何分隔该列表中的每个信息?
解决方案
private static class Student{
private long studentId;
private String firstName;
private String lastName;
private String city;
private String gender;
public static Student parseLine(String line){
String[] properties = line.split(", ");
Student student = new Student();
student.studentId = Long.parseLong(properties[0]);
student.firstName = properties[1];
student.lastName = properties[2];
student.city = properties[3];
student.gender = properties[4];
return student;
}
public static Collection<Student> readFile(String path) throws IOException {
return Files.lines(Paths.get(path)).map(Student::parseLine).collect(Collectors.toList());
}
推荐阅读
- three.js - Three.js:如何使用相机旋转值在轴上旋转球体
- python - 如何从具有多个值的 groupby 对象中获取最大值?
- angular - 角 | 不同项目之间的共享模块
- sql - 多个子集的查询优化
- c# - 如何使抛出的物体根据其抛出的方向自动旋转?
- google-apps-script - 设置 SpreadsheetApp.flush(); 和实用程序.sleep(5000); 脚本中的行位置(Google App 脚本)
- string - 在SAS中的数字之后提取字符串
- javascript - 无法在我的正则表达式中转义字符
- c# - 从 SharePoint 读取文件时,“对象引用未设置为服务器上的对象实例。对象与属性文件相关联”
- python - 如何仅使用布尔和数值比较以及 abs 函数找到三个变量的最小值最大值