Use Scanner to read a text file - Java File Path IO

Java examples for File Path IO:Scanner

Description

Use Scanner to read a text file

Demo Code


import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class Part1 {
    public static void main(String[] args) throws IOException{ new Part1().start(); }
    public void start() throws IOException {
        File inFile = new File ("src/day04/input.txt");
        Scanner sc = new Scanner (inFile);

        while (sc.hasNext()) {
            String line = sc.nextLine();

            System.out.print( line.substring(0, (line.indexOf('[')-3) ) +"  :  ");
            System.out.print( line.substring((line.indexOf('[')-3), line.indexOf('[') ) +"  :  ");
            System.out.println( line.substring(line.indexOf('['), line.length() ));
        }/*w  w  w .  j  a va2s  .  c om*/

        System.out.println(" ans = ");
    }
}

Related Tutorials