The Java IO System Different kinds of IO Different kinds of operations Files, blocks of memory, network connections Different kinds of operations Sequential, random-access, binary, character, by lines, by words, etc.
Typical Uses of IO System Reading and writing files Manipulating blocks of memory Network Programming
Streams, Readers & Writers Java’s stream, reader, and writer classes view input and output as ordered sequences of bytes. Dealing strictly with bytes would be tremendously bothersome (data appears as bytes, or ints, or floats and so on) So we need low-level stream and high-level stream.
Low-level Streams Low-level output stream receives bytes and writes bytes to an output device. Low-level input stream read bytes from an input device and returns bytes to its caller. Example: FileInputStream & FileOutputStream ByteArrayInputStream & ByteArrayOutputStream
High-level Streams A high-level output stream receives general-format data, (ex: primitives), and writes bytes to: a low-level output stream , or another high-level output stream A high-level input stream reads bytes from: a low-level input stream, or another high-level input stream and returns general format data to its caller
High-level Streams Examples: DataInputStream & DataOutputStream BufferedInputStream & BufferedOutputStream
High-level & Low-level Streams DataOutputStream DataInputStream FileOutputStream FileInputStream writeByte() readByte() writeInt() readInt() readUTF() writeUTF() file Etc... Etc…. BufferedOutputStream BufferedInputStream
High-level & Low-level Streams Example: FileOutputStream fo = new FileOutputStream (“my.data”); BufferedOutputStream bo = new BufferedOutputStream (fo); DataOutputStream do = new DataOutputStream (bo); do.writeChar (‘A’); do.writeInt (32.5);
Reader and Writer Classes Internationalization: uses 16-bit char instead of 8-bit byte Also designed to improve speed Sometimes they can replace InputStream and OutputStream
Low-level Readers & Writers FileReader & FileWriter CharArrayReader & CharArrayWriter StringReader & StringWriter etc….
High-level Readers & Writers BufferedReader & BufferedWriter InputStreamReader & OutputStreamWriter LineNumberReader PrintWriter etc….
Readers and Writers PrintWriter OutputStreamWriter FileOutputStream Text File BufferedWriter
Readers and Writers PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter( new FileOutputStream (“TextFile”)))); out.println(str);