10.1 Introduction to File Input and Output

Data stored in variables and arrays is temporary — it’s lost when the program terminates. Java allows a program to read data from a file or write data to a file. Once the data is saved in a file on computer disk, it will remain there after the program stops running. The data can then be retrieved and used at a later time.

There are two types of files in Java - text files and binary files.

A text file is processed as a sequence of characters. In a text file there is a special end-of-line symbol that divides file into lines. In addition you can think that there is a special end-of-file symbol that follows the last component in a file. A big advantage of text files is that it may be opened and viewed in a text editor such as Notepad.

A binary file stores data that has not been translated into character form. Binary files typically use the same bit patterns to represent data as those used to represent the data in the computer's main memory. These files are called binary files because the only thing they have in common is that they store the data as sequences of zeros and ones.

Steps to process file Input/Output in Java :

  1. Import the necessary classes from the packages into the program.
  2. Open the file. To open a file, create and associate the appropriate objects with the input/output sources.
  3. Read / write data to the file.
  4. Close the file, when the program is finished.