Mainscannertestjava8 cpsc324spring2010 9 scanner

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: FileNotFoundExcepIon { File aFile = new File(“file.txt ”); Scanner in = new Scanner(aFile); [sbowers:]$ java ScannerTest while(in.hasNextLine()) ExcepIon in thread "main" java.io.FileNotFoundExcepIon: file.txt (No System.out.println(in.nextLine()); such file or directory) in.close(); at java.io.FileInputStream.open(NaIve Method) at java.io.FileInputStream.<init>(FileInputStream.java:106) } at java.uIl.Scanner.<init>(Scanner.java:621) } at ScannerTest.main(ScannerTest.java:8) CPSC 324 ‐‐ Spring 2010 9 Scanner example Another solution ... import java.uIl.Scanner; import java.io.File; Handle the exception import java.io.FileNotFoundException; public class ScannerTest { public staIc void main(String args) { File aFile = new File(“file.txt ”); try { Scanner in = new Scanner(aFile); This is called a while(in.hasNextLine()) System.out.println(in.nextLine()); “try-catch” block in.close(); } catch(FileNotFoundExcepIon e) { // degrade gracefully … } } } CPSC 324 ‐‐ Spring 2010 10...
View Full Document

This document was uploaded on 03/18/2014 for the course CPSC 324 at Gonzaga.

Ask a homework question - tutors are online