Java Reference
In-Depth Information
java.io.File
+File(pathname: String)
Creates a
File
object for the specified path name. The path name may be a
directory or a file.
Creates a
File
object for the child under the directory parent. The child may be
a file name or a subdirectory.
Creates a
File
object for the child under the directory parent. The parent is a
File
object. In the preceding constructor, the parent is a string.
Returns true if the file or the directory represented by the
File
object exists.
Returns true if the file represented by the
File
object exists and can be read.
Returns true if the file represented by the
File
object exists and can be written.
Returns true if the
File
object represents a directory.
Returns true if the
File
object represents a file.
Returns true if the
File
object is created using an absolute path name.
Returns true if the file represented in the
File
object is hidden. The exact
definition of
hidden
is system-dependent. On Windows, you can mark a file
hidden in the File Properties dialog box. On Unix systems, a file is hidden if
its name begins with a period(.) character.
Returns the complete absolute file or directory name represented by the
File
object.
Returns the same as
getAbsolutePath()
except that it removes redundant
names, such as "." and "..", from the path name, resolves symbolic links (on
Unix), and converts drive letters to standard uppercase (on Windows).
Returns the last name of the complete directory and file name represented by
the
File
object. For example, new
File("c:\\book\\test.dat").getName()
returns
test.dat
.
Returns the complete directory and file name represented by the
File
object.
For example, new
File("c:\\book\\test.dat").getPath()
returns
c:\book\test.dat
.
Returns the complete parent directory of the current directory or the file
represented by the
File
object. For example, new
File("c:\\book\\test.dat").getParent()
returns
c:\book
.
Returns the time that the file was last modified.
Returns the size of the file, or 0 if it does not exist or if it is a directory.
Returns the files under the directory for a directory
File
object.
Deletes the file or directory represented by this
File
object.The method returns
true if the deletion succeeds.
Renames the file or directory represented by this
File
object to the specified name
represented in dest. The method returns true if the operation succeeds.
Creates a directory represented in this
File
object. Returns true if the the directory is
created successfully.
Same as
mkdir
() except that it creates directory along with its parent directories if
the parent directories do not exist.
+File(parent: String, child: String)
+File(parent: File, child: String)
+exists(): boolean
+canRead(): boolean
+canWrite(): boolean
+isDirectory(): boolean
+isFile(): boolean
+isAbsolute(): boolean
+isHidden(): boolean
+getAbsolutePath(): String
+getCanonicalPath(): String
+getName(): String
+getPath(): String
+getParent(): String
+lastModified(): long
+length(): long
+listFile(): File[]
+delete(): boolean
+renameTo(dest: File): boolean
+mkdir(): boolean
+mkdirs(): boolean
F
IGURE
14.6
The
File
class can be used to obtain file and directory properties, to delete and rename files and directo-
ries, and to create directories.
Caution
The directory separator for Windows is a backslash (
\
). The backslash is a special char-
acter in Java and should be written as
\\
in a string literal (see Table 2.6).
\
in file names
Note
Constructing a
File
instance does not create a file on the machine
. You can create a
File
instance for any file name regardless whether it exists or not. You can invoke the
exists()
method on a
File
instance to check whether the file exists.
Do not use absolute file names in your program. If you use a file name such as
c:\\book\\Welcome.java
, it will work on Windows but not on other platforms. You
should use a file name relative to the current directory. For example, you may create a
File
object using
new File("Welcome.java")
for the file
Welcome.java
in the current direc-
tory. You may create a
File
object using
new File("image/us.gif")
for the file
us.gif
under the
image
directory in the current directory. The forward slash (
/
) is the Java directory
relative file name
Java directory separator (
/
)