Download presentation
Presentation is loading. Please wait.
1
Files The UI text pane window is transient:
Typing large amounts of input into the text pane is a pain! It would be nice to be able to save the output of the program easily. Large amounts of text belong in files How can you read from a file? How can you write to a file? Easy! Files behave very like the UI text pane: next, nextInt etc read from a file, (via a Scanner object) print, println, printf write to a file (via a PrintStream object)
2
Files: An overview Enter numbers: 40 60 30 50 done UI.nextInt();
UI Window UI.nextInt(); UI.println(); A real file: “myfile.txt” next(); My Program : int a =??.nextInt(); ??.println(a*5); } Scanner object File object PrintStream object println();
3
Scanner Scanner: a class in Java that allows a program to read user input from a source (a file, input window, a String, …) New concept? Not really! UI.next() and other next methods are actually a Scanner working in the background while(UI.hasNextInt()){ double amt = UI.nextDouble() } Most UI next methods work same as Scanner next methods, but a very few subtle differences…see Java documentation Scanner scan = new Scanner(myfile); while(scan.hasNextInt()){ double amt = scan.nextDouble(); }
4
Reading using Scanner:
/** Read lines from a file and print them to UI text pane. */ public void readFile(){ File myfile = new File(“input.txt”); Scanner scan = new Scanner(myfile); UI.println(“ input.txt ”); while (scan.hasNext()){ String line= scan.nextLine(); UI.println(line); } UI.println(“ end of input.txt ”); Almost right, but compiler complains!!! Dealing with files may “raise exceptions” Missing bits to handle exceptions
5
Files: handling exceptions
If a piece of code might raise an exception: Have to enclose it in a try { … } catch (IOException e) { … } public void readFile(){ File myfile = new File(“input.txt”); try { Scanner scan = new Scanner(myfile); while (scan.hasNext()){ String line = scan.nextLine(); UI.println(line); UI.println(“ end of input.txt ”); catch (IOException e) { UI.println(“File failure: ” + e); } what to do what to do if it goes wrong
6
Reading from files: example 2
84 Isaac Newton 48 James Clerk Maxwell 66 Marie Curie 62 Aristotle /** Finds oldest person in file of ages and names. */ public void printOldest(){ try { Scanner scan = new Scanner(new File(“names.txt")); String oldest = ""; int maxAge = 0; while (scan.hasNextInt()){ int age = scan.nextInt(); String name = scan.nextLine(); if (age > maxAge) { maxAge = age; oldest = name; } UI.printf(“Oldest is %s (%d)%n”, oldest, maxAge); } catch (IOException e) { UI.printf(“File failure: %s%n”, e); } no need to prompt user! Copes with multi-token names
7
Testing end of file Unlike UI text pane, can test for end of file:
/** Finds oldest person in file of names and ages. */ public void printOldest(){ try { Scanner scan = new Scanner(new File(“names.txt")); String oldest = “”; int maxAge = 0; while (scan.hasNext()){ String name = scan.next(); int age = scan.nextInt(); if (age > maxAge) { maxAge = age; oldest = name; } UI.println(“Oldest is %s (%d)%n”, oldest, maxAge); } catch (IOException e) { UI.printf(“File failure: %s%n”, e); } Newton 84 Maxwell 48 Curie 66 Aristotle 62 False when at end of file name first; age second Note: name must be just one token!!
8
A common simple pattern
File with one entity per line, described by multiple values: while (sc.hasNext() ){ String type = sc.next(); double cost = sc.nextDouble(); int wheels = sc.nextInt(); String colour = sc.next(); String make = sc.next() if (wheels > 4) { …. } else { … bicycle green Giant truck black Isuzu car red Toyota … Read all the values into variables process the values in the variables
9
Handling multiple items on line
Sometimes you can tell where the line ends: /**Sums counts of items in file of items and counts*/ public void printItemCounts(){ try { Scanner scan = new Scanner(new File(“data.txt")); while (scan.hasNext()){ String item = scan.next(); int lineTot = 0; while (scan.hasNextInt()) { lineTot = lineTot + scan.nextInt(); } UI.println(item + “: ” + lineTot); } catch (IOException e) { UI.printf(“File failure: %s\n”, e); } biscuits cake 3 5 2 fruit beans biscuits: 88 cake: 10 fruit: 235 beans: 33
10
Reading files line by line
Sometimes can’t tell where ends of lines are: may need to read a line at a time, then process: /**Adds up numbers on each line of a file */ public void addCounts(){ try { Scanner scan = new Scanner(new File(“data.txt")); while (scan.hasNext()){ String line = scan.nextLine(); Scanner lineSc = new Scanner(line); int lineTot = 0; while (lineSc.hasNextInt()) { lineTot = lineTot + lineSc.nextInt(); } UI.println(lineTot); } catch (IOException e) { UI.printf(“File failure: %s\n”, e); } 3 5 2 Wrapping a Scanner around a String, Lets you “read” values from the String
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.