Section
11 : The java.io Package
Write code that uses objects of
the file class to navigate a file system.
File
This
class extends Object, implements Comparable,
Serializable. The current user directory is named by
the system property user.dir, and is usually the
directory in which the JVM was invoked. File is an
abstract representation of file and directory path
names. Instances of this class are immutable.
Fields
The
system dependent path separator character, represented
as a string for convenience. This string contains a
single character, namely pathSeparatorChar.
System
dependent.
The
system dependent default name separator character,
represented as a string for convenience. This string
contains a single character, namely separatorChar.
Constructors:
If
parent is null then the new file instance is created
as if by invoking File(String pathname). Otherwise,
the parent abstract pathname is taken to denote a
directory, and child is taken either as a directory or
a file.
Throws
NullPointerException if child is null.
Throws
NullPointerException if pathname is null.
Same as
first constructor.
Methods:
if the
other object is an abstract pathname, then this method
behaves as the previous method, otherwise
ClassCastException is thrown.
Atomically creates a new, empty file named by this
abstract pathname if and only if a file with this name
does not exist yet. Returns false if the named file
already exists.
Throws
IOException.
Throws
IOException.
Throws
IOException.
deletes
this file or directory. If it is a directory, then the
directory must be empty in order to be deleted.
Requests
that file be deleted when JVM terminates. Once the
request has been made, it is not possible to cancel
the request.
Returns
null if this pathname does not name a parent
directory.
-
File getParentFile ()
-
String getPath ()
Converts
this abstract pathname to
a pathname string.
-
boolean isAbsolute ()
-
boolean isHidden ()
-
long lastModified ()
Returns
zero if the file does not exist or if an I/O error
occurs.
-
long length ()
-
String [] list ()
If this
file object does not denote a directory then this and
the following method return null. Names denoting the
directory itself and the directory’s parent directory
are not included in the result. Each string is a file
name rather than a complete path.
If this
file object does not denote a directory then this
method returns null. If this pathname is absolute then
each resulting pathname is absolute; if this pathname
is relative then each resulting pathname will be
relative to the same directory.
Creates
the directory named by this abstract pathname,
including any necessary but non existent parent
directories. If this operation fails, it may have
succeeded in creating some of the necessary parent
directories. Returns true only if the directory, along
with all necessary parent directories was created;
false otherwise.
Throws
SecurityException. Throws NullPointerException if
destination is null.
throws
IllegalArgumentException if n is negative.
-
boolean setReadOnly ()
-
URL toURL ()
Converts
this object into a file:URL. Throws MalformedException
if the path cannot be passed as a URL.
section11-1 | section11-2 | section11-3 | section11-4 | section11-5 | section11-6
Sections :
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11
|