java从文件中读取特定行

t5zmwmid  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(271)

我有一个巨大的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]

现在,使用scanner方法,我逐行读取,并将每个房间的行附加到一个字符串中。例如:

[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]

通过这种方式,对于每个字符串,我可以使用特定的regex解析值并检索值。下面是我使用的代码:

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循环。我该怎么做?

fcy6dtqo

fcy6dtqo1#

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 = "";
    }
}

相关问题