Section
11 : The java.io Package
Write
appropriate code to read, write and update files using
FileInputStream, FileOutputStream, and
RandomAccessFile objects.
FileInputStream
Extends
InputStream.
Constructors:
Creates
a FileInputStream object by opening a connection to an
actual file, the file named by the File object in the
file system. A new FileDescriptor object is created
to represent this file connection. Throws
FileNotFoundException if the file does not exist, is a
directory or for some other reason cannot be opened
for reading. Throws SecurityException if access is
denied by SecurityManager.
Creates
a FileInputStream object by using fdObj, which
represents an existing connection to an actual file.
Throws NullPointerException if fdObj is null. Throws
SecurityException if read access is denied.
Creates
a FileInputStream object by opening a connection to an
actual file, the file named by the path-name String
name. A new FileDescriptor object is created to
represent this file connection. Throws
FileNotFoundException if the file does not exist, is a
directory or for some other reason cannot be opened
for reading. Throws SecurityException if access is
denied by SecurityManager.
Methods:
Ensures
that close() is called at the time of garbage
collection.
-
FileDescriptor getFD ()
-
int read ()
-
int read (byte
[] b)
This
method blocks until some input is available.
This
method may, for a variety of reasons, end up skipping
a smaller number of bytes, possibly zero. Returns
actual number of bytes skipped.
This
class does not override mark() and reset().
FileOutputStream
Extends
OutputStream. It writes data to a file or a
FileDescriptor. Whether or not a file is available or
may be created depends upon the underlying platform.
Constructors:
-
FileOutputStream (File f)
Creates
a file output stream to write to the file represented
by the specified
File object.
-
FileOutputStream (FileDescriptor
fdObj)
Creates
an output file stream to write to the specified file
descriptor, which represents an existing connection to
an actual file in the file system.
-
FileOutputStream (String name)
Creates
an output file stream to write to the file with the
specified name. A new FileDescriptor object is created
to represent this file connection.
-
FileOutputStream (String name,
boolean append)
Creates
an output file stream to write to the file with the
specified name. If the second argument is true, then
bytes will be written to the end of the file rather
than the beginning. A new FileDescriptor object is
created to represent this file connection.
Constructors 1,3 and 4 throw FileNotFoundException if
the file exists but is a directory rather than a
regular file, does not exist but cannot be created, or
cannot be opened for any other reason.
Methods:
Overrides those in OutputStream.
RandomAccessFile
Extends
object, implements DataInput and DataOutput.
A random
access file behaves like a large array of bytes stored
in the file system. For all the reading routines in
the class if end-of-file is reached before the desired
number of bytes has been read, an EOFException is
thrown. If any byte cannot be read for any reason
other than end of file, an IOException other than
EOFException is thrown.
No
method of this class throws IllegalArgumentException.
Constructors:
-
RandomAccessFile (File f, String
mode)
-
RandomAccessFile (String name,
String mode)
The mode
argument must either be equal to “r” or “rw”,
indicating that the file is to be opened for input
only or for both input and output, respectively. The
write methods on this object will always throw an
IOException if the file is opened with a mode of “r”.
If the mode is “rw” and the file does not exist,
then an attempt is made to create it. An IOException
is thrown if the file argument refers to a directory.
These constructors will throw IllegalArgumentException
if mode is not “r” or “rw”. If file exists but is a
directory, or cannot be opened or created for any
other reason.
Methods:
-
void close ()
-
long length ()
-
int read ()
Reads a
byte which is returned as an integer in the range of 0
to 255. method blocks if no onput is available.
Returns
the current offset in this file.
type is
java primitive type (all 8 types). The method
readBoolean () reads a single byte. A value of
0
represents false. All other values represent true.
-
void readFully (byte [] b)
-
void readFully (byte [] b, int offset, int
length)
Both of
the above methods block until the requested number of
bytes are read, the end of stream is detected or
exception thrown.
Does not
support the full Unicode character set. This method
successively reads the bytes from the file, starting
at the current file pointer, until it reaches a line
terminator or end of file. Each byte is converted to a
character by taking the byte’s value for the lower
eight bits of the character and setting the high eight
bits to zero. This method blocks for input.
The
offset may be set beyond the end of file. This does
not change the length of the file. The file length
changes only by writing after the offset has been set
beyond the end of the file. If ‘position’ is less than
zero, IOException is thrown.
It may
skip over smaller number of bytes (even zero). This
method never throws an EOFException. If n is negative,
no bytes are skipped.
-
void write (int b)
-
void write (byte
[] b)
-
void write (byte
[] b, int offset, int length)
-
void writeByte (int b)
-
void writeBytes (String s)
-
void writeChar ( int v)
-
void writeChars (String s)
-
void writeDouble (double d)
-
void writeFloat (float f)
-
void writeInt (int v)
-
void writeLong (long l)
-
void writeShort (int s)
-
void writeUTF (String s)
section11-1 | section11-2 | section11-3 | section11-4 | section11-5 | section11-6
Sections :
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11
|