Jun 24, 2017

Java provides a Scanner class that can be used as a text parser.

The Java class Scanner is used for text parsing.

Scanner implements the AutoCloseable interface so we can use it in try-with-resources block.

It accepts a regular expression as a delimiter and returns tokens separated by the delimiter.


try (Scanner scanner = new Scanner(new File(filename));) {
   
    while (scanner.hasNext()) {
    String word = scanner.next();
    
    System.out.println(word);
    }
}

By setting the delimiter appropriately, various parsing tasks can be accomplished.

scanner.useDelimiter("put regular expression here");