Reading a Text File
Using Scanner

By: Nicholas Duchon

 Code Steps:

  1. create the public class file
  2. add header comments
  3. main
  4. get main to print something
  5. import Scanner, File and FileNotFoundException
  6. put together lines required for new Scanner on a file
  7. read the file line by line
  8. echo lines as they are read
  9. run the program and check the output
  10. change the stuff to work with your own application

Scanner

Musings

Home


Step 1 - :
public class ReadingExample {
} // end class ReadingExample


Step 2 - add header comments:
// File: ReadingExample.java
// Date: Mar 26, 2017
// Author: Nicholas Duchon
// Purpose: Show a development sequence
//          leading to reading a text file line by lin
e

public class ReadingExample {
} // end class ReadingExample

Step 3 - main:
// File: ReadingExample.java
// Date: Mar 26, 2017
// Author: Nicholas Duchon
// Purpose: Show a development sequence
//          leading to reading a text file line by line

public class ReadingExample {
  public static void main (String args []) {
  } // end main method

} // end class ReadingExample

Step 4 - get main to print something:
// File: ReadingExample.java
// Date: Mar 26, 2017
// Author: Nicholas Duchon
// Purpose: Show a development sequence
//          leading to reading a text file line by line

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

public class ReadingExample {
  public static void main (String args []) {
    System.out.println ("--- Bye ---");
  } // end main method
} // end class ReadingExample

Step 5 - import Scanner, File and FileNotFoundException:
// File: ReadingExample.java
// Date: Mar 26, 2017
// Author: Nicholas Duchon
// Purpose: Show a development sequence
//          leading to reading a text file line by line

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

public class ReadingExample {
  public static void main (String args []) {
    System.out.println ("--- Bye ---");
  } // end main method
} // end class ReadingExample

Step 6 - put together lines required for new Scanner on a file
// File: ReadingExample.java
// Date: Mar 26, 2017
// Author: Nicholas Duchon
// Purpose: Show a development sequence
//          leading to reading a text file line by line

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

public class ReadingExample {
  public static void main (String args []) {
    try {
      Scanner sf = new Scanner (new File ("ReadingExample.java"));
    } catch (FileNotFoundException e) {
    } // end try/catch block   
    System.out.println ("--- Bye ---");
  } // end main method
} // end class ReadingExample

Step 7 - read the file line by line:

// File: ReadingExample.java
// Date: Mar 26, 2017
// Author: Nicholas Duchon
// Purpose: Show a development sequence
//          leading to reading a text file line by line

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

public class ReadingExample {
  public static void main (String args []) {
    try {
      Scanner sf = new Scanner (new File ("ReadingExample.java"));
      while (sf.hasNextLine()) {
      } // end for each line in the file
    } catch (FileNotFoundException e) {
    } // end try/catch block   
    System.out.println ("--- Bye ---");
  } // end main method
} // end class ReadingExample

Step 8 - echo lines as they are read:
// File: ReadingExample.java
// Date: Mar 26, 2017
// Author: Nicholas Duchon
// Purpose: Show a development sequence
//          leading to reading a text file line by line

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

public class ReadingExample {
  public static void main (String args []) {
    try {
      Scanner sf = new Scanner (new File ("ReadingExample.java"));
      while (sf.hasNextLine()) {
        String line = sf.nextLine ();
        System.out.printf ("Read >%s<\n", line);
      } // end for each line in the file
    } catch (FileNotFoundException e) {
    } // end try/catch block   
    System.out.println ("--- Bye ---");
  } // end main method
} // end class ReadingExample

Step 9 - run the program and check the output
Read >// File: ReadingExample.java<
Read >// Date: Mar 26, 2017<
Read >// Author: Nicholas Duchon<
Read >// Purpose: Show a development sequence <
Read >//          leading to reading a text file line by line<
Read ><
Read >import java.util.Scanner;<
Read >import java.io.File;<
Read >import java.io.FileNotFoundException;<
Read ><
Read >public class ReadingExample {<
Read >  public static void main (String args []) {<
Read >    try {<
Read >      Scanner sf = new Scanner (new File ("ReadingExample.java"));<
Read >      while (sf.hasNextLine()) {<
Read >        String line = sf.nextLine ();<
Read >        System.out.printf ("Read >%s<\n", line);<
Read >      } // end for each line in the file<
Read >    } catch (FileNotFoundException e) {<
Read >    } // end try/catch block    <
Read >    System.out.println ("--- Bye ---");<
Read >  } // end main method<
Read >} // end class ReadingExample<
--- Bye ---

Step 10 - change the stuff to work with your own application

Mar 26, 2017
End.