/** * LineReader.java * Jeff Ondich, Carleton College, 2014-01-01 * * A very brief introduction to File and Scanner, excessively commented * for tutorial purposes. * * This is the Java half of a pair of parallel examples in Python and Java. * See linereader.py. * * Try a few things: * * 1. Create a small text file called somelines.txt, put it in the same * directory as LineReader.java, and then compile and run LineReader. * ("javac LineReader.java" followed by "java LineReader") * * 2. What happens when you change the inputFilePath variable to "nonexistent.txt"? * * 3. What happens when you remove the "= null" from the Scanner declaration? * * 4. What happens when you remove the try/catch blocks and just do * "Scanner scanner = new Scanner(inputFile)"? * * 5. Visit the java String documentation and play around with some String * methods other than toUpperCase. */ import java.io.File; import java.util.Scanner; import java.io.FileNotFoundException; public class LineReader { public static void main(String[] args) { // Create a File object connected to the file you want to read. String inputFilePath = "somelines.txt"; File inputFile = new File(inputFilePath); // Create a Scanner object connected to your file. This is where // the JVM tries to actually open the file, and thus this is // where an exception can occur. That's why there's a you need to catch the // exception. Scanner scanner = null; try { scanner = new Scanner(inputFile); } catch (FileNotFoundException e) { System.err.println(e); System.exit(1); } // Get one line at a time from the file, and print each line in upper // case to standard output. int numberOfLines = 0; while (scanner.hasNextLine()) { String line = scanner.nextLine(); System.out.println(line.toUpperCase()); numberOfLines++; } System.out.println("\nNumber of lines: " + numberOfLines); } }