Data Files and Streams

Data that is stored in a program is temporary and is lost when the program ends. To permanently store date requires that the data be placed in a file on some type of storage device, such as a disk.

In the real world, computer applications need to store large amounts of data for extended periods of time. Imagine the amount of data stored and accessed by the IRS (Internal Revenue Service). To accomplish the task of storing large amounts of data, data files are used.

A file is simply a container for data. A file may contain data needed by an application, or it may store data generated by an application. A file is separate from the application that is using it, and as such can be accessed by more than one application.

Think of a file as a drawer in a file cabinet which needs to be opened and closed. Before you can put something into a file, or take something out, you must open the file (the drawer). When you are finished using the file, the file (the drawer) must be closed.
file cabinet

File Streams:

Java views input (or output) as a stream of bytes. On input, a program extracts bytes from an input stream. On output, a program inserts bytes into the output stream. The Scanner class, for example, requires an input/output stream. The stream acts as a mediator between the program and the stream's source or destination.

In this same manner, file input and output data is handled by the use of a stream buffer. 

 A buffer is a block of memory used as an intermediate, temporary storage area for the transfer of information between a program and a device (e.g. file). 




Types of Files:

Sequential access files must be accessed in the same order in which they were written.  This process is analogous to the old audio cassette tapes where you must fast forward or rewind through the songs sequentially to get to a specific song.  In order to access data from a sequential file, you must start at the beginning of the file and search through the entire file for the data that you want. We will be concentrating on these files.

Random access files are analogous to audio compact disks where you can easily access any song, regardless of the order in which the songs were recorded.  Random access files allow instant access to any data in the file.  Unfortunately, random access files often occupy more disk space than sequential access files.

While Java files may be of the form text or binary, we will be examining only text files.
A text file is processed as a sequence of characters. A text file appears to be divided into a series of lines with an end-of-line symbol and an end-of-file symbol to designate the last component in the file.
A binary file is processed as a sequence of bytes and may be computer dependent.


There are several programming options that can be employed when working with files.
reddot When dealing with Input (reading an existing file), we will use the Scanner class.
reddot When dealing with Output (writing to or appending file), we will use the PrintWriter class.


Files will be stored in the same Java Eclipse src directory as the working program and a relative address such as src/filename.txt will be used to access the file.

Return to Unit Menu | | | Terms of Use  |

Notice: These materials are free for your on-line use at this site, but are not free for the taking.
Please do not copy these materials or re-post them to the Internet, as it is copyright infringement.
If you wish hard-copies of these materials, refer to our subscription area,
Help us keep these resources free. Thank you.