#java #string #file #text
#java #строка #файл #текст
Вопрос:
У меня есть огромный текстовый файл с повторяющимися разделами, начиная с имени дома и заканчивая 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" amp;amp; 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. Как я могу это сделать?
Ответ №1:
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 = "";
}
}
Комментарии:
1. Спасибо! Но это печатает весь файл и заканчивается ценой аренды. Возможно ли распечатать только разделы каждого дома?
2. @thranduil90
string
содержит всю информацию об одном доме. Если вы удалитеSystem.lineSeparator()
, это будет ясно в распечатке.3. Как я могу создать цикл for, чтобы каждый раз печатать дом и его значения?