■ If you know what you want to do, this chart can tell you what methods to use to do it:
Meaning what you want to do: |
any of these methods: |
Then, it can be read back using any of these methods: |
Is the resultant file human |
Write from a char or from
an array of chars. Create a regular 8-bit text file
in the machine's default encoding |
Writer void write ( char[ ] c ) void write ( char[ ] c, int off, int len) PrintWriter void print( char c ) void print( char[ ] c ) |
. . . . . . . . . |
Yes |
Write from a byte, from a
byte array, or from the low end of an int to create a regular 8-bit
file |
Writer OutputStream void write( byte[ ] b, int off,
int len) void writeByte( int v ) |
. Reader int read( )
int read( char[ ] c ) InputStream void readFully( byte[ ] b ) int read ( ) int read( byte[ ] b, int off,
int len byte readByte( )
void readFully( byte[ ] b ) void readFully( byte[ ] b, int off,
int len) int readUnsignedByte( ) |
It may be
|
Write from a String to
create a regular 8-bit file |
Writer void write ( String s, int off,
int len) PrintWriter
void print( String s ) |
(In the rest of the chart this large group
of methods is referred to as "all") . . . . |
Yes |
Write from a String or
from the low two bytes of an int to create a file of 16-bit chars |
OutputStream |
InputStream |
Yes, but
there will appear to be a one-byte "space" between each of the
file's characters |
Write from a String to create
a special Java-modified UTF file of one, two, or three bytes per character,
with a file length header |
OutputStream |
InputStream |
Can be
human readable, if the ASCII range 00-7f is written and the file length
header is ignored when viewing |
Write booleans as
one-byte values, hex 01 for true and 00 for false. |
OutputStream |
InputStream |
No |
Write the four bytes
"true" or six bytes "false" based on a boolean
value |
PrintWriter |
all - see above |
Yes |
Write doubles as
eight-byte values |
OutputStream |
InputStream double readDouble( ) |
No |
Write doubles as characters,
creating one byte per digit or decimal point, dropping
trailing zeroes |
PrintWriter |
all - see above |
Yes |
Write floats as four-byte
values |
OutputStream |
InputStream |
No |
Write floats as
characters, creating one byte per digit or decimal point, dropping trailing zeroes |
PrintWriter |
all - see above |
Yes |
Write ints as four-byte
values |
OutputStream |
InputStream |
No |
Write ints as characters,
creating one byte per digit or decimal point |
PrintWriter |
all - see above |
Yes |
Write longs as eight-byte
values |
OutputStream |
InputStream |
No |
Write longs as
characters, creating one byte per digit or decimal point |
PrintWriter |
all - see above |
Yes |
Write shorts as two-byte
values |
OutputStream |
InputStream |
No |
Write shorts as
characters, creating one byte per digit or decimal point |
PrintWriter |
all - see above |
Yes |
Write the character representation
of an object's toString( ) result |
PrintWriter |
InputStream all - see above |
Yes, but
the result may not appear meaningful |
Write text characters, as above,
but write them as a line, always adding an automatic line ending of \r (hex 0D) and \n (hex 0A) |
PrintWriter void println( char c ) void println( char[ ] c ) void println( double d ) void println( float f ) void println( int i ) void println( long l ) void println( Object o ) void println( String s ) |
BufferedReader method: String readLine( ) |
Yes |