Yes, since FileWriter
inherits
from OutputStreamWriter
.
import java.io.*; class WriteTextFile { public static void main ( String[] args ) throws IOException { String fileName = "reaper.txt" ; FileWriter writer = new FileWriter( fileName ); writer.write( "Behold her, single in the field,\n" ); writer.write( "Yon solitary Highland Lass!\n" ); writer.write( "Reaping and singing by herself;\n" ); writer.write( "Stop here, or gently pass!\n" ); writer.close(); } }
This example program
constructs a FileWriter
stream
and creates a disk file in the current directory
named reaper.txt.
The write()
method writes characters to the file.
The close()
method closes the file.
The next pages explain the details. For now, save the program to a file and run it.
Warning: each time you run the program it will delete any file named reaper.txt in the directory and create a new one. You might wish to check that the 20 page report that you wrote last night is not named reaper.txt.
Is an IOException
a checked exception?