0% found this document useful (0 votes)
12 views5 pages

Streams

Uploaded by

220701299
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views5 pages

Streams

Uploaded by

220701299
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

Input Output (I/O) Basics

Input:

- A data/information provided by users through an input device to Java programs.


Example: users provide password input to login to their system.

Input Devices:

- Devices that are used to provide input to Java programs.


Example: Keyboard, Mouse

Output:

- A data/information provided by Java programs to output devices.


Example: Write user information into a file.

Output Devices:

- Devices that are used to provide output for Java programs.


Example: Disk Files, Console.

2. Stream:

- Is an abstraction that either produces or consumes information


- Is a logical connection of sequence of data that either
a. Reads input from sources.
 Example Sources: Keyboard, Mouse.
b. Write output to destination.
 Example destination: Disk files, Java arrays, Network Socket.
- A stream is linked to a physical device by the Java I/O system.
- Java programs perform I/O through streams.

3. Types of Streams:

1. Byte Streams
a. Provide a convenient means for handling input and output of bytes.
b. Used when reading or writing binary data.
c. Suitable for: Save videos, audios, characters
d. Available in java.io package.
2. Character Streams
a. Provide a convenient means for handling input and output of characters.
b. Read and write 16-bit Unicode data.
c. Suitable for: read text file.
d. Available in java.io package.
4. Differences: Char vs Byte Streams

5. Class Hiererachy:

a. Possible Read and Write Streams

1. File
2. Array
3. Object
4. Pipe (network)
5. Data (Buffer & Filter)
b. Byte Streams

Byte Stream Classes Description


- Obtain input bytes from a file in a file system.
FileInputStream
- Meant for reading streams of raw bytes such as image data.
- Reads from a byte array.
- Retain bytes read from the input stream using its internal buffer.
ByteArrayInputStream
- An internal counter keeps track of the next byte to be supplied by
the read method.
- Represent a logical concatenation of multiple input streams.
- Read streams data sequentially, one after the other.
SequenceInputStream
- Reads are taken from the first stream until it ends, then the next
stream is used until the last stream returns end of file.
- It has some additional input stream used as basic data source.
- These input streams can provide transformation of data and
additional functionality.
FilterInputStream - Implements all methods of InputStream with versions that pass all
requests to the contained input stream.
- Subclasses of this class may further override some of these
methods and may also provide additional methods and fields.
DataInputStream - Used for reading Java standard/primitive data types.
- Provide additional ability to buffer the input and to support the
mark and reset methods. Default buffer size is 8192 bytes.
- As bytes from the stream are read or skipped, the internal buffer
BufferedInputStream
is refilled as necessary from the contained input stream, many
bytes at a time.
- Improve performance due to internal buffer processing.
- Read byte data from output pipes/network.
PipedInputStream
- Before reading, pipes must be connected.
- Deserialize primitive data and objects previously written using an
ObjectInputStream
ObjectOutputStream.

c. Character Streams

Character Stream Description


StringReader - Reads text from a string.
- A bridge from byte streams to character streams.
InputStreamReader - Translate bytes to characters.
- Read bytes and decodes them into characters.
- A bridge from character to byte streams.
OutputStreamWriter - Translate characters to bytes.
- Read characters and decodes them into bytes.
6. Basic File Stream Operations

1. Import the file stream class from java.io.*;


import java.io.FileInputStream;
2. Make a stream connection.
FileInputStream in = new FileInputStream(“test.txt”);
3. Read/write data using read/readLine or write methods.
in.read() OR in.readLine()
4. Close the stream.
in.close()
5. Handle exceptions FileNotFoundException & IOException.
catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("Error occured. File not found");
}

7. File Methods

Stream Methods Description


- Mark the present position in the stream.
- Subsequent calls to reset() will attempt to reposition the
stream to this point.
void mark(int
- Limit on the number of characters that may be read while
limit)
still preserving the mark.
- After reading this many characters, attempting to reset the
stream may fail.
- Reset the stream.
- If the stream has been marked, then attempt to reposition
it at the mark.
void reset()
- If the stream has not been marked, then attempt to reset
it in some way appropriate to the particular stream, for
example by repositioning it to its starting point.
- immediately flush the contents of the buffer to the output
void flush() stream

You might also like