FIT1002 2006 1 FIT1002 Computer Programming Unit 19 File I/O and Exceptions.

Slides:



Advertisements
Similar presentations
Mr. Wortzman.  So far, we have gotten all our input and written all our output to the console  In reality, this is somewhat uncommon  Instead, we often.
Advertisements

Exceptions: when things go wrong. Various sources of error public static doSomething() { int i = 3.0; while(!done); { int i = false } ) Syntactic errors.
Lecture 23 Input and output with files –(Sections 2.13, 8.7, 8.8) Exceptions and exception handling –(Chapter 17)
CS102--Object Oriented Programming
COMP 121 Week 5: Exceptions and Exception Handling.
© 2004 Pearson Addison-Wesley. All rights reserved10-1 Chapter 10 : Exceptions Intermediate Java Programming Summer 2007.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Java I/O Java I/O is based on input streams and output streams. All input and output are defined in the Java IO package. 1.
Chapter 8Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 8 l Basic Exception Handling »the mechanics of exceptions l.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Exception Handling Chapter 12.  Errors- the various bugs, blunders, typos and other problems that stop a program from running successfully  Natural.
10-1 Writing to a Text File When a text file is opened in this way, a FileNotFoundException can be thrown – In this context it actually means that the.
Text File I/O. Text Files and Binary Files Files that are designed to be read by human beings, and that can be read or written with an editor are called.
File I/O and Exceptions File I/O Exceptions Throwing Exceptions Try statement and catch / finally clauses Checked and unchecked exceptions Throws clause.
CPSC150 Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 1 CPSC150 Exceptions When things.
11-Jun-15 Exceptions. 2 Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a.
EXCEPTIONS Def: An exception is a run-time error. Examples include: attempting to divide by zero, or manipulate invalid data.
1 File Output. 2 So far… So far, all of our output has been to System.out  using print(), println(), or printf() All input has been from System.in 
16-Jun-15 Exceptions. Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a null.
Files and Streams CS 21a Chapter 11 of Horstmann.
Exceptions. Errors and Exceptions An error is a bug in your program –dividing by zero –going outside the bounds of an array –trying to use a null reference.
Chapter 8: Exceptions and I/O Streams Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,
Chapter 8 Overview – Learn to use try catch blocks – Learn to use streams – Learn to use text files.
Slides prepared by Rose Williams, Binghamton University Chapter 10 File I/O.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Exception Handling in Java Course Lecture Slides 7 th July 2010 “ Admitting.
Streams and File I/O Chapter 14. I/O Overview I/O = Input/Output In this context it is input to and output from programs Input can be from keyboard or.
Input/Ouput and Exception Handling. 2 Exceptions  An exception is an object that describes an unusual or erroneous situation  Exceptions are thrown.
What is an exception? An exception is: – an event that interrupts the normal processing of the program. –an error condition that violates the semantic.
Object Oriented Programming
Exceptions 1. Your computer takes exception Exceptions are errors in the logic of a program (run-time errors). Examples: Exception in thread “main” java.io.FileNotFoundException:
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
Chapter 9 1 Chapter 9 – Part 1 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
COMP Exception Handling Yi Hong June 10, 2015.
Winter 2006CISC121 - Prof. McLeod1 Last Time Misc. useful classes in Java: –String –StringTokenizer –Math –System.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Computer Programming with JAVA Chapter 8. Exception Handling Basic Exception Handling the mechanics of exceptions Defining and Using Exceptions some "simple"
© 2004 Pearson Addison-Wesley. All rights reserved April 24, 2006 Exceptions (part 2) ComS 207: Programming I (in Java) Iowa State University, SPRING 2006.
CMSC 202 Text File I/O. Aug 8, Text Files and Binary Files Files that are designed to be read by human beings, and that can be read or written with.
Chapter 10 Text Files Section 10.2 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
Programming and Problem Solving With Java Copyright 1999, James M. Slack Exceptions Handling Exceptions with try and catch The finally-block The throws.
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
Chapter 11 Exceptions and Input/Output Operations.
I/O Basics 26 January Aside from print( ) and println( ), none of the I/O methods have been used significantly. The reason is simple: most real.
Exceptions Lecture 11 COMP 401, Fall /25/2014.
© 2004 Pearson Addison-Wesley. All rights reserved December 5, 2007 I/O Exceptions & Working with Files ComS 207: Programming I (in Java) Iowa State University,
COMP 110: Spring Announcements Program 5 Milestone 1 was due today Program 4 has been graded.
File Input & Output Sections Outcomes  Know the difference between files and streams  Use a Scanner to read from a file  add “throws” annotations.
1 Text File Input and Output. Objectives You will be able to Write text files from your Java programs. Read text files in your Java programs. 2.
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
Introduction to Exceptions in Java CS201, SW Development Methods.
Building Java Programs Chapter 6 File Processing Copyright (c) Pearson All rights reserved.
CompSci 230 S Programming Techniques
Basic Text File Input/Output
File - CIS 1068 Program Design and Abstraction
Streams & File Input/Output (I/O)
CMSC 202 Text File I/O.
Streams and File I/O.
Building Java Programs
I/O Basics.
CSS 161: Fundamentals of Computing
File I/O ICS 111: Introduction to Computer Science I
Fundamental Error Handling
Exception Handling Contents
I/O Exceptions & Working with Files
Exceptions 10-May-19.
Streams A stream is an object that enables the flow of data between a program and some I/O device or file If the data flows into a program, then the stream.
Presentation transcript:

FIT FIT1002 Computer Programming Unit 19 File I/O and Exceptions

FIT 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 (p , p ): File I/O Savitch, Sec. 9.1: Exception Basics

FIT 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)

FIT 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(); }

FIT 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.

FIT Input/Output Devices Program System.in System.out

FIT 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

FIT 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).

FIT Putting it all together… 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; } as before, this code read input lines into message, but now it does so from a file.

FIT Notes 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; } 1.you need to import java.io.* to use files 2.you need to import java.util.Scanner to use Scanners 3.you can use “hasNext()” to test whether there is more input available 4.you need to add “throws FileNotFoundException” to the method header (more about this later).

FIT 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…

FIT 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

FIT 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.”

FIT 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:

FIT 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(); }

FIT 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. Opening and closing of files can be performed implicitly by opening and closing the associated PrintWriter objects. Hello! System.out.println(“Hello!”);

FIT 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

FIT 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 ""; }

FIT 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); }

FIT 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); }

FIT 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

FIT 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); }

FIT 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 BC

FIT 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"); }

FIT 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.

FIT 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.