Академический Документы
Профессиональный Документы
Культура Документы
Streams
Java Uses the concept of Streams to represent the ordered sequence of data, a common
characteristic shared by all I/O devices.
Streams presents a uniform, easy to use, object oriented interface between the program and I/O
devices.
A stream in Java is a path along which data flows (like a river or pipe along which water flows).
Stream Types
The concepts of sending data from one stream to another (like a pipe feeding into another pipe)
has made streams powerful tool for file processing.
Stream: an object that either delivers data to its destination (screen, file, etc.) or that takes data
from a source (keyboard, file, etc.)
it acts as a buffer between the data source and destination
Connecting streams can also act as filters.
writes
Streams are classified into two basic types:
Input Steam
Output Stream
1
Input stream: a stream that provides input to a program
System.in is an input stream
Output stream: a stream that accepts output from a program
System.out is an output stream
A stream connects a program to an I/O object
System.out connects a program to the screen
System.in connects a program to the keyboard
Streams
Byte Stream
Byte streams are defined using two class hierarchies, InputStream and OutputStream
Two of the most important are read() and write().
2
InputStream
the abstract class that describes stream input.
To use it, we must define
InputStream is = new BufferedInputStream(System.in);
is.read();
The InputStream class defines methods for performing input functions such as
Reading Bytes
Closing Streams
Marking positions in Streams
Skipping ahead in a stream
Finding the number of bytes in a stream.
Byte Input Streams – operations
public abstract int read() Reads a byte and returns as a integer 0-255
public int read(byte[] buf, int offset, int Reads and stores the bytes in buf starting at offset.
count) Count is the maximum read.
public int available() Returns the number of bytes that can be read.
Example
public void write(byte[] buf, int offset, int count) Write count bytes starting from offset in buf.
3
public void flush() Flushes the stream.
import java.io.*;
class ReadWrite {
public static void main(string[] args)
throws IOException
{
int b;
while (( b = System.in.read()) != -1)
{
System.out.write(b);
}
}
Character Stream Classes
Character streams are defined by using two class hierarchies.
The two abstract classes are
Reader Stream Classes and
Writer Stream Classes
These abstract classes handle Unicode character streams.
Reader Class Hierarchy
Writer
This is an abstract class that describes character stream output.
The format is:
Writer w = new OutputStreamer(System.out);
w.write(“This is the output”); //prepares to send
w.flush(); // send it out
Example
4
Using Streams
Table 16.3
List of Tasks and Classes implementing them
Other Useful I/O Classes
Specialized functions
Random Access File
Stream Tokenizer
Implementation of the RandomAccess File
Creation of Files
A filename is a unique string of characters that helps to identify file on the disk.
2 parts
1. Primary name
2. Optional period with extension
Examples: Input.data , Rand.dat
A file stream can be defined using the classes of
Reader/InputStream for reading data
Writer/OutputStream for writing data
5
Creation of Files
There are 2 ways of initialising file stream objects:
Passing file name (directly) to the stream constructor
Passing File Object (indirectly) :
Manipulation operations are same once the file is opened.
6
Class DataOutput
Interface
FilterOutputStream
DataOutputStream
A Data stream for Input can be created as,
FileInputStream fis = new FileInputStream(infile);
DataInputStream dis = new DataInputStream(fis);
A Data stream for Output can be created as,
FileOutputStream fos = new FileOutputStream(infile);
DataInputStream dos = new DataInputStream(fis);
Example Program : 16.5 R/W Primitive Data
dos fos
fis dis
7
Random Access Files
sequential files –used for storing data and accessed (read/write) them in sequence.
In most real world applications, it is necessary to access data in non-sequential order (e.g,
banking system) and append new data or update existing data.
Java .io package supports RandomAccessFile class that allow us to create files that can be used
for reading and/or writing with random access.