Writer is an abstract class that defines streaming character output. AIl of the methods in this class return a void value and throw an IOException in the case of-errors. Table 17-4 shows a synopsis of the methods in Writer.


FileReader class creates a Reader that you can use to read the contents of a file. Its two most commonly used constructors are shown here:

FileReader(String file Pntl)
FileReader(File fileObj)

The following example shows how to read lines from a file.and print these to the standard output stream. It reads Its own source file, which must be in the-current directory.


FileWriter creates a Writer that you can use to write to a file. Its most commonly used constructors are shown here:

Creation of a FileWriter is not dependent on the file already existing, FileWriter will create the file before opening it for output when you create the object. In the case , where you attempt to open a read-only file, an IOException will be thrown.

The following' example is a character stream version of an example shown earlier when FileOutputStream was discussed. This version creates a simple bufFer of characters by first making a String and then using the getChars( ) method to extract the character array equivalent, It then creates three files. The first, file'l.txt, will contain every other character from the sample. The second; fUel-bet, will.contain the, entire ,set of characters. Finally, the third, file3.txt, will contact only:the last quarter.

Share This