-
Notifications
You must be signed in to change notification settings - Fork 3
Chapter 6 : Streams and File IO
Saket Khopkar edited this page Jan 7, 2022
·
1 revision
- So far we have used variables and arrays for storing data. But this approach has two problems:
- The storage is temporary.
- It is difficult to handle large amount of data.
- Above problems can be solved by storing data on secondary storage using the concept of file. Data stored in the file is called persistent data.
- A file is a collection of related records stored on the disk.
- A record is composed of several fields and a field is a group of characters. Characters in Java are Unicode characters made up of 2 bytes each.
- Storing and managing data using file is known as file processing. It includes:
- Crating files
- Updating files
- Manipulation of data
- Java provides powerful features of input and output of data using file.
- Reading and writing data can be done at:
- Byte level
- Character level
- Java provides capabilities to read and write class object directly.
- The process of reading and writing object is called object serialization.
- A stream is a continuous group of data or a path along which data flows from one point to another.
- Java streams are classified into two basic types:
- Input Stream takes input data from source and moves it into a program.
- Output Stream takes output data generated from program and moves it to a destination.
- A stream presents a uniform, easy to use, object-oriented interface between the program and input / output devices.
- Stream is a powerful file processing tool in Java, which can be used to get data in one format and convert it into another format. For example, one stream can be used to get raw data in binary format and another stream can be used to convert it to integers.
- In file processing, input refers to data flow into a program and output refers to data flow out of a program.
- Standard Input Output Stream in Java represented by three members of System classes:
- System.in : Input Stream
- System.out : Output Stream
- System.err : Error Stream
- The java.io package contains a large number of stream classes that provide capabilities for processing all type of data.
- These classes are of two types:
- Byte stream: Support for I/O operations on byte.
- Character Stream: Support I/O operations on characters.
- These classes are further classified according to their functions.
- provide functional features for creating and manipulating streams and files for reading and writing bytes.
- Byte stream classes handle the data in 8 bit format.
- It contains InputStream and OutputStream as its fundamental classes.
- Streams are unidirectional; hence they can transmit bytes in only one direction.
- It reads data in 8 bit format.
- Here InputStream is a super class and number of subclasses for supporting various input related functions.
- InputStream is an abstract class, so we cannot create its instance.
- InputStream class define 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