Presentation is loading. Please wait.

Presentation is loading. Please wait.

EE2E1. JAVA Programming Lecture 7 Files and Streams.

Similar presentations


Presentation on theme: "EE2E1. JAVA Programming Lecture 7 Files and Streams."— Presentation transcript:

1 EE2E1. JAVA Programming Lecture 7 Files and Streams

2 Contents Introduction - input and output streams Introduction - input and output streams Exception handling Exception handling The stream class hierarchy The stream class hierarchy Simple file i/o Simple file i/o Layering streams – reading/writing formatted files Layering streams – reading/writing formatted files Reading and writing text files Reading and writing text files

3 Introduction - input and output streams Most applications require data to be stored in files Most applications require data to be stored in files  Spreadsheets  Word processing  etc Java has extensive facilities for handling files of various types Java has extensive facilities for handling files of various types Associated with files is the general concept of streams which can be applied to both files and networks Associated with files is the general concept of streams which can be applied to both files and networks

4 In Java, streams are simply sequences of bytes In Java, streams are simply sequences of bytes We can read a byte stream from an input stream object We can read a byte stream from an input stream object We can write a byte stream to an output stream object We can write a byte stream to an output stream object Typically input and output stream objects can be files but they also can be network connections Typically input and output stream objects can be files but they also can be network connections  This generality means we can use the same functions for accessing networks as reading files

5 Input stream object byte streamOutput stream objectbyte stream

6 Java distinguishes between byte streams and character streams Java distinguishes between byte streams and character streams  In Java, a character is represented by a 2- byte Unicode character (www.unicode.org) Writer object character stream Reader object character stream

7 Finally Java uses the concept of a data stream to read and write useful data such as integers and doubles (as opposed to just bytes and characters) Finally Java uses the concept of a data stream to read and write useful data such as integers and doubles (as opposed to just bytes and characters)  Java uses a mechanism called layering to attach these different types of streams together  For example we can attach an input stream to a file and then attach a data input stream to the file input stream so we can read a file of integers. (See later!) The java.io package contains all of the classes for input/output and streams The java.io package contains all of the classes for input/output and streams  Its all quite complex because of its huge flexibility Its even worse than that because before we can use streams we need to know about exceptions!! Its even worse than that because before we can use streams we need to know about exceptions!!

8 Exception handling All Java programs which deal with files and/or streams must include exception handling All Java programs which deal with files and/or streams must include exception handling Exceptions are error conditions encountered in executing class methods Exceptions are error conditions encountered in executing class methods  Attempting to read past an end of file  Attempting to read a file that doesn’t exist  Trying to open a malformed URL  Divide by zero  Taking the square root of a negative number  etc

