C
C
CheshKin2015-07-01 22:20:14
Java
CheshKin, 2015-07-01 22:20:14

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

4 answer(s)
E
Evhen, 2015-07-02
@CheshKin

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();
  }

G
GavriKos, 2015-07-01
@GavriKos

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.

D
DVamp1r3, 2015-07-01
@DVamp1r3

We read line by line, split by the separator -> output the result where necessary.

A
abs0lut, 2015-07-02
@abs0lut

You can check strings against a regular expression using String.matches

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question