jcontrol.io
Class File

java.lang.Object
  extended by jcontrol.io.File

public class File
extends Object

An abstract representation of file and directory pathnames.

User interfaces and operating systems use system-dependent pathname strings to name files and directories. This class presents an abstract, system-independent view of hierarchical pathnames. An abstract pathname has two components:

  1. An optional system-dependent prefix string, such as a disk-drive specifier, "usba0" for the USB root directory, or "roma0" for the internal ROMFS, and
  2. A sequence of zero or more string names.
Each name in an abstract pathname except for the last denotes a directory; the last name may denote either a directory or a file. The empty abstract pathname has no prefix and an empty name sequence.

When an abstract pathname is converted into a pathname string, each name is separated from the next by a single copy of the default separator character "/". The default name-separator is made available in the public static fields SEPARATOR and SEPARATOR_CHAR of this class. When a pathname string is converted into an abstract pathname, the names within it may be separated by the default name-separator character.

A pathname, whether abstract or in string form, may be always absolute. An absolute pathname is complete in that no other information is required in order to locate the file that it denotes.

The prefix concept is used to handle different devices, like USB stick or ROM-Filesystem, as follows:

Instances of the File class are immutable; that is, once created, the abstract pathname represented by a File object will never change.

Since:
JCVM32 (Gimlin3,Gimlin4)

Field Summary
static String SEPARATOR
          The system-dependent default name-separator character, represented as a string for convenience.
static char SEPARATOR_CHAR
          The system-dependent default name-separator character.
static String SEPARATOR_URI
           
 
Constructor Summary
File(File parent, String child)
          Creates a new File instance from a parent abstract pathname and a child pathname string.
File(String pathname)
          Creates a new File instance by converting the given pathname string into an abstract pathname.
 
Method Summary
 boolean canRead()
          NOTE: Not Supported Tests whether the application can read the file denoted by this abstract pathname.
 boolean canWrite()
          NOTE: Not Supported Tests whether the application can modify to the file denoted by this abstract pathname.
 boolean createNewFile()
          Atomically creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist.
 boolean delete()
          Deletes the file denoted by this abstract pathname.
 boolean exists()
          Tests whether the file or directory denoted by this abstract pathname exists.
protected  void finalize()
          Free internal data structures used by this file object.
 String getCanonicalPath()
          Returns the canonical pathname string of this abstract pathname.
 String getName()
          Returns the name of the file or directory denoted by this abstract pathname.
 String getPath()
          Converts this abstract pathname into a pathname string.
 boolean isDirectory()
          Tests whether the file denoted by this abstract pathname is a directory.
 boolean isFile()
          Tests whether the file denoted by this abstract pathname is a normal file.
 int length()
          Returns the length of the file denoted by this abstract pathname.
 String[] list()
          Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname.
 File[] listFiles()
          Returns an array of abstract pathnames denoting the files in the directory denoted by this abstract pathname.
static File[] listRoots()
          List the available filesystem roots.
 String toString()
          Returns the pathname string of this abstract pathname.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, notifyAll, wait
 

Field Detail

SEPARATOR_URI

public static final String SEPARATOR_URI
See Also:
Constant Field Values

SEPARATOR_CHAR

public static final char SEPARATOR_CHAR
The system-dependent default name-separator character.

See Also:
java.lang.System#getProperty(java.lang.String), Constant Field Values

SEPARATOR

public static final String SEPARATOR
The system-dependent default name-separator character, represented as a string for convenience. This string contains a single character, namely SEPARATOR_CHAR.

Constructor Detail

File

public File(String pathname)
Creates a new File instance by converting the given pathname string into an abstract pathname. If the given string is the empty string, then the result is the empty abstract pathname.

Parameters:
pathname - A pathname string
Throws:
NullPointerException - If the pathname argument is null

File

public File(File parent,
            String child)
Creates a new File instance from a parent abstract pathname and a child pathname string.

If parent is null then the new File instance is created as if by invoking the single-argument File constructor on the given child pathname string.

Otherwise the parent abstract pathname is taken to denote a directory, and the child pathname string is taken to denote either a directory or a file. If the child pathname string is absolute then it is converted into a relative pathname in a system-dependent way. If parent is the empty abstract pathname then the new File instance is created by converting child into an abstract pathname and resolving the result against a system-dependent default directory. Otherwise each pathname string is converted into an abstract pathname and the child abstract pathname is resolved against the parent.

Parameters:
parent - The parent abstract pathname
child - The child pathname string
Throws:
NullPointerException - If child is null
Method Detail

getName

public String getName()
Returns the name of the file or directory denoted by this abstract pathname. This is just the last name in the pathname's name sequence. If the pathname's name sequence is empty, then the empty string is returned.

Returns:
The name of the file or directory denoted by this abstract pathname, or the empty string if this pathname's name sequence is empty

getPath

public String getPath()
Converts this abstract pathname into a pathname string. The resulting string uses the default name-separator character to separate the names in the name sequence.

Returns:
The string form of this abstract pathname

getCanonicalPath

public String getCanonicalPath()
Returns the canonical pathname string of this abstract pathname.

