Download presentation
Presentation is loading. Please wait.
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
Byte-level communication is represented in Java by data streams. 10001101 A Data Stream Provided that the data stream is constructed correctly, what goes in one end comes out the other.
5
Streams may be chained together, to provide additional functionality and an easier and more manageable interface. 'a' 111101 Two Data Streams
6
Streams are divided into two categories: Input streams Output streams 111101
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); 1024 FileOutput Stream DataOutput Stream 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); 1024 FileInput Stream DataInput Stream File int bytes
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.