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
Exceptions and IO Dr. Andrew Wallace PhD BEng(hons) EurIng [email protected] Overview • Problems • Handling problems • Input / output • Streams Problems • What can go wrong? • Boundaries • Problems • • • • • User inputs Memory limits Resource limits Impossible maths IO errors Problems • User inputs • public class ParseException extends Exception • public class DataFormatException extends Exception Problems • Memory limits • public class ArrayIndexOutOfBoundsException • public class NullPointerException • public class OutOfMemoryError Problems • Resource limits • public class FileSystemNotFoundException • Public class RunTimeErrorException Problems • Impossible maths • public class ArithmeticException Problems • IO errors • public class FileNotFoundException • public UnknownHostException Quiz • Name five problems that can occur in any java program • How does Java handle the problems? Handling problems • Two types of problem handling classes • Checked • Anticipate and recover • Try / catch block • Method throws • Unchecked • No special handling • Errors in the machine or run time errors Object Handling problems Throwable unchecked Error Exception checked unchecked RuntimeException Handling problems • Create your own exceptions • Create a subclass of one of the Java defined classes public class EndOfTheWorldException extends Exception • Add information on the error • Parameter to the constructor • Use the “throw” key word to throw your exception if(bError) { throw new EndOfTheWorldException(); } public void func() throws Exception Handling problems • Example try { … } catch(IOException e) { … } catch(Exception e) { … } finally { … } Handling problems • Try with resources BufferedReader br = new BufferedReader(new FileReader(path)); try { try (BufferedReader br = new BufferedReader(new FileReader(path))) { Throwable.getSuppressed Quiz • What does checked and uncheck mean in regards to java exceptions? • How do you create you own exceptions? Input / Output • Communicate with the outside world • Java allows many ways to get data in a send data out • • • • GUI (graphic inputs / outputs) Keyboard / screen Database input / output File input / output Input / Output Input / Output • File IO is an abstraction • “File” is the data • Place to read and write to is a “stream” Input / Output • Java IO package • Standard streams • System.in • System.out • System.err - keyboard - console - console • Redirect • System.setIn (Out) (Err) - InputStream - PrintStream - PrintStream Input / Output • Java InputStream is abstract • System.in is a specific implementation • Read strings • Read numbers • Reads in a whole line • Use Scanner to break up the string Input / Output • Scanner class • Breaks up a sting into “tokens” • Uses a delimiter (like space, for example) • Use next functions to get data out • Use has functions to check for more tokens Input / Output Scanner sc = new Scanner(System.in); String strMsg; while(sc.hasNext()) { strMsg = sc.next(); … } Input / Output • File • Save data • Exists between program runs • Text file • Simple and readable with other programs Input / Output • The File class • Represents information on the file • Path • Director • File attributes (hidden, is it readable? Does it exists?) • System independent Quiz • What is a stream in java IO? • What is a file in java IO? Input / Output • Class Reader and Class Writer • Java super classes • Read and write text (char) • Open a file • Read or write to a file • Close a file Input / Output try { FileReader fInput = new FileReader(“inputfile.txt”); FileWriter fOutput = new FileWriter(“outputfile.txt); nChar = fInput.read(); fOutput.write(nChar); } catch( …) { … } finally { if(fInput != null) fInput.close(); if(fOutput != null) fOutput .close(); } Input / Output • Open a file • File object • FileDescriptor • String (file name) • FileWriter allow you to append text as well • Close Input / Output • Read char as int • Read an array of char • Write a char as int • Write an array of char • Write a string • flush Input / Output • More advanced IO • Connect writer to another class • PrintWriter(new FileWriter(“file.txt”)); • println methods for all basic java objects • No exceptions - checkError() Input / Output • BufferedReader • new BufferedREader(new FileReader(“file.txt”)) • String readLine(); • Scanner • Scanner • Scanner fIn = new Scanner(new File(“file.txt”)); fIn = new Scanner(new FileReader(“file.txt”)); Streams Data streams Character Input Character Output Byte Output Byte Input Processing streams Character Input Character Output Byte Input Byte Output Streams • Data Streams • character • Unicode • Reader • FileReader • int read methods • Char arrays Streams • Data Streams • Byte • 8 bit data • InputStream • FileInputStream • int read() • Byte arrays Streams • Data Streams • Character • Writer • FileWriter • Byte • OutputStream • FileOutputStream • int write • void write Streams • Processing Streams • Character • • • • BufferedReader BufferedWriter InputStreamReader LineNumberReader • Byte • ObjectInputStream (output) • ZipInputStream (output) Quiz • What are the two types of steams in java? • What are the basic data types used? Questions?