- A File
object represents a file name, not an actual file. Such objects
are, moreover, immutable. Thus the code
File f1 = new File("foo");
File f2 = new File("bar");
f1.renameTo(f2);
will rename the file named "foo" (if it exists) to
"bar". It will not change the value of the File
object referred to by f1; in particular, the expression
f1.getPath() will still evaluate to "foo".
- The available
method of the InputStream class
and its subclasses does not necessarily return the maximum number
of bytes that can be read without blocking. In particular, code of
the form
int n = in.available();
byte buf = new byte[n];
in.read(buf);
is not guaranteed to read all of the remaining bytes from the given
input stream. Similarly, the ready method of
Reader and
its subclasses may return false even if the stream is
ready to be read.
- The read(byte[])
and
read(byte[], int, int) methods of InputStream and
its subclasses are not guaranteed to read all available bytes. A
loop may be required, for example, in order to read a large file
into an array:
for (int off = 0; off < size;) {
int r = in.read(buf, off, buf.length - off);
if (r == -1) break;
off += r;
}
Alternatively, a BufferedInputStream
may be used. Similar remarks apply to the read(char[])
and read(char[], int, int)
methods of Reader and its
subclasses.
- The PrintStream and
PrintWriter
classes suppress all I/O errors. To see whether an error has
occurred, invoke the checkError method.
- PrintStream and
PrintWriter
objects do not always flush their output. To arrange for automatic
flushing, use the two-argument constructors of these classes and
specify true for the second argument.