Java Reference
In-Depth Information
Here, critical data is being written to a file in a loop. Calling the
force()
method after each write
operation minimizes the risk of data loss in the event of a system crash. The
force()
method throws a
ClosedChannelException
if the channel is closed, or an
IOException
if some other I/O error occurs. Note
that the
force()
method guarantees only that all data is written for a local storage device. If the ultimate
destination for the data you are writing using the channel is a storage device elsewhere on a network, you
have no direct way to guarantee that the data gets written to the device.
Only one write operation can be in progress for a given file channel at any time. If you call
write()
while a
write()
operation that was initiated by another thread is in progress, your call to the
write()
meth-
od blocks until the write that's in progress has been completed.
SUMMARY
In this chapter, you explored the most important facilities for writing basic types of data to a file. The static
methods in the
Files
class create the objects you need for writing files. You now have acquired knowledge
of what you need to be able to write files. Reading files is the subject of the next chapter. Writing and read-
ing objects is a little different from regular file input and output, which you learn about in Chapter 12.
EXERCISES
You can download the source code for the examples in the topic and the solutions to the following exer-
1.
Implement a new version of the example that writes proverbs to a file that writes the proverbs to one
file using a
Writer
and the length of each proverb to a separate file using an
OutputStream
object.
2.
Write a program that creates an integer array of date values containing month, day, and year as integers
for some number of dates (10, say, so the integer array is two-dimensional with 10 rows and 3 columns).
The program should write a file with a string representation of each date written as Unicode characters.
For example, the date values 3,2,1990 would be written to the file as 2nd March 1990. Make sure that the
date strings can be read back, either by using a separator character of some kind to mark the end of each
string or by writing the length of each string before you write the string itself.
3.
Extend the previous example to write a second file at the same time as the first, but containing the
month, day, and year values as binary data. You should have both files open and be writing to both at the
same time.
4.
Write a program that, for a given
String
object defined in the code, writes strings to a file in the local
character encoding (as bytes) corresponding to all possible permutations of the words in the string. For
example, for the string
the fat cat
, you would write the strings
the fat cat
,
the cat fat
,
cat the
fat
,
cat fat the
,
fat the cat
, and
fat cat the
to the file, although not necessarily in that sequence.
(Don't use very long strings; with
n
words in the string, the number of permutations is
n
!, so a string with
10 words has 3,628,800 permutations!).
• WHAT YOU LEARNED IN THIS CHAPTER