首页 > 解决方案 > java从文件中读取特定行

问题描述

我有一个包含重复部分的巨大 txt 文件,从 HouseName 开始,到 RentPrice 结束,如下文所示:

[04:04:05s] [startedRetrieving]random useless text
[04:04:05s] [startedRetrieving]random useless text
[04:04:05s] [startedRetrieving]random useless text
[04:04:04s] [startedRetrieving]getHouseName: house1
[04:04:04s] [startedRetrieving]random useless text
[04:04:04s] [startedRetrieving]price (in doll) [min: 1000, max: 1200]
[04:04:05s] [startedRetrieving]squaremtr (in doll) [min: 75, max:85]
[04:04:04s] [startedRetrieving]sellVal (in doll) [min: 1000, max: 1200]
[04:04:05s] [startedRetrieving]random useless text
[04:04:05s] [startedRetrieving]random useless text
[04:04:05s] [startedRetrieving]random useless text
[04:04:05s] [startedRetrieving]rentPrice(in doll) [min: 150, max: 200]
[04:06:04s] [startedRetrieving]getHouseName: house2
[04:06:04s] [startedRetrieving]price(in doll) [min: 1004, max 1100]
[04:06:04s] [startedRetrieving]squaremtr(in doll) [min: 85, max 99]
[04:06:04s] [startedRetrieving]sellVal(in doll) [min: 950, max: 1050]
[04:06:04s] [startedRetrieving]random useless text
[04:06:04s] [startedRetrieving]rentPrice(in doll) [min: 150, max: 290]
[04:09:04s] [startedRetrieving]getHouseName: house3
[04:09:04s] [startedRetrieving]price(in doll) [min: 1099, max: 1200]
[04:09:04s] [startedRetrieving]squaremtr(in doll) [min: 90, max: 110]
[04:09:04s] [startedRetrieving]random useless text
[04:09:04s] [startedRetrieving]random useless text
[04:09:04s] [startedRetrieving]sellVal(in doll) [min: 1100, max: 1300]
[04:09:04s] [startedRetrieving]random useless text
[04:09:04s] [startedRetrieving]rentPrice(in doll) [min: 199, max: 300]

现在,使用扫描仪方法,我正在逐行阅读,我想将每个房间的行附加到字符串中。例如:

[04:04:04s] [startedRetrieving]getHouseName: house1
[04:04:04s] [startedRetrieving]random useless text
[04:04:04s] [startedRetrieving]price (in doll) [min: 1000, max: 1200]
[04:04:05s] [startedRetrieving]squaremtr (in doll) [min: 75, max:85]
[04:04:04s] [startedRetrieving]sellVal (in doll) [min: 1000, max: 1200]
[04:04:05s] [startedRetrieving]random useless text
[04:04:05s] [startedRetrieving]random useless text
[04:04:05s] [startedRetrieving]random useless text
[04:04:05s] [startedRetrieving]rentPrice(in doll) [min: 150, max: 200]

这样,对于每个字符串,我都可以使用特定的正则表达式解析值并检索值。这是我正在使用的代码:

Scanner scan = new Scanner(new File("path//to//file", "UTF-8"));
String string = "";
while(scan.hasNextLine()){
String str = scan.nextLine();
while(str.startsWith("getHouseName" && str.endsWith("rentPrice")){
string = string.append(str);
}
System.out.println(string);
}

但我得到一个空值。我怎样才能保存他文件中特定于每个房子的所有行?非常感谢

编辑:感谢回复,我设法检索了所有内容。我怎样才能分开组?现在我有:

[04:04:04s] [startedRetrieving]getHouseName: house1
[04:04:04s] [startedRetrieving]random useless text
[04:04:04s] [startedRetrieving]price (in doll) [min: 1000, max: 1200]
[04:04:05s] [startedRetrieving]squaremtr (in doll) [min: 75, max:85]
[04:04:04s] [startedRetrieving]sellVal (in doll) [min: 1000, max: 1200]
[04:04:05s] [startedRetrieving]random useless text
[04:04:05s] [startedRetrieving]random useless text
[04:04:05s] [startedRetrieving]random useless text
[04:04:05s] [startedRetrieving]rentPrice(in doll) [min: 150, max: 200]

[04:04:04s] [startedRetrieving]getHouseName: house2
[04:04:04s] [startedRetrieving]random useless text
[04:04:04s] [startedRetrieving]price (in doll) [min: 1000, max: 1200]
[04:04:05s] [startedRetrieving]squaremtr (in doll) [min: 75, max:85]
[04:04:04s] [startedRetrieving]sellVal (in doll) [min: 1000, max: 1200]
[04:04:05s] [startedRetrieving]random useless text
[04:04:05s] [startedRetrieving]random useless text
[04:04:05s] [startedRetrieving]random useless text
[04:04:05s] [startedRetrieving]rentPrice(in doll) [min: 150, max: 200]

[04:04:04s] [startedRetrieving]getHouseName: house3
[04:04:04s] [startedRetrieving]random useless text
[04:04:04s] [startedRetrieving]price (in doll) [min: 1000, max: 1200]
[04:04:05s] [startedRetrieving]squaremtr (in doll) [min: 75, max:85]
[04:04:04s] [startedRetrieving]sellVal (in doll) [min: 1000, max: 1200]
[04:04:05s] [startedRetrieving]random useless text
[04:04:05s] [startedRetrieving]random useless text
[04:04:05s] [startedRetrieving]random useless text
[04:04:05s] [startedRetrieving]rentPrice(in doll) [min: 150, max: 200]

[04:04:04s] [startedRetrieving]getHouseName: house4
[04:04:04s] [startedRetrieving]random useless text
[04:04:04s] [startedRetrieving]price (in doll) [min: 1000, max: 1200]
[04:04:05s] [startedRetrieving]squaremtr (in doll) [min: 75, max:85]
[04:04:04s] [startedRetrieving]sellVal (in doll) [min: 1000, max: 1200]
[04:04:05s] [startedRetrieving]random useless text
[04:04:05s] [startedRetrieving]random useless text
[04:04:05s] [startedRetrieving]random useless text
[04:04:05s] [startedRetrieving]rentPrice(in doll) [min: 150, max: 200]

但我想在 for 循环中单独打印所有内容。我怎样才能做到这一点?

标签: javastringfiletext

解决方案


String string = "";
while(scan.hasNextLine()){
    String str = scan.nextLine();
    if (str.contains("getHouseName")) {
        string = "";
    }
    string = string + str + System.lineSeparator();
    if (str.contains("rentPrice")) {
        System.out.println(string);
        string = "";
    }
}

推荐阅读