Alan Kaminsky
•
Department of Computer Science
•
Rochester Institute of Technology
•
4486
+
2220
= 6706
Home Page
Advanced Programming
•
4003-707-02
•
Fall Quarter 2008
Course Page
4003-707-02 Advanced Programming
Module 6. I/O -- Lecture Notes
Prof. Alan Kaminsky
Rochester Institute of Technology -- Department of Computer Science
Byte-Level Input/Output
Class java.io.File -- Represents a file
Byte-level output
Write a sequence of bytes (numbers in the range 0..255)
Class java.io.OutputStream -- Base class for writing bytes
Class java.io.FileOutputStream -- Subclass for writing bytes to a file
Byte-level input
Read a sequence of bytes (numbers in the range 0..255)
Class java.io.InputStream -- Base class for reading bytes
Class java.io.FileInputStream -- Subclass for reading bytes from a file
Character-Level Input/Output
Convert a sequence of bytes to a sequence of characters (or vice versa) using a
character encoding
ASCII
Unicode
UTF-8
Character-level output
Write a sequence of characters
Class java.io.Writer -- Base class for writing characters
Class java.io.OutputStreamWriter -- Subclass for writing characters to an output stream
Character-level input
Read a sequence of characters
Class java.io.Reader -- Base class for reading characters
Class java.io.InputStreamReader -- Subclass for reading characters from an input stream
High-Level Textual Input/Output
Textual output
Print numbers of various types as human-readable character sequences
Class java.io.PrintStream -- prints using default character encoding
Class java.io.PrintWriter -- prints using a given character encoding
System.out -- A PrintStream for printing on the console ("standard output")
System.err -- A PrintStream for printing on the console ("standard error")
Creating A PrintStream that prints into a file
Textual input
Parse numbers, words, lines, etc. from human-readable character sequences
Class java.util.Scanner
System.in -- An InputStream for reading from the console
Binary Data Input/Output
Read and write values of primitive data types in the exact binary format in which the values are stored inside the CPU
int
-- 4 bytes
double
-- 8 bytes
See class java.io.DataOutputStream for others
Binary data output
Class java.io.DataOutputStream
Binary data input
Class java.io.DataInputStream
Number of bytes written by PrintStream vs. number of bytes written by DataOutputStream
Speeding up I/O for large files
Class java.io.BufferedOutputStream
Class java.io.BufferedInputStream
Object Input/Output
Read and write entire objects as sequences of bytes
Serialization
-- Convert an object to a sequence of bytes
Deserialization
-- Convert a sequence of bytes back to an object
Object output
Class java.io.ObjectOutputStream
The
writeObject()
method
Serializes the object and writes the sequence of bytes to the underlying output stream
Object input
Class java.io.ObjectInputStream
The
readObject()
method
Reads the sequence of bytes from the underlying input stream and deserializes the object
To be written by
writeObject()
or read by
readObject()
:
The object's class must implement interface java.io.Serializable
The object's fields must be primitive types or serializable objects
Advanced Programming
•
4003-707-02
•
Fall Quarter 2008
Course Page
Alan Kaminsky
•
Department of Computer Science
•
Rochester Institute of Technology
•
4486
+
2220
= 6706
Home Page
Copyright © 2008 Alan Kaminsky. All rights reserved. Last updated 16-Oct-2008. Please send comments to ark
@
cs.rit.edu.