1 / 11

Overview of Java I/O

Overview of Java I/O. Stream. A Program often needs to read/write information from/to outer source/destination . Outer source/destination can be : A File in a disk A Network connection(especially socket connction) Other memory location(array)

marika
Download Presentation

Overview of Java I/O

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Overview of Java I/O

  2. Stream • A Program often needs to read/write information from/to outer source/destination. • Outer source/destination can be : • A File in a disk • A Network connection(especially socket connction) • Other memory location(array) • Stream is a abstract connection between outer world and your Java program. As the word “Stream” indicates, it has a direction; incoming or outgoing. • Through incoming stream a sequence of byte or character is flowed into your program. • Through outgoing stream a sequence of byte of character is flushed out to outer destination.

  3. consists of one or more streams in java.io package

  4. What Stream means to Programmer? Reading: open a stream while more information in the stream read information close the stream Writing: open a stream while more information write information to the stream close the stream

  5. Byte Oriented Stream • For reading and writing bytes, Java provides two base classes; InputStream and OutputStream. InputStream abstract int read() int read( byte[] b) int read(byte[] b, int off, int len) int available() void close(), and some more ... OutputStream abstract void write(int b) void write(byte[] b) void write(byte[], int off, int len) void close(); void flush();

  6. Character Oriented Stream • For reading and writing characters, Java provides two base classes; Reader and Writer. Reader abstract int read(char[] cbuf, int off, int len) int read() int read(char[] cbuf) abstract void close(), and some more ... Writer abstract void write(char[] cbuf, int off, int len) void write(int c) void write(String str) void write(String str, int off, int len) abstract void close(); abstract void flush();

  7. What are all those stream classes for? • See Figure 12-1 and 12-2 in page 628 and 629 of the text book and you’ll find so many classes are derived from base class. • Do not be intimidated by its size!!! No one expect you to remember all the details. • Your life will be much easier when you keep the following in mind. We can classify each class in java.io package into two categories: • sink stream: A stream which directrly interact with source/destination • processing stream: A stream which performs some special processing on top of a sink or another processing stream.

  8. What’s Processing Streams for? • Recall that InputStream/Reader defines the methods to read series of bytes or characters only. • Generally sink streams does not offer more methods than its base classes. Then consider the followings. • What if you want to read integer data from stream? • What if you want to read an entire line at a time? • What if you want to open and read compressed file? • Etc. • All these and many other features are made possible by using a series of processing streams.

  9. source program Processing Streams Sink Stream program destination Processing Streams Sink Stream

  10. import java.io.*; public class IOWrite { public static void main(String args[]) { try { DataOutputStream out = new DataOutputStream( new FileOutputStream(args[0])); }catch (IOException ioe) { System.out.println(“io error”); } out.writeInt(4); out.writeFloat((float)3.4); out.writeChar('h'); out.flush(); out.close(); } }

  11. import java.io.*; public class IORead { public void static main(String args[]) { try { DataInputStream in = new DataInputStream( new FileInputStream(args[0])); System.out.println("first integer " + in.readInt()); System.out.println("first float " + in.readFloat()); System.out.println("first char " + in.readChar()); } catch(FileNotFoundException fnf){ System.out.println("can't open "); } catch(IOException ioe) { System.out.println("io error"); } } }

More Related