Simple Java I/Osgauch/3193/notes/fileio.pdf• An InputStreamReader is a kind of Reader • A...

32
Simple Java I/O Part I General Principles

Transcript of Simple Java I/Osgauch/3193/notes/fileio.pdf• An InputStreamReader is a kind of Reader • A...

Page 1: Simple Java I/Osgauch/3193/notes/fileio.pdf• An InputStreamReader is a kind of Reader • A FileReader is a kind of InputStreamReader The LineWriter class class LineWriter { PrintWriter

Simple Java I/O 

Part I General Principles 

Page 2: Simple Java I/Osgauch/3193/notes/fileio.pdf• An InputStreamReader is a kind of Reader • A FileReader is a kind of InputStreamReader The LineWriter class class LineWriter { PrintWriter

Streams 

•  All modern I/O is stream‐based •  A stream is a connec8on to a source of data or to a des8na8on for data (some8mes both) 

•  An input stream may be associated with the keyboard 

•  An input stream or an output stream may be associated with a file  

•  Different streams have different characteris8cs: –  A file has a definite length, and therefore an end –  Keyboard input has no specific end 

Page 3: Simple Java I/Osgauch/3193/notes/fileio.pdf• An InputStreamReader is a kind of Reader • A FileReader is a kind of InputStreamReader The LineWriter class class LineWriter { PrintWriter

How to do I/O 

  import java.io.*; 

•  Open the stream 

•  Use the stream (read, write, or both) 

•  Close the stream 

Page 4: Simple Java I/Osgauch/3193/notes/fileio.pdf• An InputStreamReader is a kind of Reader • A FileReader is a kind of InputStreamReader The LineWriter class class LineWriter { PrintWriter

Why Java I/O is hard 

•  Java I/O is very powerful, with an overwhelming number of op8ons 

•  Any given kind of I/O is not par8cularly difficult 

•  The trick is to find your way through the maze of possibili8es 

open use close 

Page 5: Simple Java I/Osgauch/3193/notes/fileio.pdf• An InputStreamReader is a kind of Reader • A FileReader is a kind of InputStreamReader The LineWriter class class LineWriter { PrintWriter

Opening a stream 

•  There is data external to your program that you want to get, or you want to put data somewhere outside your program 

•  When you open a stream, you are making a connec8on to that external place 

•  Once the connec8on is made, you forget about the external place and just use the stream 

open use close 

Page 6: Simple Java I/Osgauch/3193/notes/fileio.pdf• An InputStreamReader is a kind of Reader • A FileReader is a kind of InputStreamReader The LineWriter class class LineWriter { PrintWriter

Example of opening a stream •  A FileReader is used to connect to a file that will be used for input:    FileReader fileReader =

new FileReader(fileName);

•  The fileName specifies where the (external) file is to be found 

•  You never use fileName again; instead, you use fileReader

open use close 

Page 7: Simple Java I/Osgauch/3193/notes/fileio.pdf• An InputStreamReader is a kind of Reader • A FileReader is a kind of InputStreamReader The LineWriter class class LineWriter { PrintWriter

Using a stream 

•  Some streams can be used only for input, others only for output, s8ll others for both 

•  Using a stream means doing input from it or output to it 

•  But it’s not usually that simple‐‐you need to manipulate the data in some way as it comes in or goes out 

open use close 

Page 8: Simple Java I/Osgauch/3193/notes/fileio.pdf• An InputStreamReader is a kind of Reader • A FileReader is a kind of InputStreamReader The LineWriter class class LineWriter { PrintWriter

Example of using a stream 

  int ch; ch = fileReader.read( );

•  The fileReader.read() method reads one character and returns it as an integer, or -1 if there are no more characters to read 

•  The meaning of the integer depends on the file encoding (ASCII, Unicode, other) 

open use close 

Page 9: Simple Java I/Osgauch/3193/notes/fileio.pdf• An InputStreamReader is a kind of Reader • A FileReader is a kind of InputStreamReader The LineWriter class class LineWriter { PrintWriter

Manipula8ng the input data 

•  Reading characters as integers isn’t usually what you want to do 

•  A BufferedReader will convert integers to characters; it can also read whole lines 

•  The constructor for BufferedReader  takes a FileReader parameter:    BufferedReader bufferedReader =

new BufferedReader(fileReader);

open use close 

Page 10: Simple Java I/Osgauch/3193/notes/fileio.pdf• An InputStreamReader is a kind of Reader • A FileReader is a kind of InputStreamReader The LineWriter class class LineWriter { PrintWriter

Reading lines 

  String s; s = bufferedReader.readLine( );

•  A BufferedReader will return null if there is nothing more to read 

open use close 

Page 11: Simple Java I/Osgauch/3193/notes/fileio.pdf• An InputStreamReader is a kind of Reader • A FileReader is a kind of InputStreamReader The LineWriter class class LineWriter { PrintWriter

Closing 

•  A stream is an expensive resource •  There is a limit on the number of streams that you can have open at one 8me 

•  You should not have more than one stream open on the same file 

•  You must close a stream before you can open it again 

•  Always close your streams! 

open use close 

Page 12: Simple Java I/Osgauch/3193/notes/fileio.pdf• An InputStreamReader is a kind of Reader • A FileReader is a kind of InputStreamReader The LineWriter class class LineWriter { PrintWriter

Simple Java I/O 

Part II LineReader and LineWriter 

Page 13: Simple Java I/Osgauch/3193/notes/fileio.pdf• An InputStreamReader is a kind of Reader • A FileReader is a kind of InputStreamReader The LineWriter class class LineWriter { PrintWriter

Text files 

•  Text (.txt) files are the simplest kind of files –  text files can be used by many different programs 

•  FormaYed text files (such as .doc files) also contain binary formaZng informa8on 

•  Only programs that “know the secret code” can make sense formaYed text files 

•  Compilers, in general, work only with text 

Page 14: Simple Java I/Osgauch/3193/notes/fileio.pdf• An InputStreamReader is a kind of Reader • A FileReader is a kind of InputStreamReader The LineWriter class class LineWriter { PrintWriter

My LineReader class 

class LineReader { BufferedReader bufferedReader;

LineReader(String fileName) {...}

String readLine( ) {...}

void close( ) {...} }

Page 15: Simple Java I/Osgauch/3193/notes/fileio.pdf• An InputStreamReader is a kind of Reader • A FileReader is a kind of InputStreamReader The LineWriter class class LineWriter { PrintWriter

Basics of the LineReader constructor 

•  Create a FileReader for the named file:    FileReader fileReader =

new FileReader(fileName);

•  Use it as input to a BufferedReader:    BufferedReader bufferedReader =

new BufferedReader(fileReader);

•  Use the BufferedReader; but first, we need to catch possible Exceptions 

Page 16: Simple Java I/Osgauch/3193/notes/fileio.pdf• An InputStreamReader is a kind of Reader • A FileReader is a kind of InputStreamReader The LineWriter class class LineWriter { PrintWriter

The full LineReader constructor 

LineReader(String fileName) { FileReader fileReader = null; try { fileReader = new FileReader(fileName); } catch (FileNotFoundException e) { System.err.println ("LineReader can't find input file: " + fileName); e.printStackTrace( ); } bufferedReader = new BufferedReader(fileReader); }

Page 17: Simple Java I/Osgauch/3193/notes/fileio.pdf• An InputStreamReader is a kind of Reader • A FileReader is a kind of InputStreamReader The LineWriter class class LineWriter { PrintWriter

readLine 

String readLine( ) { try { return bufferedReader.readLine( ); } catch(IOException e) { e.printStackTrace( ); } return null; }

Page 18: Simple Java I/Osgauch/3193/notes/fileio.pdf• An InputStreamReader is a kind of Reader • A FileReader is a kind of InputStreamReader The LineWriter class class LineWriter { PrintWriter

close 

void close() { try { bufferedReader.close( ); } catch(IOException e) { } }

Page 19: Simple Java I/Osgauch/3193/notes/fileio.pdf• An InputStreamReader is a kind of Reader • A FileReader is a kind of InputStreamReader The LineWriter class class LineWriter { PrintWriter

How did I figure that out? 

•  I wanted to read lines from a file •  I found a readLine method in the 

BufferedReader class •  The constructor for BufferedReader takes a 

Reader as an argument •  An InputStreamReader is a kind of Reader •  A FileReader is a kind of InputStreamReader 

Page 20: Simple Java I/Osgauch/3193/notes/fileio.pdf• An InputStreamReader is a kind of Reader • A FileReader is a kind of InputStreamReader The LineWriter class class LineWriter { PrintWriter

The LineWriter class 

class LineWriter { PrintWriter printWriter;

LineWriter(String fileName) {...}

void writeLine(String line) {...}

void close( ) {...} }

Page 21: Simple Java I/Osgauch/3193/notes/fileio.pdf• An InputStreamReader is a kind of Reader • A FileReader is a kind of InputStreamReader The LineWriter class class LineWriter { PrintWriter

The constructor for LineWriter 

LineWriter(String fileName) { try { printWriter = new PrintWriter( new FileOutputStream(fileName), true); } catch(Exception e) { System.err.println("LineWriter can't " + "use output file: " + fileName); } }

Page 22: Simple Java I/Osgauch/3193/notes/fileio.pdf• An InputStreamReader is a kind of Reader • A FileReader is a kind of InputStreamReader The LineWriter class class LineWriter { PrintWriter

Flushing the buffer 

•  When you put informa8on into a buffered output stream, it goes into a buffer 

•  The buffer may not be wriYen out right away 

•  If your program crashes, you may not know how far it got before it crashed 

•  Flushing the buffer is forcing the informa8on to be wriYen out 

Page 23: Simple Java I/Osgauch/3193/notes/fileio.pdf• An InputStreamReader is a kind of Reader • A FileReader is a kind of InputStreamReader The LineWriter class class LineWriter { PrintWriter

PrintWriter 

•  Buffers are automa8cally flushed when the program ends normally 

•  Usually it is your responsibility to flush buffers if the program does not end normally 

•  PrintWriter can do the flushing for you    public PrintWriter(OutputStream out,

boolean autoFlush) 

Page 24: Simple Java I/Osgauch/3193/notes/fileio.pdf• An InputStreamReader is a kind of Reader • A FileReader is a kind of InputStreamReader The LineWriter class class LineWriter { PrintWriter

writeLine 

void writeLine(String line) { printWriter.println(line); }

Page 25: Simple Java I/Osgauch/3193/notes/fileio.pdf• An InputStreamReader is a kind of Reader • A FileReader is a kind of InputStreamReader The LineWriter class class LineWriter { PrintWriter

close 

void close( ) { printWriter.flush( ); try { printWriter.close( ); } catch(Exception e) { } }

Page 26: Simple Java I/Osgauch/3193/notes/fileio.pdf• An InputStreamReader is a kind of Reader • A FileReader is a kind of InputStreamReader The LineWriter class class LineWriter { PrintWriter

Simple Java I/O 

Part III Serializa=on 

Page 27: Simple Java I/Osgauch/3193/notes/fileio.pdf• An InputStreamReader is a kind of Reader • A FileReader is a kind of InputStreamReader The LineWriter class class LineWriter { PrintWriter

Serializa8on 

•  You can also read and write objects to files •  Object I/O goes by the awkward name of serializa8on 

•  Serializa8on in other languages can be very difficult, because objects may contain references to other objects 

•  Java makes serializa8on (almost) easy 

Page 28: Simple Java I/Osgauch/3193/notes/fileio.pdf• An InputStreamReader is a kind of Reader • A FileReader is a kind of InputStreamReader The LineWriter class class LineWriter { PrintWriter

Condi8ons for serializability 

•  If an object is to be serialized: – The class must be declared as public 

– The class must implement Serializable – The class must have a no‐argument constructor – All fields of the class must be serializable: either primi8ve types or serializable objects 

Page 29: Simple Java I/Osgauch/3193/notes/fileio.pdf• An InputStreamReader is a kind of Reader • A FileReader is a kind of InputStreamReader The LineWriter class class LineWriter { PrintWriter

Implemen8ng the Serializable interface 

•  To “implement” an interface means to define all the methods declared by that interface, but... 

•  The Serializable interface does not define any methods! –  Ques8on: What possible use is there for an interface that does not declare any methods? 

–  Answer: Serializable is used as flag to tell Java it needs to do extra work with this class 

Page 30: Simple Java I/Osgauch/3193/notes/fileio.pdf• An InputStreamReader is a kind of Reader • A FileReader is a kind of InputStreamReader The LineWriter class class LineWriter { PrintWriter

Wri8ng objects to a file 

  ObjectOutputStream objectOut = new ObjectOutputStream( new BufferedOutputStream( new FileOutputStream(fileName)));

  objectOut.writeObject(serializableObject);

  objectOut.close( ); 

open use close 

Page 31: Simple Java I/Osgauch/3193/notes/fileio.pdf• An InputStreamReader is a kind of Reader • A FileReader is a kind of InputStreamReader The LineWriter class class LineWriter { PrintWriter

Reading objects from a file 

  ObjectInputStream objectIn = new ObjectInputStream( new BufferedInputStream( new FileInputStream(fileName)));

  myObject = (itsType)objectIn.readObject( );

  objectIn.close( ); 

open use close 

Page 32: Simple Java I/Osgauch/3193/notes/fileio.pdf• An InputStreamReader is a kind of Reader • A FileReader is a kind of InputStreamReader The LineWriter class class LineWriter { PrintWriter

What have I leb out? 

•  Encrypted files, compressed files, files sent over internet connec8ons, ... 

•  Excep8ons! All I/O involves Excep8ons! •  try { statements involving I/O }

catch (IOException e) { e.printStackTrace ( ); }