Creating a Scanner for an input file requires several lines of code when all you want is to simply provide a file name and get a Scanner.
Here is a class with methods that handle all the details and do want you want.
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.Scanner;
public class MyIO {
public static Scanner openInput(String fname)
{
Scanner infile = null;
try {
infile = new Scanner(new File(fname));
} catch(FileNotFoundException e) {
System.out.printf("Cannot open file '%s' for input\n", fname);
System.exit(0);
}
return infile;
}
public static PrintWriter openOutput(String fname)
{
PrintWriter pw = null;
try {
pw = new PrintWriter(fname);
} catch(FileNotFoundException e) {
System.out.printf("Cannot open file/create file '%s' for output\n", fname);
System.exit(0);
}
return pw;
}
public static PrintWriter openOutput(PrintStream ps)
{
PrintWriter pw;
pw = new PrintWriter(ps);
return pw;
}
}