Explain the use of Streams.
1. A stream is flow of information. Streams are targeted to handle input and output data.
2. Stream brings data into a program – not just by assigning in the application / program.
3. Java stream opens connection to the data source, such as buffer, console, files, and sockets.
4. Data can be written onto the stream and can be read from the stream.
5. The stream can be a console, file, buffer.
- The following is the classification of streams:
1. Byte Streams
– Used to handle binary data I/O.
2. Character Streams
– Used to handle character data I/O – Unicode characters.
3. Buffered Streams
– Used to optimize the input and output data. Reduces the number of calls to read and write data.
- Allows to read formatted data, like, reading primitive types / strings.
5. Data Streams
– Used to handle binary I/O of primitive data types and strings.
Difference between Stream classes and Reader writer classes Stream Classes
1. They are byte-oriented
2. They never support Unicode characters
3. Supports 8-bit streams
4. The streams supports filtering data into primitive types using DataOutputStream and DataInputStream
5. Object serialization supports byte oriented stream
6. Data over network passes a stream of bytes
7. Available since JDK 1.0 Reader and Writer Classes
1. They are character-oriented
2. The never support byte streams
3. Supports 16-bit Unicode characters.
4. Supports to read a group of characters at once
5. Available since JDK 1.1
Explain and demonstrate the use of File, RandomAccessFile classes. File class
1. It represents an abstraction of files and folders.
2. Represents system independent view of hierarchical paths and files.
3. File related operations such as deleting, renaming, changing properties like read-only etc. can be performed by using File class.
4. It has methods to create directories, list files in a directory. RandomAccessFile
1. The objects of RandomAccessFile can write and read data to and from the files.
2. It has facility to write / read primitive data onto and from the files.
3. Data from any offset can be read, in contrast with other file streams, which supports only sequential access. The following code snippet illustrates to write, append and read data from a file
public class RandomAccessFileExample
public void RAFExample()
RandomAccessFile raf = new RandomAcessFile("books.dat", "rw");
/* instead of books.dat the following File object can also be used
File randFile new File(“books.dat”);
RandomAccessFile raf = new RandomAcessFile(randFile, "rw");
String books = new String;
books = "Professional JSP";
books = "The Java API";
books = "Security in Java";
books = "Java Collections Framework";
books = "JSE for JEE – Professional Edition";
for (int index = 0; index < books.length; index++)
raf.seek(raf.length()); // file pointer at eof
// appends a new record
raf.writeUTF("Servlet & JSP Programming");
raf.seek(0); // file pointer at the beginning
// reads data from the file
while (raf.getFilePointer() < raf.length())
catch (FileNotFoundException e)
catch (IOException e)
Explain the use of Reader and Writer classes.
1. Reader and Writer classes are abstract classes of character streams
2. Both classes have methods to read and write characters into a file
3. The method read() reads a single character and returns an integer. Returns -1 if EOF reached
4. The method write() writes two byte character onto a file.
5. An array of characters can also be written to a file
6. Reader and Writer streams can be implemented for both console and file streams.
7. Buffered streams along with Reader and Writer classes perform well in reading files.