Scanning text with java.util.Scanner : Scanner « File Input Output « Java






Scanning text with java.util.Scanner

 
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

public class Main {
  private static void readFile(String fileName) throws Exception {
    File file = new File(fileName);
    FileReader reader = new FileReader(file);
    BufferedReader in = new BufferedReader(reader);
    String string;
    while ((string = in.readLine()) != null) {
      System.out.println(string);
    }
    in.close();
  }

  public static void main(String[] args) throws Exception {
    if (args.length != 1) {
      System.err.println("usage: java TextReader " + "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.Simplify the code in TextReader by using java.util.Scanner
6.Read in the current contents of the java.net homepage
7.Perform the conversions after parsing the String