Operations on Files
Before starting its processing, a program needs to read input from the input resources and after its processing; the program needs to send output to the output resources. This is called input-output (IO) operations. File based IO allows program to store data permanently on secondary storage in a file and use it whenever there is a need. File stream provides methods to perform operations on a file for File based IO.
Two main operations that users need to perform on a file are File Input operation to read data from the file and File Output operation to write data back to the file. File input-output operations can be performed on both types of files, i.e. On Character files and On Binary files. The processing to input and output causes some overhead and needs to be synchronized with the flow of a program. This synchronization is achieved through the use of buffering. So, before learning more about the operations on a file, let us know about buffer and buffering of input output. Buffer can be applied on both input and output using Input Buffer and Output Buffer.
Input buffer is used to read a large amount of data from the stream. It is accessed by program when needed. When buffer is emptied, again it reads data from the stream. Output buffer is used to store data that is to be written to the stream. Once the buffer is full, data is sent to the stream all at once. After sending data to the stream, buffer is emptied to receive more data. Synchronization of data transfer between the file and program for input and output is achieved using buffering. Different Buffer streams are used for different types of files and operations. BufferedInputStream and BufferedOutputStream are used with binary file streams for input and output respectively.BufferedReader and BufferedWriter are used with character file streams for Input and Output respectively.