Methods to use for different I/O situations

 

  If you know what you want to do, this chart can tell you what methods to use to do it:

 

 


For this situation

 

Meaning what

you want to do:


Write it using

any of these methods:

 

Then,

it can be read back using any of these methods:

Is the resultant file

human
readable?

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
void write ( int  v )

 

OutputStream 
void write ( int b )
void write( byte[ ] b )

void write( byte[ ] b, int off, int len)

void writeByte( int v )

.
Reader 
int read( )

int read( char[ ] c )
int read( char[ ] c, int off, int len)

 

InputStream 

void readFully( byte[ ] b )
void readFully( byte[ ] b, int off, int len)
String readLine( ) - if \r and \n written

int read ( )
int read( byte[ ] b )

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
human readable,
if just data in the ASCII  range 00-7f was written.

Write from a String to create a regular 8-bit file

Writer
void write ( String s )

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 
void writeChars( String s )
void writeChar( int v )

InputStream 
char readChar( )

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 
void writeUTF( String s )

InputStream 
String readUTF( )

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 
void writeBoolean( boolean v )

InputStream 
boolean readBoolean( )

No

Write the four bytes "true" or six bytes "false" based on a boolean value

PrintWriter
void print( boolean v )

all - see above

Yes

Write doubles as eight-byte values

OutputStream 
void writeDouble( double d )

InputStream 

double readDouble( )

No

Write doubles as characters, creating one byte per digit or decimal point, dropping trailing zeroes

PrintWriter 
void print( double d )

all - see above

Yes

Write floats as four-byte values

OutputStream 
void writeFloat( float f )

InputStream 
double readFloat( )

No

Write floats as characters, creating one byte per digit or decimal point, dropping trailing zeroes

PrintWriter 
void print( float f )

all - see above

Yes

Write ints as four-byte values

OutputStream 
void writeInt( int i )

InputStream 
double readInt( )

No

Write ints as characters, creating one byte per digit or decimal point

PrintWriter 
void print( int i )

all - see above

Yes

Write longs as eight-byte values

OutputStream 
void writeLong( long l )

InputStream 
double readLong( )

No

Write longs as characters, creating one byte per digit or decimal point

PrintWriter 
void print( long l )

all - see above

Yes

Write shorts as two-byte values

OutputStream 
void writeShort( short sh )

InputStream 
double readShort( )

No

Write shorts as characters, creating one byte per digit or decimal point

PrintWriter 
void print( short sh )

all - see above

Yes

Write the character representation of an object's toString( ) result

PrintWriter 
void print( Object o )

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( boolean v )

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