A canonical pathname is both absolute and unique. The precise definition of canonical form is system-dependent. This method first converts this pathname to absolute form if necessary, as if by invoking the #getAbsolutePath method, and then maps it to its unique form in a system-dependent way. This typically involves removing redundant names such as "." and ".." from the pathname, resolving symbolic links (on UNIX platforms), and converting drive letters to a standard case (on Microsoft Windows platforms).

Every pathname that denotes an existing file or directory has a unique canonical form. Every pathname that denotes a nonexistent file or directory also has a unique canonical form. The canonical form of the pathname of a nonexistent file or directory may be different from the canonical form of the same pathname after the file or directory is created. Similarly, the canonical form of the pathname of an existing file or directory may be different from the canonical form of the same pathname after the file or directory is deleted.

Returns:
The canonical pathname string denoting the same file or directory as this abstract pathname
Throws:
IOException - If an I/O error occurs, which is possible because the construction of the canonical pathname may require filesystem queries
Since:
JDK1.1

canRead

public boolean canRead()

NOTE: Not Supported

Tests whether the application can read the file denoted by this abstract pathname.

Returns:
true if and only if the file specified by this abstract pathname exists and can be read by the application; false otherwise

canWrite

public boolean canWrite()

NOTE: Not Supported

Tests whether the application can modify to the file denoted by this abstract pathname.

Returns:
true if and only if the file system actually contains a file denoted by this abstract pathname and the application is allowed to write to the file; false otherwise.

exists

public boolean exists()
Tests whether the file or directory denoted by this abstract pathname exists.

Returns:
true if and only if the file or directory denoted by this abstract pathname exists; false otherwise

isDirectory

public boolean isDirectory()
Tests whether the file denoted by this abstract pathname is a directory.

Returns:
true if and only if the file denoted by this abstract pathname exists and is a directory; false otherwise

isFile

public boolean isFile()
Tests whether the file denoted by this abstract pathname is a normal file. A file is normal if it is not a directory and, in addition, satisfies other system-dependent criteria. Any non-directory file created by a Java application is guaranteed to be a normal file.

Returns:
true if and only if the file denoted by this abstract pathname exists and is a normal file; false otherwise

length

public int length()
Returns the length of the file denoted by this abstract pathname. The return value is unspecified if this pathname denotes a directory.

Returns:
The length, in bytes, of the file denoted by this abstract pathname, or 0L if the file does not exist

createNewFile

public boolean createNewFile()
                      throws IOException
Atomically creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist. The check for the existence of the file and the creation of the file if it does not exist are a single operation that is atomic with respect to all other filesystem activities that might affect the file.

Note: this method should not be used for file-locking, as the resulting protocol cannot be made to work reliably. The FileLock facility should be used instead.

Returns:
true if the named file does not exist and was successfully created; false if the named file already exists
Throws:
IOException - If an I/O error occurred
Since:
BFT 1.3

delete

public boolean delete()
Deletes the file denoted by this abstract pathname. Directories are not supported!

Returns:
true if and only if the file is successfully deleted; false otherwise

list

public String[] list()
Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname.

If this abstract pathname does not denote a directory, then this method returns null. Otherwise an array of strings is returned, one for each file or directory in the directory. 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.

There is no guarantee that the name strings in the resulting array will appear in any specific order; they are not, in particular, guaranteed to appear in alphabetical order.

Returns:
An array of strings naming the files and directories in the directory denoted by this abstract pathname. The array will be empty if the directory is empty. Returns null if this abstract pathname does not denote a directory, or if an I/O error occurs.

listFiles

public File[] listFiles()
Returns an array of abstract pathnames denoting the files in the directory denoted by this abstract pathname.

If this abstract pathname does not denote a directory, then this method returns null. Otherwise an array of File objects is returned, one for each file or directory in the directory. Pathnames denoting the directory itself and the directory's parent directory are not included in the result. Each resulting abstract pathname is constructed from this abstract pathname using the File(File, String) constructor. Therefore 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.

There is no guarantee that the name strings in the resulting array will appear in any specific order; they are not, in particular, guaranteed to appear in alphabetical order.

Returns:
An array of abstract pathnames denoting the files and directories in the directory denoted by this abstract pathname. The array will be empty if the directory is empty. Returns null if this abstract pathname does not denote a directory, or if an I/O error occurs.
Since:
1.2

listRoots

public static File[] listRoots()
List the available filesystem roots.

A particular Java platform may support zero or more hierarchically-organized file systems. Each file system has a root directory from which all other files in that file system can be reached. Windows platforms, for example, have a root directory for each active drive; UNIX platforms have a single root directory, namely "/". JControl platforms have a root directory for each existing filesystem, etc. "usba0" or "roma0". The set of available filesystem roots is affected by various system-level operations such the insertion or ejection of removable media and the disconnecting or unmounting of physical or virtual disk drives.

This method returns an array of File objects that denote the root directories of the available filesystem roots. It is guaranteed that the canonical pathname of any file physically present on the local machine will begin with one of the roots returned by this method.

Returns:
An array of File objects denoting the available filesystem roots, or null if the set of roots could not be determined. The array will be empty if there are no filesystem roots.
Since:
BFT 1.3

toString

public String toString()
Returns the pathname string of this abstract pathname. This is just the string returned by the getPath() method.

Overrides:
toString in class Object
Returns:
The string form of this abstract pathname

finalize

protected void finalize()
Free internal data structures used by this file object.