java.io
Class File

java.lang.Object
  |
  +--java.io.File

public class File
extends Object

This class represents a file name of the host file system. The file name can be relative or absolute. It must use the file name conventions of the host platform.

The intention is to provide an abstraction that deals with most of the system-dependent file name features such as the separator character, root, device name, etc. Not all features are currently fully implemented.

Note that whenever a file name or path is used it is assumed that the host's file name conventions are used.


Field Summary
static String pathSeparator
          The system dependent path separator string.
static char pathSeparatorChar
          The system dependent path separator character.
static String separator
          The system dependent file separator String.
static char separatorChar
          The system dependent file separator character.
 
Constructor Summary
File(File dir, String name)
          Creates a File object (given a directory File object).
File(String path)
          Creates a File object.
File(String path, String name)
          Creates a File object from the specified directory.
 
Method Summary
 boolean canRead()
          Returns a boolean indicating whether or not a readable file exists.
 boolean canWrite()
          Returns a boolean indicating whether or not a writable file exists.
 boolean delete()
          Deletes the specified file.
 boolean equals(Object obj)
          Compares this object against the specified object.
 boolean exists()
          Returns a boolean indicating whether or not a file exists.
 String getAbsolutePath()
          Gets the absolute path of the file.
 String getName()
          Gets the name of the file.
 String getParent()
          Gets the name of the parent directory.
 String getPath()
          Gets the path of the file.
 int hashCode()
          Computes a hashcode for the file.
 boolean isAbsolute()
          Returns a boolean indicating whether the file name is absolute.
 boolean isDirectory()
          Returns a boolean indicating whether or not a directory file exists.
 boolean isFile()
          Returns a boolean indicating whether or not a normal file exists.
 long lastModified()
          Returns the last modification time.
 long length()
          Returns the length of the file.
 String[] list()
          Lists the files in a directory.
 String[] list(FilenameFilter filter)
          Uses the specified filter to list files in a directory.
 boolean mkdir()
          Creates a directory and returns a boolean indicating the success of the creation.
 boolean mkdirs()
          Creates all directories in this path.
 boolean renameTo(File dest)
          Renames a file and returns a boolean indicating whether or not this method was successful.
 String toString()
          Returns a String object representing this file's path.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

separator

public static final String separator
The system dependent file separator String.


separatorChar

public static final char separatorChar
The system dependent file separator character.


pathSeparator

public static final String pathSeparator
The system dependent path separator string.


pathSeparatorChar

public static final char pathSeparatorChar
The system dependent path separator character.

Constructor Detail

File

public File(String path)
Creates a File object.

Parameters:
path - the file path
Throws:
NullPointerException - If the file path is equal to null.

File

public File(String path,
            String name)
Creates a File object from the specified directory.

Parameters:
path - the directory path
name - the file name

File

public File(File dir,
            String name)
Creates a File object (given a directory File object).

Parameters:
dir - the directory
name - the file name
Method Detail

getName

public String getName()
Gets the name of the file. This method does not include the directory.

Returns:
the file name.

getPath

public String getPath()
Gets the path of the file.

Returns:
the file path.

getAbsolutePath

public String getAbsolutePath()
Gets the absolute path of the file.

Returns:
the absolute file path.

getParent

public String getParent()
Gets the name of the parent directory.

Returns:
the parent directory, or null if one is not found.

exists

public boolean exists()
Returns a boolean indicating whether or not a file exists.


canWrite

public boolean canWrite()
Returns a boolean indicating whether or not a writable file exists.


canRead

public boolean canRead()
Returns a boolean indicating whether or not a readable file exists.


isFile

public boolean isFile()
Returns a boolean indicating whether or not a normal file exists.


isDirectory

public boolean isDirectory()
Returns a boolean indicating whether or not a directory file exists.


isAbsolute

public boolean isAbsolute()
Returns a boolean indicating whether the file name is absolute.


lastModified

public long lastModified()
Returns the last modification time. The return value should only be used to compare modification dates. It is meaningless as an absolute time.


length

public long length()
Returns the length of the file.


mkdir

public boolean mkdir()
Creates a directory and returns a boolean indicating the success of the creation.


renameTo

public boolean renameTo(File dest)
Renames a file and returns a boolean indicating whether or not this method was successful.

Parameters:
dest - the new file name

mkdirs

public boolean mkdirs()
Creates all directories in this path. This method returns true if all directories in this path are created.


list

public String[] list()
Lists the files in a directory. Works only on directories.

Returns:
an array of file names. This list will include all files in the directory except the equivalent of "." and ".." .

list

public String[] list(FilenameFilter filter)
Uses the specified filter to list files in a directory.

Parameters:
filter - the filter used to select file names
Returns:
the filter selected files in this directory.
See Also:
FilenameFilter

delete

public boolean delete()
Deletes the specified file. Returns true if the file could be deleted.


hashCode

public int hashCode()
Computes a hashcode for the file.

Overrides:
hashCode in class Object
See Also:
Hashtable

equals

public boolean equals(Object obj)
Compares this object against the specified object.

Overrides:
equals in class Object
Parameters:
obj - the object to compare with
Returns:
true if the objects are the same; false otherwise.
See Also:
Hashtable

toString

public String toString()
Returns a String object representing this file's path.

Overrides:
toString in class Object