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 :
- Import the necessary classes from the packages into the program.
- Open the file. To open a file, create and associate the appropriate objects with the input/output sources.
- Read / write data to the file.
- Close the file, when the program is finished.