Getting Input from a File To open a file for reading: Scanner inFile = new Scanner (file); Scanner constructor - takes a File parameter and provides methods to get individual pieces of data 1 Scanner class Provides methods to get the next data in a file. public String next(); public int nextInt(); public double nextDouble(); Provides methods to check whether the next data has a particular type public boolean hasNext(); public boolean hasNextInt(); public boolean hasNextDouble(); 2 � private void readResultFile() throws FileNotFoundException { � � / / Open the file. � � Scanner skierFile = new Scanner(new File (RESULTS_FILE)); � � � � � / / Find out how many skiers are in the file � � int numSkiers = skierFile.nextInt(); � � skiers = new Skier[numSkiers]; � � / / Read the information for each racer. � � for (int i = 0; i < numSkiers; i++) { � � � / / Line of file is a skier name, followed by 2 doubles, one for each / / race time � � � String skierName = skierFile.next(); � � � double time1 = skierFile.nextDouble(); � � � double time2 = skierFile.nextDouble(); � � � skiers[i] = new Skier(skierName, time1, time2); � � � skierList.append(skiers[i].toString() + "\n"); � � } � � skierFile.close(); � } 3 Tuesday, February 26, 13
Closing a file When you are all done reading from a file, call close: inFile.close(); 4 What if... There is no file with the name given to the File constructor: new File (“IDontExist.txt”); Throws FileNotFoundException 5 Handling Exceptions Call that throws the exception try { File f = new File (“file.txt”); ��� Scanner in = new Scanner (f); ��� ... } catch (FileNotFoundException e) { ��� ... } Exception being handled Exception Handler 6 Tuesday, February 26, 13
What happens Call the constructor try { File f = new File (“file.txt”); ��� Scanner in = new Scanner (f); ��� ... } catch (FileNotFoundException e) { ��� ... } 7 If the file doesn’ t exist try { File f = new File (“file.txt”); ��� Scanner in = new Scanner (f); ��� ... } catch (FileNotFoundException e) { ��� ... } Not executed Go to the exception handler 8 If the file does exist try { File f = new File (“file.txt”); ��� Scanner in = new Scanner (f); ��� ... } catch (FileNotFoundException e) { ��� ... } Go to the next line Not executed 9 Tuesday, February 26, 13
What happens if no handler public void someMethod() { File f = new File (“file.txt”); Scanner in = new Scanner (f); ... } Compiler error: Unhandled exception type FileNotFoundException Solution: method must declare that it throws the exception: public void someMethod() throws FileNotFoundException 10 Exceptions and Contracts Exceptions should be documented in a method’ s contract What exception is thrown Under what conditions is the exception thrown /** Does something really wonderful @throws FileNotFoundException if the file named file.txt does not exist. */ public void someMethod () throws FileNotFoundException 11 What if... You call nextInt() and the next thing in the file is not an integer? InputMismatchException You call one of the next methods and there is nothing left in the fille? NoSuchElementException You call one of the next methods but the file is closed? IllegalStateException Be sure to call has… methods before next… methods to avoid these exceptions. 12 Tuesday, February 26, 13
Recommend
More recommend