Java Reference
In-Depth Information
After all the numbers have been written to longBuf , the contents of buf are converted to a byte[] array
and written to the file. You can verify that the file has been written by inspecting it and checking the file
properties. It should be 400 bytes.
The example writes values of just one type to the file but you can easily deal with sets of values of dif-
ferent fundamental types. You do this with the third mechanism for writing files that you try out later in
this chapter. First, you can learn a bit more about using a Writer object to write to a file.
WRITING A FILE USING A WRITER
The static newBufferedWriter() method in the Files class returns a java.io.BufferedWriter object
that you can use to write to a file. A BufferedWriter can only write character data to a file. A
BufferedWriter has an internal buffer that ensures that strings and even single characters can be written
efficiently.
The first argument to the newBufferedWriter() method is a Path object identifying the file path and
the second is a java.nio.charset.Charset object that defines a charset. As I explained at the beginning
of this chapter, ultimately you are always writing bytes to a file, whatever the type of the original data, and
a charset determines how Unicode characters are mapped to the sequences of bytes that are written to the
file. There are a range of standard charsets available with Java that offer different mappings for Unicode
characters and to create a Charset object for one of these, you just pass the name of the standard charset
as a string to the static forName() method in the Charset class. You can also obtain the default charset for
the current Java virtual machine by calling the static defaultCharset() method. The charset you use when
you write a file determines what bytes are written for each Unicode character and of course, when you read
a file, you must use the charset that was used when the file was written. In examples that use charsets I use
the "UTF-16" charset, which is the character encoding for the Java language.
You can optionally specify additional arguments to the newBufferedWriter() method that determine
how the file is opened. These are the same options that you saw with the newOutputStream() method and
with the same default options set if you don't specify any.
Here's how you could create a BufferedWriter object for a file:
Path file = Paths.get("D:/Junk/proverbs.txt");
BufferedWriter fileOut = Files.newBufferedWriter(
file, Charset.forName("UTF-16"), CREATE, APPEND);
This creates the file if it doesn't already exist and data is appended to the end of the file if it does exist.
A BufferedWriter object has the methods shown in Table 10-3 .
TABLE 10-3 : BufferedWriter Methods
METHOD DESCRIPTION
write(
Writes length characters from the string s starting at position offset .
Writes length characters from the chars array starting at position offset .
write(
Write a single character, c .
write(
int c)
newLine() Writes a newline character as defined by the line.separator system property. You should use this meth-
od rather than including a '\n' character in the text when you need a line separator in the output.
 
 
Search WWH ::




Custom Search