new Scanner(FileReader file) : Scanner « java.util « Java by API






new Scanner(FileReader file)

 
/**
 *Output:
String: Testing
String: Scanner
int: 10
double: 12.2
String: one
boolean: true
String: two
boolean: false
 */

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class MainClass {
  public static void main(String args[]) throws IOException {

    int i;
    double d;
    boolean b;
    String str;

    FileWriter fout = new FileWriter("test.txt");
    fout.write("Testing Scanner 10 12.2 one true two false");
    fout.close();

    FileReader fin = new FileReader("Test.txt");

    Scanner src = new Scanner(fin);

    while (src.hasNext()) {
      if (src.hasNextInt()) {
        i = src.nextInt();
        System.out.println("int: " + i);
      } else if (src.hasNextDouble()) {
        d = src.nextDouble();
        System.out.println("double: " + d);
      } else if (src.hasNextBoolean()) {
        b = src.nextBoolean();
        System.out.println("boolean: " + b);
      } else {
        str = src.next();
        System.out.println("String: " + str);
      }
    }

    fin.close();
  }
}

           
         
  








Related examples in the same category

1.new Scanner(InputStream source)
2.new Scanner(String instr)
3.new Scanner(Readable source)
4.Scanner: findInLine(String str)
5.Scanner: findWithinHorizon(String pattern, int horizon)
6.Scanner: hasNext()
7.Scanner: hasNextBoolean()
8.Scanner: hasNextDouble()
9.Scanner: hasNextInt()
10.Scanner: hasNextLine()
11.Scanner: next()
12.Scanner: nextBoolean()
13.Scanner: nextDouble()
14.Scanner: nextInt()
15.Scanner: nextLine()
16.Scanner: useDelimiter(String pattern)