Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
FIT1002 2006 FIT1002 Computer Programming Unit 19 File I/O and Exceptions 1 FIT1002 2006 Objectives By the end of this lecture, students should: • understand simple character-oriented file input and output • understand exceptions as mechanism of handling abnormal circumstances • understand the differences between throwing exceptions and aborting a program • understand the difference between Exceptions and Errors • be able to write code for character-oriented input and output using the Sanner class • be able to write code using basic exceptions without subclassing • use try & catch statements Reading: Savitch, Sec. 10.1 (p. 574-596, p 606-607): File I/O Savitch, Sec. 9.1: Exception Basics 2 FIT1002 2006 Input We have used the Scanner class for character input from the console before… •you must “import java.util.Scanner;” •You must create a new scanner object : Scanner console = new Scanner(System.in); •new values can then be read from the Scanner object using –console.nextInt() “for Integers” –console.nextFloat() “for Floats” –console.nextLine() “for Strings” –console.next() “any token” –etc (see Java API doc at Sun’s web site) 3 FIT1002 2006 Reminder: Using the Scanner Class Recall the method readLines in the class Message (Tutorial 8) to read in and concatenate a number of input lines: import java.util.Scanner; public abstract class Message { private String msg; … public void readLines() { Scanner console = new Scanner(System.in); String input = ""; while (!input.equals("STOP")) { msg += input; input = console.nextLine(); } } } 4 FIT1002 2006 System.in • You create a new scanner object for a particular input stream (“channel”): Scanner console = new Scanner(System.in); • this is obviously a constructor use, System.in is the parameter. • System is a pre-defined class • System.in is a static (class) variable of System • It is a “stream”: • – a stream is something that you can read from or write to. Note that we also have System.out which is the standard output stream. 5 FIT1002 2006 Input/Output Devices System.out System.in Program 6 FIT1002 2006 Using other Input Sources • If you want to read from another source, you need to give another stream to the Scanner when it is constructed • Example: reading from a file • You need to construct a Scanner that is attached to the file • check the Scanner constructors in the Java API • So we can generate a Scanner for a particular file using the first constructor … but we need to make that File available first 7 FIT1002 2006 Files • to make the file available we need to “open” it. • “Opening a file” means • 1. to construct a new File object in the program and 2. to attach it to a file in the Operating System. This can be done in a single step using an appropriate constructor for a File object (check the API). • After constructing the File object, we need to construct a Scanner object for this file. we will later see how we can easily let the user choose a file in an interactive program by using the type of file chooser dialog that you usually see in your operating system (Lectures on GUIs). 8 FIT1002 2006 Putting it all together… as before, this code read input lines into message, but now it does so from a file. import java.io.*; import java.util.Scanner; public class FileInputTest { private String message; public String readLinesFromFile(String filename) throws FileNotFoundException { File inFile = new File(filename); Scanner inScan = new Scanner(inFile); String tmp=""; while (inScan.hasNext()) tmp += inScan.next()+” “; message = tmp; return tmp; } } 9 FIT1002 2006 1. 2. 3. 4. Notes you need to import java.io.* to use files you need to import java.util.Scanner to use Scanners you can use “hasNext()” to test whether there is more input available you need to add “throws FileNotFoundException” to the method header (more about this later). import java.io.*; import java.util.Scanner; public class FileInputTest { private String message; public String readLinesFromFile(String filename) throws FileNotFoundException { File inFile = new File(filename); Scanner inScan = new Scanner(inFile); String tmp=""; while (inScan.hasNext()) tmp += inScan.next(); message = tmp; return tmp; } } 10 FIT1002 2006 Checking End-of-File • how do you know that you have read the whole file without being forced to use a special token (like “STOP”) as the last word of the file? • The Scanner knows whether there is still something to read… public boolean console.hasNext() • returns true if there are more tokens in the input and false otherwise. • There are also specific checks whether the next token (if any) is of a particular type: • • • • hasNextInt() hasNextFloat() hasNextLine() etc… 11 FIT1002 2006 File Output • We have already used simple output System.out.println(“bla bla bla”); Analyze this expression: • System (a class), • out (a static variable in the class System), • println(…) a message sent to System.out • Recall: System.out is a printStream (Slide 5): • The class PrintStream has a println(…) method 12 FIT1002 2006 PrintStream vs PrintWriter • To print to a file we have to replace the PrintStream System.out in the System.out.println(“bla bla bla”); • To do so we need to construct a new PrintStream and attach it to a file. However, looking up PrintStream in the API we find the following information: “The PrintWriter class should be used in situations that require writing characters rather than bytes.” 13 FIT1002 2006 PrintWriter • PrintWriter has print(…) and println(…) • We could open a file and attach it to a PrintWriter, • more conveniently, a special constructor for PrintWriter allows us to do this automatically: 14 FIT1002 2006 Printing to a file import java.io.*; import java.util.Scanner; public class FileInputTest { private String message; … public void writeLinesToFile(String filename) throws FileNotFoundException { PrintWriter outWriter = new PrintWriter(filename); outWriter.println("The text in this object is"); outWriter.println(message); outWriter.close(); } } 15 FIT1002 2006 Opening / Closing Files 1. Before you can use a file you need to “open” it. 2. When you are finished you need to “close” it. This signals to the O/S that you will not need it anymore, it makes the file available to others it also ensures that all characters from the internal buffers are flushed to the file. System.out.println(“Hello!”); H e l l o ! • Opening and closing of files can be performed implicitly by opening and closing the associated PrintWriter objects. 16 FIT1002 2006 Exceptions • Exceptions are a special error handling mechanism. • An exception can be “thrown” in a statement to abort the current method and signal to the calling code that something has gone wrong. • In principle this is very similar to throwing an Error (as we have done in Constructors). • The calling method can test whether an exception has occurred and react to this. This is done with a try-catch statement: • the “try” statement executes some code that may throw an exception • If an exception happens, • the whole “try” block is immediately aborted and • the “catch” block is then executed • If no exception happens, • the “try” block is completely executed • execution then skips the “catch” block and continues after it 17 FIT1002 2006 Handling Exceptions • Many built-in methods signal problems by throwing exceptions • For example, the constructors associated with files throw a special kind of exception, the FileNotFoundException. • We can use try-catch to handle this: public class FileIOTest { private String message; public String readLinesFromFile(String filename) { try { File inFile = new File(filename); Scanner inScan = new Scanner(inFile); String tmp=""; while (inScan.hasNext()) tmp += inScan.next()+" "; message = tmp; return tmp; } catch (FileNotFoundException e) { System.out.println("Error: This File does not exist"); return ""; } } 18 FIT1002 2006 Throwing Exceptions • You can also throw exceptions in your own code • This works just like throwing errors • You can handle your own exceptions in the same way as built-in ones. public class ExceptionTest { public void test(double aNumber) { try { System.out.println(mySqrt(aNumber)); } catch (Exception e) { System.out.println("Can't compute Sqrt of "+aNumber); } } public double mySqrt(double x) throws Exception { if (x<0) throw new Exception(); else return Math.sqrt(x); } } 19 FIT1002 2006 Throwing Exceptions: Notes • the braces around the try and catch block are mandatory • you must declare that your method may throw an exception • you do not have to return anything when you throw an exception (in fact, you can’t). public class ExceptionTest { public void test(double aNumber) { try { System.out.println(mySqrt(aNumber)); } catch (Exception e) { System.out.println("Can't compute Sqrt of "+aNumber); } } public double mySqrt(double x) throws Exception { if (x<0) throw new Exception(); else return Math.sqrt(x); } } 20 FIT1002 2006 The Exception Object • An Exception is an object • a catch clause declares – which type of exception it wants to catch (its class) and – a local variable to which this exception will be bound • The Exception object contains useful (debugging) information, in particular an error message – You can obtain this information using • the toString() method • the getMessage() method – You can set the error message in the constructor 21 FIT1002 2006 Using Exception Objects public class ExceptionTest { public void test(double aNumber) { try { System.out.println(mySqrt(aNumber)); } catch (Exception e) { System.out.println(e.toString()); System.out.println(e.getMessage()); } } public double mySqrt(double x) throws Exception { if (x<0) throw new Exception("Can't compute Sqrt of "+x); else return Math.sqrt(x); } } 22 FIT1002 2006 Exception in Call Hierarchies • You can also choose not to handle an exception (no try-catch) • An exception that is not handled will “bubble up” the call hierarchy. • Your code must declare in the method header that it is aware that an exception could occur using a “throws” declaration (see previous slide). • Example: Method A calls Method B which in turn calls Method C • • • • Method C throws an exception C is immediately aborted if B does not handle the exception, B is also immediately aborted A can now handle the exception or let it bubble up further A B C 23 FIT1002 2006 Demo: Exceptions in Call Hierarchies public class TripleThrow { public void A(int x) { System.out.println("A running"); try { B(x); } catch (Exception e) { System.out.println("Caught exception while executing B"); } System.out.println("A finalizing"); } public void B(int x) throws Exception { System.out.println("B running"); C(x); System.out.println("B finalizing"); } public void C(int x) throws Exception { System.out.println("C running"); if (x<0) throw new Exception(); System.out.println("C finalizing"); } } 24 FIT1002 2006 Errors • Previously (constructor lessons) we have thrown “Errors” •The fundamental difference is • Exceptions must be handled • either caught in a “try-catch” • or declared to bubble up with a “throws” declaration • Errors can, but do not need to be handled • no “throws” declaration is required in the calling code The advantage of Exceptions is that a programmer will automatically be made aware by the compiler that he/she has potentially forgotten to handle a particular exception. Thus Errors should only be used where it is a reasonable to let them bubble up to the top level (and thus abort the entire program). If you want the problem to be handled at some level in your code, you should use an Exception. 25 FIT1002 2006 Distinguishing Exceptions Types (advanced, optional) • We can handle exceptions selectively • handle only a specific exception type in a catch clause and • let other exceptions “bubble up” • Exceptions are in a class hierarchy, • The catch clause only handles exceptions of the type it has declared • Exceptions with other types (classes) will “bubble up” • This means that you must still declare a throw in the message header if you do not handle all exceptions that might occur . • The most general type is simply “Exception”, the top-class of all exceptions: This will catch all possible exceptions. • However, it is good to be specific. The compiler can only effectively tell you whether you have forgotten to handle some exception if your catch clauses and throw declarations are sufficiently specific. 26