Writing Text Files
10. The write() Method
Answer:
An IOException
is thrown by FileWriter
and caught in the catch
block.
The write()
Method
write()
MethodWith FileWriter
use the write()
method to write characters to the file. There are several write()
methods with different parameters inherited from Writer
and OutputStreamWriter
.
The example program uses a method that writes characters from a string:
public void write(String str) throws IOException
To output numerical results as characters use string concatenation:
int answer;
. . .
stream.write( "The answer is: " + answer)
This constructs a new string by converting the data in answer
into characters and concatenating them to characters from the literal. It then writes the resulting string to the disk file.
Question 10:
Does the disk file contain
int
data?