FileWriter class

 

††††††††††††††††† see Writer classfor descriptions of the methods which FileWriter uses.They are:

 

.close( ) †††††††††††††††††††††††††††††††††† Closes the stream, flushing it first.

.flush( ) †††††††††††††††††††††††††††††††††† Flushes all associated buffers in a stream without closing it.

.write( int c )†††††††††††††††††††††††††† Writes a single char, supplied as the low-order two bytes of a four-byte int.

.write( char[ ] c )†††††††††††††††††††† Writes an array of characters.

.write( char[ ] c, int off, int len )Writes a portion len of a character array, beginning at offset

.write( String s )††††††††††††††††††††† Writes a String.

.write( String s, int off, int len )Writes a portion len of a String, beginning at off

 

 

FileWriter is a class for writing streams of default Unicode UTF-8 characters to files.It is basically like an OutputStreamWriter which does not happen to convert its output to bytes, as OutputStreamWriter does.Instead it keeps them in the default Java character format. The data source for its methods is usually characters generated from the actual program code.Or it can be chained from another output character stream which supplies the characters.

 

Here FileWriter's methods would be supplied with values to write to a file.

 

FileWriter fw = new FileWriter( "somenewfile" );

// put something into the int called x

fw.write(x);††††††††††††

fw.close( );††††††††††††

 

Here a PrintWriter has been chained in to supply the characters to the FileWriter stream.The PrintWriter's methods would now be used.

 

PrintWriter pw = new PrintWriter( new FileWriter( "somenewfile" ) );

pw.println( "something" );

pw.close( );

 

 

Here a BufferedWriter has been inserted into the chain, to provide buffering. PrintWriter's methods would still be used.

 

PrintWriter pw = new PrintWriter( new BufferedWriter ( new FileWriter( "somenewfile" ) ) );

pw.println( "something" );

pw.close( );

 

FileWriter extends OutputStreamWriter, from Writer, so all OutputStreamWriterís getEncoding( ) method plus all the Writer methods are available to you.FileWriter adds no additional methods.†† seeWriter class .

 

FileWriter's constructors accept either a String designation of the sink filename or a File object representing the destination sink.The three FileWriter constructor variations shown below all work:

 

File f = new File( "mycharfile" );

FileWriter fw = new FileWriter( f );

or

FileWriter fw = new FileWriter( new File( "mycharfile" ));

or

FileWriter fw = new FileWriter( "mycharfile" );

 

You cannot correctly write non-default Unicode files with FileWriter.That's because you cannot change Java's UTF-8 Unicode default file character encoding using FileWriter constructors.These constructors do not have a provision for specifying the CharSet of the output file. To specify another CharSet yourself, so that you can correctly write a non-default Unicode file, chain an OutputStreamWriter which specifies the necessary CharSet to a FileOutputStream.†† SeeOutputStreamWriter classfor an example of this.

 

Copying a char file.The FileWriter snippet below reads an existing file a character at a time and copies it to a new one, which it creates.If newcharfile already exists, it will be overwritten.

 

import java.io.*;

 

try {

††† File fi = new File( "oldcharfile" );

††† FileReader fr = new FileReader( fi );

††† File fo = new File( "newcharfile" );

††† FileWriter fw = new FileWriter( fo );

††† int c;

††† while (( c = fr.read( )) != -1 ) {

††††††† fw.write( c );

††† }

††† fr.close( );

††† fw.close( );

} catch (IOException e) { }

††

 

Writing a String to a char file.This FileWriter snippet reads a String from a StringReader and copies the String to a file, one character at a time. If newcharfile already exists, it will be overwritten.

 

import java.io.*;
 
try {
††† Reader sr = new StringReader("abcdefghijklmnopqrstuvwxyz");
††† Writer fw = new FileWriter( "newcharfile" );
††† int c;
††† while ( (c = sr.read( ) ) != -1) {
††††††† fw.write( c );
††† }
††† fw.close( );
} 
catch (IOException e) { }
 
  You can append to the ends of files with FileWriter, instead of overwriting them, by using the following constructors with a second boolean argument of true:
 
File f = new File( "somecharfile");
Writer fw = new FileWriter(f, true );
or
Writer fw = new FileWriter("somecharfile", true );