Simplify the code in TextReader by using java.util.Scanner : Scanner « File Input Output « Java






Simplify the code in TextReader by using java.util.Scanner

 

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

public class Main {

  static void readFile(String fileName) throws Exception {
    File file = new File(fileName);
    Scanner scanner = new Scanner(file);
    while (scanner.hasNext()) {
      System.out.println(scanner.next());
    }
    scanner.close();
  }

  public static void main(String[] args) throws Exception{
    if (args.length != 1) {
      System.err.println("usage: java TextScanner1" + "file location");
      System.exit(0);
    }
    readFile(args[0]);
  }
}

   
  








Related examples in the same category

1.new Scanner(new BufferedReader(new FileReader("xanadu.txt")))
2.Split a string using Scanner class
3.Read file using Scanner class
4.Read user input from console using Scanner class
5.Scanning text with java.util.Scanner
6.Read in the current contents of the java.net homepage
7.Perform the conversions after parsing the String