Java Reference
In-Depth Information
the program. As such, you will also deal with ways of handling I/O between your program and data
sources. This chapter covers the most basic of data sources, namely that of a file.
General input and output
When we talk about input and output in computing, we describe all forms of communication
between a program on the computer and the “outside world,” which includes human end users,
other programs on the same machine, or other programs running on other computers.
Input includes all the data and signals received by the running program. For instance, input can be
sent to a program using input devices such as a keyboard or mouse, or can come from other com-
puters, such as when you use your web browser to load in a specific web page. Output on the other
hand includes all the signals and data sent from a program. Monitors and printers are prime exam-
ples of output devices, but again, output can involve pure data, such as when your web browser
sends a request to a web server to receive a web page. The latter immediately illustrates that the
same program (a web browser) can involve a series of input and output operations. The same holds
for hardware devices, such as a network card or a modem.
A particular form of I/O we will be taking a closer look at in this chapter is file I/O, meaning input
and output operations that read and write data to files stored on your computer. We can skip the
details until we are ready to start dealing with files in Java, but two aspects are worth mentioning
right now—file modes and the difference between text and binary files as they apply to program-
ming languages other than Java.
Let's start with file modes. In many programming languages, once you specify a particular file,
the language requires you to specify a particular mode (which, in many cases, is passed on to the
operating system). Two general modes are fairly obvious: opening a file for reading and opening
a file for writing. However, other modes can be specified as well. The following list provides an
enumeration of such file modes, together with their common abbreviations used in most program-
ming languages:
r : Open a file for reading only (pointer at beginning of file).
r+ : Open a file for reading and writing (pointer at beginning of file).
w : Open a file for writing only (pointer at beginning of file); if the file does not exist, try to
create it; existing files will be truncated (made empty).
w+ : Open a file for reading and writing (pointer at beginning of file); if the file does not exist,
try to create it; existing file will be truncated (made empty).
a : Open a file for writing only (pointer at end of file, i.e. append new data at the end of the
file).
a+ : Open a file for reading and writing (pointer at end of file, i.e. append new data at the end
of the file).
x : Create a file and open for writing only; fail if file already exists.
x+ : Create a file and open for reading and writing; fail if file already exists.
 
Search WWH ::




Custom Search