Java I/O • For the next couple of classes we will be Java I/O talking about Java I/O – Last class: basics and low level I/O – This class: “wrappers” and high level I/O Reading, Writing, and stuff – Pt II • All Java I/O classes are defined in the java.io package. A question InputStreamReader • Byte -> character conversion • Converts read bytes to characters – In order to support multiple languages (e.g. English, Japanese, etc), conversion from bytes to characters must be performed. char bytes Text InputStream read() Reader file Bytes-> char Bytes->Char • Default encoding is defined by the Java • Note that FileWriter and FileReader assume System property file.encoding the default encoding – System.getProperty (“file.encoding”) • See me if interested in reading/writing files • This property is during Java installation that are not encoded using the default • You can override this when instantiating an encoding. InputStreamReader or OutputStreamWriter – public InputStreamWriter (InputStream in, String enc) 1
Questions Java I/O • Any other questions from last class? • Low level vs high level – Low level: can only read/write a character or byte at a time – High level: can read/write strings that represent different data types • Ex. read/write an int, float, Streams Streams • Reading from a stream • Basic low level mechanism for I/O in Java – Open a stream is the stream – While more info • Read data – Close the stream • Writing to a stream – Open a stream – While more info • Write data – Close the stream Data and Streams The 4 base Java I/O classes READ WRITE • Types of data that can be read from/written to streams CHAR Reader Writer – Bytes (8-bits / bytes) • Raw data – Characters (16-bits / bytes) • Text data BYTE InputStream OutputStream • Basic stream operations – Read – Write Each of these are abstract classes 2
Wrapper classes Wrapper classes • public class myWrapper { • A class that takes a base class or data item public myWrapper (WrappedClass C) and provides additional methods to manipulate it. // additional methods for manipulating • The new class is said to act as a wrapper for // objects of type Wrapped Class the base class or data item. } Class being wrapped Wrapper classes Wrapper classes and I/O classes • Float, Integer, Double , and • Many subclasses of the 4 base java.io Long classes are wrapper classes: – Are wrapper classes for the basic datatypes of – Add additional functionality float, int, double , and long . – Convert from one format to another – Float F = new Float (5.4f); – Filter the data coming in or going out int i = F.intValue(); • These wrapper subclasses wrap the base System.out.println (F.toString()) java.io classes. I/O Wrapper classes Wrapper classes and I/O classes • Classes wrap both extend base classes and wrap • Added functionality them. – Buffering – Data Conversion public class PrintWriter extends Writer{ – counting (I.e. line numbering) – Pushback public PrintWriter (Writer W) { … } } 3
Why not use inheritance? InputStream Wrappers • Wrapper classes do not define a strict class hierarchy. • Can use many wrappers dependent on what extra functionality you may need. InputStream Wrappers A look at DataInputStream • public DataInputStream extends • BufferedInputStream InputStream{ – Buffers input as it reads. – Designed for efficiency • public DataInputStream (InputStream in) • DataInputStream • boolean readBoolean()throws IOException – Allows binary data to be interpreted a basic data type. • int readInt() throws IOException • float readFloat() throws IOException • PushbackInputStream • double readDouble() throws IOException – Allows one to pushback (or unread ) a byte after it’s • short readShort() throws IOException been read. • char readChar() throws IOException Creating wrapped streams OutputStream Wrappers try { // Binary data coming from a file InputStream in = new FileInputStream(“filename”); // Buffer the data for effiency BufferedInputStream bin = new BufferedInputStream (in); // Add “read-by-type” functionality DataInputStream din = new DataInputStream(bin); // read data by type double d = din.getDouble(); int i = din.getInt(); } catch (IOException E) { ... } 4
OutputStream Wrappers A look at DataOutputStream • BufferedOutputStream • public DataOutputStream extends OutputStream{ – Buffers output as it writes. • public DataOutputStream (OutputStream out) – Designed for efficiency • void writeBoolean(boolean b)throws IOException • DataOutputStream • void writeInt(int i) throws IOException • void writeFloat(float f) throws IOException – Allows basic data types to be written to the stream • void writeDouble(double d) throws IOException • PrintStream • void writeShort(short s) throws IOException – Allows character representation of basic data types to • void writeChar(char c) throws IOException be written to the stream. A look at PrintOutputStream A look at PrintOutputStream � void println (boolean b) • public PrintWriter extends OutputStream{ � void println (int i) � void println (float f) • void print (boolean b) � void println (char c) • void print (int i) � void println (char[] c) • void print (float f) � void println (double d) • void print (char c) � void println (String S) • void print (char[] c) � void println (Object O) • void print (double d) � void println (long l) • void print (String S) • void print (Object O) • void print (long l) Reader Wrappers Reader Wrappers • BufferedReader – Buffers input as it reads. – Designed for efficiency • LineNumberReader – Keeps track of number of lines read – Allows you to read text a line at a time • PushbackReader – Allows one to pushback (or unread ) a character after it’s been read. 5
Reader Wrappers Writer Wrappers • Why there isn’t a DataReader? – Actually, I don’t know…a DataReader would be nice – However, you can always convert text to a basic data type by using valueOf methods: • float Float.valueOf (String S) • int Integer.valueOf (String S) • double Double.valueOf (String S) • boolean Boolean.valueOf (String S) Writer Wrappers PrintWriter - constructors • public PrintWriter(Writer out) • BufferedWriter • public PrintWriter(OutputStream out) – Buffers output as it writes. – Supplied for convenience – Designed for efficiency – Includes a OutputStreamWriter to convert text • PrintWriter data to binary – Allows character representation of basic data types to be written to the stream. • Why is there no DataWriter? A look at PrintWriter A look at PrintWriter � void println (boolean b) • public PrintWriter extends Writer{ � void println (int i) � void println (float f) • void print (boolean b) � void println (char c) • void print (int i) � void println (char[] c) • void print (float f) � void println (double d) • void print (char c) � void println (String S) • void print (char[] c) � void println (Object O) • void print (double d) � void println (long l) • void print (String S) • void print (Object O) • void print (long l) 6
Mixing low level I/O with high Standard in, out, error level I/O • Since PrintWriter extends as well as wraps Writer, • System.in you can use it to do both low and high level I/O: – Defined as a static InputStream try { – Standard input stream PrintWriter P = new PrintWriter • System.out (new FileWriter (“filename”)); – Defined as a static PrintStream int i = 7; – Standard output stream char c = ‘a’; P.println (i); • System.err P.write (c); – Defined as a static PrintStream } – Standard error stream catch (IOException E) { … } Summary Reading lines of text from System.in // System.in is an InputStream, we want • Wrapper classes // read characters, not bytes • Uses for wrapper classes InputStreamReader ir = new InputStreamReader (System.in); – High level data I/O • Wrappers available for Reader, Writer, // We’ll need the ability to read text // lines at a time InputStream, OutputStream BufferedReader br = new BufferedReader (ir); • System.in, System.out, System.err // Now we can read lines of text String curline = br.readLine(); Something to think about for next time Something to think about for next time /** * A test program for the Payroll Class • Change the Payroll app testing function so */ static public void main (String args[]) that Performers are read in from a text file { // Create a payroll or from standard input: Payroll pay = new Payroll(); // Create some actors, define the number of – Format of input (1 line for each): // performances for each then add them to the payroll Actor A = new Actor ("Nathan Lane"); • Name A.perform (8); pay.addPerformer (A); ... • Type (A for actor, G for Guitarist, D for Drummer) // Calculate and print out the total weekly pay • # of performance System.out.println ("The total weekly pay for this week is " + pay.calculateTotalPay()); } 7
Questions? Something to think about for next time • Usage: – java Payroll • will take input from standard in – Java Payroll infile • Will take input from input file infile 8
Recommend
More recommend