See the following
subclasses of Writer
which use Writer's methods: BufferedWriter
class, CharArrayWriter
class,
FileWriter
class, FilterWriterclass, OutputStreamWriter
class, PipedWriter
class, PrintWriter
class,
■ The seven basic Writer methods (examples below) 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
■ Writer is the abstract class which defines basic methods for all of the character stream output classes listed above. All these classes have the seven methods listed below. Although many classes use these methods, on this site they're described here, in one central place.
■ Since this site's focus is on usage, not on extension, we omit subtleties about when these basic methods are abstract, overridden or inherited by Writer's various subclasses. The bottom line is that you can use these methods, the way they are described here, with all the Writer subclasses listed above.
■
Although Writer is a character stream class, you can use these
methods to write fine everyday byte files.
That's because they default to Java's UTF-8 Unicode character set
format, which encompasses ASCII.
■ close( ) performs a flush( ) and then closes the stream. You should make it a habit to use close( ) consistently, to ensure that all your data is actually written out.
■ flush( ) flushes the unwritten data out of the stream without closing it. i.e.
import
java.io.*;
try
{
FileWriter
fw = new FileWriter( "3newfile" );
String
s1 = "HELLO ";
String
s2 = "WORLD";
fw.write(
s1 );
fw.flush(
); // flush it
fw.write(
s2 ); // stream is
stll open for writing after the flush
fw.close(
);
} catch (IOException e) { }
■ Writes out the entire contents of a char array as UTF-8 characters.
import
java.io.*;
try
{
FileWriter
fw = new FileWriter( "anewfile" );
char
c[ ] = {'H','E','L','L','O', '
','W','O','R','L','D'};
fw.write(
c );
fw.close(
);
} catch (IOException e) { }
void .write(
char[ ], offset, len
) method
■ Writes a portion of a char array for a length of len, starting at offset off. The example below writes just the second word "WORLD" by picking off the 5 characters starting at offset 6.
import
java.io.*;
try
{
FileWriter
fw = new FileWriter( "anewfile" );
char
c[ ] = {'H','E','L','L','O', '
','W','O','R','L','D'};
fw.write(
c, 6 , 5 );
fw.close(
);
} catch (IOException e) { }
■ Writes a single character from the 16 low-order bits of the int. The int's 16 high-order bits are ignored. Note that, because of the UTF-8 default, bytes are written and not 16-bit Unicode characters. (Although this might appear to mean that another byte is also ignored, the high byte of the 16 low-order bits, it does not. Anything but zeroes in that upper byte will cause question mark characters to be written.) The example below loads a series of 2-byte chars into the low end of an int for pickup by the method.
import
java.io.*;
try
{
FileWriter
fw = new FileWriter( "anewfile" );
char
c[ ] = {'H','E','L','L','O', '
','W','O','R','L','D'};
for
(int x = 0; x <= 10; x++) {
int y = c[ x ]; // get a two-byte char into the int
fw.write( y );
}
fw.close(
);
} catch (IOException e) { }
■ Writes an entire String as 8-bit UTF characters.
import
java.io.*;
try
{
FileWriter
fw = new FileWriter( "anewfile" );
String
s = "HELLO WORLD";
fw.write(
s );
fw.close(
);
}
catch (IOException e) { }
void .write(
String, offset, len
) method
■ Writes a portion of a String for a length of len, starting at offset off. The example below writes just the first word "HELLO" by picking off 5 characters starting at offset 0.
import
java.io.*;
try
{
FileWriter
fw = new FileWriter( "anewfile" );
String
s = "HELLO WORLD";
fw.write(
s, 0 , 5 );
fw.close(
);
} catch (IOException e) { }