Java Reference
In-Depth Information
FileDescriptor Objects
A
FileOutputStream
object has a method,
getFD()
, which returns an object of type
FileDescriptor
that represents the current connection to the physical file. You cannot create a
FileDescriptor
object
yourself. You can only obtain a
FileDescriptor
object by calling the
getFD()
method for an object that
represents a file stream. Once you have closed the stream, you can no longer obtain the
FileDescriptor
object for it since the connection to the file will have been terminated.
You can use a
FileDescriptor
object to create other stream objects when you want to have several
connected to the same file concurrently. Since a
FileDescriptor
object represents an existing
connection, you can only use it to create streams with read and/or write permissions that are consistent
with the original stream. You can't use the
FileDescriptor
object from a
FileOutputStream
to
create a
FileInputStream
for instance.
If you look at the documentation for the
FileDescriptor
class, you'll see that it also defines three
public static data members:
in
,
out
, and
err
, which are themselves of type
FileDescriptor
. These
correspond to the standard system input, the standard system output, and the standard error stream
respectively, and they are there as a convenience for when you want to create byte or character stream
objects corresponding to the standard streams.
Don't confuse the data members of the
FileDescriptor
class with the data
members of the same name defined by the
System
class in the
java.lang
package.
The
in
,
out
, and
err
data members of the
System
class are of type
PrintStream
,
so they have the
print()
and
println()
methods. The
FileDescriptor
data
members do not.
Summary
In this chapter, we have discussed the facilities for inspecting physical files and directories, and for
writing basic types of data to a file. The important points we have discussed include:
An object of the class
File
can encapsulate a file or directory path. The path encapsulated by
a
File
object does not necessarily correspond to a physical file or directory.
❑
You can use a
File
object to test whether the path it encapsulates refers to a physical file or
directory. If it does not, there are methods available to create it together with any directories
that are part of the path that may also be required.
❑
The
File
class defines static methods for creating temporary files.
❑
An object of type
FileDescriptor
can also identify a physical file.
❑
A
FileOutputStream
object can be created from a
File
object and the file will be opened
for writing. If the file does not exist it will be created where possible.
❑
You can lead a horse to water but you can't make him drink.
❑