java - 如何对文件中的文本行块进行排序?
问题描述
我有一个文本文件,其中包含 ID、人名和姓氏、银行余额和帐户创建日期。我不知何故需要按姓氏和平衡对这个文本文件进行排序,但我不知道如何。
ID:41
Name and surname: Andris Lietis
Balance: 30000 Eur
Created: 31.03.2019
ID:33
Name and surname: Rainis Maize
Balance: 2000 Eur
Created: 32.03.2019
ID:34
Name and surname: Akmens Liepins
Balance: 123 Eur
Created: 29.03.2019
ID:35
Name and surname: Karlis Maiznieks
Balance: 32000 Eur
Created: 04.04.2019
所以我首先考虑以某种方式读取该文件,当我接近 ID 行之一时,我读取了接下来的三行并以某种方式将这 4 行存储在某种块/变量中,然后对其他块执行相同的操作。我的朋友提到制作对象和比较,但我对此没有任何经验,当我在谷歌中查找时,这真的让我感到困惑。
public static void Sakartosana(String nosaukums) throws IOException {
br = new BufferedReader(new FileReader(nosaukums));
String s;
//I understand that I need to somehow sort this with selection.sort
ArrayList<String> al = new ArrayList<String>();
while ((s = br.readLine()) != null) {
if (s.startsWith("ID:")) {
}
}
br.close();
}
这就是我逐行阅读的方式,我有很多不同任务的方法来读取这样的文件。
预期的结果是这样的:
ID:41
Name and surname: Andris Lietis
Balance: 30000 Eur
Created: 31.03.2019
ID:34
Name and surname: Akmens Liepins
Balance: 123 Eur
Created: 29.03.2019
ID:35
Name and surname: Karlis Maiznieks
Balance: 32000 Eur
Created: 04.04.2019
ID:33
Name and surname: Rainis Maize
Balance: 2000 Eur
Created: 32.03.2019
解决方案
推荐阅读
- qt - 按类型转换时获取子类 qgraphicsitem 坐标
- windows - 写入时全双工命名管道锁定
- java - 如何在从杰克逊的字符串构造 JsonNode 时更改它的值
- python - 将不同的数据框分配给不同的变量
- kubernetes - 找不到所需的 GKE pod,谷歌云作曲家
- java - 如何在不从 Swing 继承的情况下正确“分层”图像(并更改源)
- java - 如何更改 JScrollPane 中滚动条的颜色?
- sql - SQL:使用从选择 SQL 中提取的值连接表
- automation - 使用自动化问题使窗口保持活动/打开
- html - 如何使字体很棒以在实时网站上显示