Answer the question
In order to leave comments, you need to log in
How to find a line in a txt file?
There is a txt file. in it:
1 Sasha 0 Petya 07/12/2015 17
2 Serezha 0 Vitya 08/17/2015 13
3 Igor 3 Sonya 04/24/2011 16
:
How to search in a file, let's say by date? or by number?
And output the entire line?
line.split is a space.
Answer the question
In order to leave comments, you need to log in
File file = new File("src/main/resources/textfile.txt");
Scanner scanner = new Scanner(file);
try {
while(scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] cols = line.split(" ");
if (cols[4].equals("17.08.2015")) {
System.out.println(line);
}
}
} finally {
scanner.close();
}
Parse into a digestible data structure, and then work with this structure. Apparently, your format is custom, so all sorts of standard parsing for all sorts of xml and json are unlikely to help.
We read line by line, split by the separator -> output the result where necessary.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question