1 / 21

Lab 2

Lab 2. Data Streams. Lab 2: Data Streams. Introduction Reading from an Input Stream Writing to an Output Stream Filter Streams. Introduction. I/O Streams are commonly used in Java for communicating over networks, with files, and between applications.

Télécharger la présentation

Lab 2

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. Lab 2 Data Streams

  2. Lab 2: Data Streams • Introduction • Reading from an Input Stream • Writing to an Output Stream • Filter Streams

  3. Introduction • I/O Streams are commonly used in Java for communicating over networks, with files, and between applications. • Almost all network communication (except UDP communication) is conducted over streams. • Hence, a thorough knowledge of I/O streams is critical for network programming in Java.

  4. 10001101 10001101 A Data Stream • Byte-level communication is represented in Java by data streams. • Provided that the data stream is constructed correctly, what goes in one end comes out the other.

  5. 111101 'a' Two Data Streams • Streams may be chained together, to provide additional functionality and an easier and more manageable interface.

  6. 111101 111101 • Streams are divided into two categories: • Input streams • Output streams

  7. Certainly, you should not try to read data from an output stream or write data to an input stream.

  8. Reading from an Input Stream • Six important low-level input streams: • ByteArrayInputStream • Reads bytes of data from an in-memory array • FileInputStream • Reads bytes of data from a file on the local file system • PipedInputStream • Reads bytes of data from a thread pipe

  9. StringBufferInputStream • Reads bytes of data from a string • SequenceInputStream • Reads bytes of data from two or more low-level streams, switching from one stream to the next when the end of the stream is reached. • System.in • Reads bytes of data from the user console

  10. Blocking I/O is used when reading from an input stream. • Blocking I/O is a term applied to any form of input or output that does not immediately return from an operation. • In certain situations, blocking I/O can cause performance problems. This can be alleviated by using data buffering.

  11. Writing to an Output Stream • Six important low-level output streams: • ByteArrayOutputStream • Writes bytes of data to an array of bytes • FileOutputStream • Writes bytes of data to a file on the local file system • PipedOutputStream • Writes bytes of data to a a communications pipe, which will be connected to a PipedInputStream.

  12. StringBufferOutputStream • Writes bytes to a string buffer • System.err • Writes bytes of data to the error stream of the user console, also known as standard error. • System.in • Writes bytes of data to the user console, also known as standard output.

  13. Like input streams, data is communicated sequentially; the first byte in will be the first byte out.

  14. Filter Streams • The basic low-level streams have limited flexibility. • Filter streams add additional functionality to an existing stream. For example, allowing one to read a line of text instead of reading byte by byte.

  15. Filter streams can be connected to any other stream (low-level stream or another filter stream). • Filter stream classes extend from java.io.FilterInputStream or java.io.FilterOutputStream

  16. Examples of Filter Output Streams • BufferedOutputStream • Uses I/O buffering for output to improve system performance. • Outputs to an internal buffer. Buffer contents are dumped to the output stream when it is full or flushed.

  17. DataOutputStream • Writes primitive data types, such as an int, float, a double, or even a line of text, to an output stream. • PrintStream • Offers additional methods for writing lines of text, and other datatypes as text. • Provides a convenient way to print primitive datatypes as text using the print() and println() method.

  18. Example: FileOutputStream fout; DataOutputStream dos; fout = new FileOutputStream("out"); dos = new DataOutputStream(fout); dos.writeInt(1024); dos.writeFloat(43.235); DataOutput Stream FileOutput Stream 1024 File int bytes

  19. Examples of Filter Input Streams • BufferedInputStream • Uses I/O buffering for input to improve system performance. • Tries to reduce the number of times an application blocks for input by reading bytes in batches into a buffer.

  20. DataInputStream • Reads primitive data types, such as an int, float, a double, or even a line of text, from an input stream.

  21. Example: FileInputStream fin; DataInputStream dis; fin = new FileInputStream("out"); dis = new DataInputStream(fin); int intData = dis.readInt(); float floatData = dis.readFloat(); System.out.println("Int data: "+intData); System.out.println("Float data: "+floatData); DataInput Stream FileInput Stream 1024 File int bytes

More Related