




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





■ Programmers refer to input/output as "I/O". ■ Input is received from the keyboard, mouse, files. output is sent to the console, monitor, files, … ■ The File class represents files as objects, and is defined in the java.io package. ■ Creating a File object allows you to get information about a file (on the hard disk or optical drive). ■ Creating a File object does NOT create a new file on your disk. File f = new File("example.txt"); if (f.exists() && f.length() > 1000) { f.delete(); }
CS 160, Summer Semester 2016 2
■ Scanner scan = new Scanner(“Hello there”);
■ Scanner scan = new Scanner(System.in);
■ Scanner scan = new Scanner(new File(
❑ Read a line String str = scan.nextLine(); ❑ Read a string (separated by whitespace) String str = scan.next( ); ❑ Read an integer int ival = scan.nextInt( ); ❑ Read a double double dval = scan.nextDouble( );
CS 160, Summer Semester 2016^5
Scanner
Scanner scan = new Scanner(new File("numbers.txt"));
File file = new File("numbers.txt"); Scanner scan= new Scanner(file);
String variable or string literal
6 CS 160, Summer Semester 2016
❑ In Directory: "names.dat" ❑ In Subdirectory: "code/Example.java"
❑ In Linux: "/users/cs160/programs/Example.java" ❑ In Windows: "C:/Documents/cs160/programs/data.csv"
❑ If our program is in ~/workspace/P ❑ Scanner will look for ~/workspace/P4/data/input.txt
❑ "I hereby agree that this method might throw an exception, and I accept the consequences (crashing) if this happens.” ❑ General syntax: public static
CS 160, Summer Semester 2016^13
public static void main(String[] args) { try { File file = new File(“input.txt”); Scanner scan = new Scanner(file); String firstLine = scan.nextLine(); ... } catch (IOException e) { System.out.println(“Unable to open input.txt”); System.exit(-1); } } CS 160, Summer Semester 2016^14
14.9 7.4 2.
3.9 4.7 -15.
❑ 308.2\n\t14.9 7.4 2.8\n\n3.9 4.7\t-15.4\n\t2.8\n
308.2\n 14.9 7.4 2.8\n\n\n3.9 4.7 -15.4\n2.8\n ^ scan.nextDouble(); 308.2\n 14.9 7.4 2.8\n\n\n3.9 4.7 -15.4\n2.8\n ^ scan.nextDouble(); 308.2\n 14.9 7.4 2.8\n\n\n3.9 4.7 -15.4\n2.8\n ^
CS 160, Summer Semester 2016^17
CS 160, Summer Semester 2016^18
public static void main(String[] args) try { File file = new File(“input.txt”); Scanner scan = new Scanner(file); for (int i = 0; i <= 4; i++) { double next = scan.nextDouble(); System.out.println("number = " + next); } } catch (IOException e) { System.out.println(“Unable to open input.txt”); System.exit(-1); } }
CS 160, Summer Semester 2016^25
int count = 0; while (scan.hasNextLine()) { String line = scan.nextLine(); System.out.println(count + “ “ + line); count++; }
CS 160, Summer Semester 2016^26
■ Break the input into lines. ■ Break each line into tokens.
String text = "1.4 3.2 hello 9 27.5"; Scanner scan = new Scanner(text); System.out.println(scan.next()); // 1. System.out.println(scan.next()); // 3. System.out.println(scan.next()); // hello
Scanner scan = new Scanner(new File(
CS 160, Summer Semester 2016^29
Scanner scan = new Scanner(new File("input.txt")); while (scan.hasNextLine()) { String line = scan.nextLine(); Scanner lineScan = new Scanner(line); int count = 0; while (lineScan.hasNext()) { String token = lineScan.next(); count++; } System.out.println("Line has “+count+" tokens"); } Input file input.txt: 23 3.14 John Smith "Hello world" 45.2 19
Output to console: Line has 6 tokens Line has 2 tokens CS 160, Summer Semester 2016^30
public static void main(String[] args) { try { File file = new File(“output.txt”); PrintWriter output = new PrintWriter(file); output.println(“Integer number: ” + 987654); ... } catch (IOException e) { System.out.println(“Unable to write output.txt”); System.exit(-1); }
output.println(”Double: " + 123.456); output.println("Integer: " + 987654); output.println("String: " + "Hello There");