9 Normal error handling (in C) would return an error code (eg. –1) Normal error handling (in C) would return an error code (eg. –1) class myClass {. public int readFile(….) { do { if (!end_of_file) // read the file else return –1; } while not_end_of_file return number_of_bytes_read; }

10 This is a simple sometimes effective method but: This is a simple sometimes effective method but:  Sometimes not possible to return a valid error code.  Not object oriented! No information about the error is contained in the error code  Application code gets ‘polluted’ with error checking code  It would be nice to have all of the error handling in one place  The method might not be able to return normally from the error.  An example would be if a resource the method was accessing was not available

11 In Java, if an error occurs in a method, an Exception object is thrown by the method In Java, if an error occurs in a method, an Exception object is thrown by the method  Any method that calls the first method must catch the exception in an exception handler and take appropriate action  After the exception is thrown and the exception handler code executed, control is not passed back to the caller  We don’t have to worry about return error codes

12 Throwing exceptions A method must advertise the fact that it is going to throw an exception of a specific type A method must advertise the fact that it is going to throw an exception of a specific type  For example some problem with I/O Java uses the keyword throws to indicate that a method can throw an exception Java uses the keyword throws to indicate that a method can throw an exception The method actually throws the exception using the throw keyword The method actually throws the exception using the throw keyword

13 class myClass {. public int myMethod throws IOException {. if (some_IO_error_condition) throw new IOException();// throw exception object. }

14 Catching exceptions If a method calls another method which throws an exception, that exception must either be caught or thrown on If a method calls another method which throws an exception, that exception must either be caught or thrown on  If it is not, the program will terminate (non- graphical, console applications)  or control will pass back to the user interface processing loop (graphical applications/applets)  In both cases, an error message will be printed

15 Exceptions are caught in a try/catch block Exceptions are caught in a try/catch block public aMethod { // method code which doesn’t throw an exception try { // code including a call to myClass.myMethod() // which may throw an IOException } catch(IOException e) { // Exception handler code } } // method ends

16 Execution of the above code is as follows Execution of the above code is as follows  If the code inside the try block throws an exception  The program skips the remainder of the code in the try block  The program then proceeds to the code in the catch block  If the code inside the try block doesn’t throw an exception  The program skips the code in the catch block

17 A finally clause after a catch block contains code which is always executed irrespective if an exception is caught or not A finally clause after a catch block contains code which is always executed irrespective if an exception is caught or not  Quite often it is used to release a resource which has been allocated in the try block For all of this to work, the exception generated must be of the type specified (in this case, an IOException) in the catch clause For all of this to work, the exception generated must be of the type specified (in this case, an IOException) in the catch clause

18 public aMethod { // method code which doesn’t throw an exception try { // Code which may throw an IOException // Resource allocation } catch(IOException e) { // Exception handler code } finally { // Resource deallocation // This code always executes } } // method ends

19 Alternatively, the exception can be thrown on Alternatively, the exception can be thrown on public aMethod throws IOException { // method code including a call to myClass.myMethod() // which may throw an IOException }

20 Exception inheritance hierarchy Exceptions are objects encapsulating information about the type of error which caused the exception Exceptions are objects encapsulating information about the type of error which caused the exception All exception objects derive from Throwable All exception objects derive from Throwable 2 main types of exception object 2 main types of exception object  RuntimeException  Usually a programmer error such as divide by zero or trying to access an array beyond its limits  IOException  Not generally caused by programmer error and generally relating to I/O or network errors

21 Throwable Exception IOException RuntimeException ………

22 Creating exception classes It is easy to create our own exceptions by extending an existing exception class It is easy to create our own exceptions by extending an existing exception class public DivideByZeroException extends ArithmeticException { public DivideByZeroException(String Message) { super(message); }

23 The exception can then be thrown in the usual way The exception can then be thrown in the usual way class myClass {. public double quotient(int num, int den) throws DivideByZeroException { if (den==0) throw new DivideByZeroException(“Error!”); return ((double) num/(double)den);` }

24 Example The following program is a simple GUI enabling integers to be inputted and their quotient computed The following program is a simple GUI enabling integers to be inputted and their quotient computed Throws exceptions for Throws exceptions for  Incorrect integer format  NumberFormatException  Divide by zero  DivideByZeroException

25 0Stuff/ExceptionTestApplet/ExceptionTestA pplet.html 0Stuff/ExceptionTestApplet/ExceptionTestA pplet.html 0Stuff/ExceptionTestApplet/ExceptionTestA pplet.html 0Stuff/ExceptionTestApplet/ExceptionTestA pplet.html Error dialog boxes are displayed when exceptions are caught Error dialog boxes are displayed when exceptions are caught  Control then passes back to the event handling loop  For a console based application, the program would terminate after the exceptions are caught

26 DivideByZero exception thrown in the method quotient() DivideByZero exception thrown in the method quotient() public double quotient(int num, int den) throws DivideByZeroException { if (den==0) throw(new DivideByZeroException()); return (double) num/(double) den; }

27 public void actionPerformed(ActionEvent evt) { try { int numerator=Integer.parseInt(inputNum.getText()); int denominator=Integer.parseInt(inputDen.getText()); double result=quotient(numerator,denominator); outputQuotient.setText(precision.format(result)); } catch(NumberFormatException e) { JOptionPane.showMessageDialog(this," Enter 2 integers","Invalid integer format!“, JOptionPane.ERROR_MESSAGE); } catch(DivideByZeroException e) { JOptionPane.showMessageDialog(this,"The denominator must be non-zero",e.toString(), JOptionPane.ERROR_MESSAGE); }

28 The exceptions are caught in the event handler class (called when ENTER is pressed from the output result text field) The exceptions are caught in the event handler class (called when ENTER is pressed from the output result text field)  Method quotient() throws the user defined exception DivideByZero exception  Method Integer.parseInt() throws the NumberFormatException exception Notice how there may be several catch() clauses to catch multiple exceptions Notice how there may be several catch() clauses to catch multiple exceptions

29 The stream class hierarchy Java has 2 abstract base classes, InputStream and OutputStream for representing input and output stream objects Java has 2 abstract base classes, InputStream and OutputStream for representing input and output stream objects  InputStream and OutputStream are base classes of a large hierarchy (more than 60!) of stream classes  For example, classes FileInputStream and FileOutputStream allow input and output streams to be attached to files Java also has 2 other abstract base classes, Reader and Writer for handling Unicode character based I/O Java also has 2 other abstract base classes, Reader and Writer for handling Unicode character based I/O

30 InputStream ……… FileInputStream OutputStream ……… FileOutputStream

31 Reader ……… Writer ………

32 Simple file i/o The classes FileInputStream and FileOutputStream give input and output to streams attached to files The classes FileInputStream and FileOutputStream give input and output to streams attached to files FileInputStream fin=new FileInputStream(“myFile.dat”); File f=new File(“myFile.dat”); FileInputStream fin=new FileInputStream(f);  This attaches an input stream to a named file  Alternatively, we can use the File class

33 FileInputStream myFile.dat byte stream

34 FileInputStream has a method read() which returns the byte read and –1 if it reaches the end of the input stream FileInputStream has a method read() which returns the byte read and –1 if it reaches the end of the input stream public int read() throws IOException Similarly, FileOutputStream has a method write() to write 1 byte to the output stream Similarly, FileOutputStream has a method write() to write 1 byte to the output stream  Both read() and write() block the thread until the byte is read or written  This often occurs when a network connection is being accessed and it is busy

35 Example The following program makes a straight copy of a file and prints out the number of bytes copies The following program makes a straight copy of a file and prints out the number of bytes copies  File names provided on the command line java FileCopyTest input_file output_file  The FileInputStream constructor throws an IOException if the file doesn’t exist  FileInputStream.read() returns a –1 at the end of file  We could call this method from our own which throws an EOFException

36 import java.io.*; public class FileCopyTest { public static void main(String[] args) { String inFile=args[0]; String outFile=args[1]; int nbytes=0; try { FileInputStream fin=new FileInputStream(inFile); FileOutputStream fout=new FileOutputStream(outFile); int b=fin.read(); do { fout.write(b); nbytes++; b=fin.read(); } while (b!=-1); } catch (IOException e) { System.out.println("i/o problem!");} System.out.println(nbytes + " bytes copied"); }

37 Layering streams – reading/writing formatted files We often want to read files containing formatted data We often want to read files containing formatted data  For example, a file of integers or doubles Our FileInputStream has no methods which return numeric types – it can only read bytes Our FileInputStream has no methods which return numeric types – it can only read bytes We need to combine our FileInputStream and FileOutputStream objects with other types of stream objects in order to read formatted files We need to combine our FileInputStream and FileOutputStream objects with other types of stream objects in order to read formatted files  A combination of different stream types are known as filtered streams

38 A DataInputStream object can be attached to a FileInputStream object as follows A DataInputStream object can be attached to a FileInputStream object as follows FileInputStream fin=new FileInputStream(“myFile.dat”); DataInputStream din=new DataInputStream(fin); din.readDouble();.

39 DataInputStreamFileInputStream formatted input stream Formatted file

40 Example The following program writes integers and doubles to a file and then reads them back The following program writes integers and doubles to a file and then reads them back  Uses DataInputStream.readInt() and DataInputStream.readDouble() for reading  Uses DataOutputStream.writeInt() and DataOutputStream.writeDouble() for writing Catches an end of file exception Catches an end of file exception

41 public class FormattedFileTest { public static void main(String[] args) { String fname=args[0]; try { FileOutputStream fout=new FileOutputStream(fname); DataOutputStream dout=new DataOutputStream(fout); for (int i=0; i<100; i++) { dout.writeInt(i); dout.writeDouble(i*0.5); } } catch (IOException e) { System.out.println("i/o problem!");} try { FileInputStream fin=new FileInputStream(fname); DataInputStream din=new DataInputStream(fin); for (int j=0; j<150; j++) { int i=din.readInt(); double d=din.readDouble(); } } catch (EOFException e) { System.out.println("EOF!");} catch (IOException e) { System.out.println("i/o problem!");} }

42 Reading and writing text files All of our classes so far have been for binary files All of our classes so far have been for binary files  Data is stored in files as bytes which is efficient but not humanly readable A text file consists of information stored in humanly readable form A text file consists of information stored in humanly readable form  For example the number 150 would be stored as ‘1’ ‘5’ ‘0’ instead of the binary representation of 150 ( ) Java has a number of classes (descended from the abstract Reader and Writer classes) for handling text i/o Java has a number of classes (descended from the abstract Reader and Writer classes) for handling text i/o

43 Outputting to a text file This can easily be done with the PrintStream class This can easily be done with the PrintStream class Both text and numeric data can be output using the print() and println() methods Both text and numeric data can be output using the print() and println() methods  Contains overloaded methods print(char c), print(int i), print(double d), etc Uses a filter class (FilterOutputStream) to convert from Unicode to readable characters Uses a filter class (FilterOutputStream) to convert from Unicode to readable characters

44 The following program segment outputs string and numeric text to a text file The following program segment outputs string and numeric text to a text file  Be clear about the difference between this and the previous program using a binary file. try { FileOutputStream fout=new FileOutputStream(fname); PrintStream out=new PrintStream(fout,true); out.println(“Output data coming up”); for (int j=0; j<150; j++) { out.print(j); out.println(j*0.5); } catch (IOException e) { System.out.println("i/o problem!");}.

45 Console-based input Reading data input from the command line in Java is not easy Reading data input from the command line in Java is not easy The System class has standard output, input and error (just like C) which are already open when the program executes The System class has standard output, input and error (just like C) which are already open when the program executes  These can be used in console-based i/o

46 Console based output is easy Console based output is easy  We can use the methods of PrintStream class System { private static InputStream in; static PrintStream out; static PrintStream err;. }. System.out.println(“Hello World!”); System.out.println(“x= ” + x);.

47 Console based input is more difficult Console based input is more difficult  A class Console has been written by the authors of the Core Java book providing convenient input methods  readLine() returns a string  readInt() returns an integer  readDouble() returns a double  readInt() and readDouble() use readLine() to read an input string and use string parsing to extract the numerical value

48 public class Console { public static readLine() {… reads a string from the keyboard…} public static double readDouble(string prompt) {… reads a double from the keyboard …} public static readInt(String prompt) {… reads an integer from the keyboard…} }

49 public static String readLine() { int ch; String r = ""; boolean done = false; while (!done) { try { ch = System.in.read(); if (ch < 0 || (char)ch == '\n') done = true; else if ((char)ch != '\r') r = r + (char) ch; } catch(java.io.IOException e) { done = true; } return r; }

50 The class is used as follows : The class is used as follows : import Console.*; class ConsoleTest { static void main(String args[]) { int i=Console.readInt("Input an integer: "); double d=Console.readDouble("Input a double: "); System.out.println("Values entered= "+ i + " " + d); }

51 And finally……… Java handling of files and streams is extensive Java handling of files and streams is extensive  We have only just scratched the surface! Lots of other facilities are provided within the various classes Lots of other facilities are provided within the various classes  Buffered i/o  Random access files  Object streams – serialization See chapter 12 in Core Java (Vol. 1) See chapter 12 in Core Java (Vol. 1) Java in Easy Steps (Amazon) Java in Easy Steps (Amazon) Java in Easy Steps (Amazon) Java in Easy Steps (Amazon)


Download ppt "EE2E1. JAVA Programming Lecture 7 Files and Streams."

Similar presentations


Ads by Google