home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-20 | 33.4 KB | 918 lines |
- /*
- * @(#)File.java 1.61 98/03/18
- *
- * Copyright 1994-1998 by Sun Microsystems, Inc.,
- * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
- * All rights reserved.
- *
- * This software is the confidential and proprietary information
- * of Sun Microsystems, Inc. ("Confidential Information"). You
- * shall not disclose such Confidential Information and shall use
- * it only in accordance with the terms of the license agreement
- * you entered into with Sun.
- */
-
- package java.io;
-
- import java.util.Vector;
- import java.util.Map;
- import java.util.Hashtable;
- import java.util.Random;
-
- /**
- * Instances of this class represent the name of a file or directory
- * on the host file system. A file is specified by a pathname, which
- * can either be an absolute pathname or a pathname relative to the
- * current working directory. The pathname must follow the naming
- * conventions of the host platform.
- * <p>
- * The <code>File</code> class is intended to provide an abstraction
- * that deals with most of the machine dependent complexities of
- * files and pathnames in a machine-independent fashion.
- * <p>
- * Note that whenever a filename or path is used it is
- * assumed that the host's file naming conventions are used.
- *
- * @version 1.61, 03/18/98
- * @author Arthur van Hoff
- * @author Jonathan Payne
- * @since JDK1.0
- */
- public class File implements java.io.Serializable, Comparable {
- /**
- * The path of the file. The host's file separator is used.
- */
- private String path;
-
- /**
- * The system-dependent path separator string. This field is
- * initialized to contain the value of the system property
- * <code>file.separator</code>.
- *
- * @see java.lang.System#getProperty(java.lang.String)
- */
- public static final String separator = initSeparator();
-
- private static String initSeparator() {
- try {
- java.security.AccessController.beginPrivileged();
- return System.getProperty("file.separator");
- } finally {
- java.security.AccessController.endPrivileged();
- }
- }
-
- /**
- * The system-dependent path separator character. This field is
- * initialized to contain the first character of the value of the
- * system property <code>file.separator</code>. This character
- * separates the directory and file components in a filename.
- *
- * @see java.lang.System#getProperty(java.lang.String)
- */
- public static final char separatorChar = separator.charAt(0);
-
- /**
- * The system-dependent path separator string. This field is
- * initialized to contain the value of the system property
- * <code>path.separator</code>.
- *
- * @see java.lang.System#getProperty(java.lang.String)
- */
- public static final String pathSeparator = initPathSeparator();
-
- private static String initPathSeparator() {
- try {
- java.security.AccessController.beginPrivileged();
- return System.getProperty("path.separator");
- } finally {
- java.security.AccessController.endPrivileged();
- }
- }
-
- /**
- * The system-dependent path separator character. This field is
- * initialized to contain the first character of the value of the
- * system property <code>path.separator</code>. This character is
- * often used to separate filenames in a sequence of files given as a
- * "path list".
- *
- * @see java.lang.System#getProperty(java.lang.String)
- */
- public static final char pathSeparatorChar = pathSeparator.charAt(0);
-
- /**
- * Creates a <code>File</code> instance that represents the file
- * whose pathname is the given path argument.
- *
- * @param path the file pathname.
- * @exception NullPointerException if the file path is equal to
- * <code>null</code>.
- * @see java.io.File#getPath()
- */
- public File(String path) {
- if (path == null) {
- throw new NullPointerException();
- }
- this.path = path;
- }
-
- /**
- * Creates a <code>File</code> instance whose pathname is the
- * pathname of the specified directory, followed by the separator
- * character, followed by the <code>name</code> argument.
- *
- * @param path the directory pathname.
- * @param name the file pathname.
- * @see java.io.File#getPath()
- * @see java.io.File#separator
- */
- public File(String path, String name) {
- if (name == null) {
- /* raise exception, per Java Language Spec
- * 22.24.6 & 22.24.7
- */
- throw new NullPointerException();
- }
- if (path != null) {
- if (path.endsWith(separator)) {
- this.path = path + name;
- } else {
- this.path = path + separator + name;
- }
- } else {
- this.path = name;
- }
- }
-
- /**
- * Creates a <code>File</code> instance that represents the file
- * with the specified name in the specified directory.
- * <p>
- * If the directory argument is <code>null</code>, the resulting
- * <code>File</code> instance represents a file in the
- * (system-dependent) current directory whose pathname is the
- * <code>name</code> argument. Otherwise, the <code>File</code>
- * instance represents a file whose pathname is the pathname of the
- * directory, followed by the separator character, followed by the
- * <code>name</code> argument.
- *
- * @param dir the directory.
- * @param name the file pathname.
- * @see java.io.File#getPath()
- * @see java.io.File#separator
- */
- public File(File dir, String name) {
- this(dir.getPath(), name);
- }
-
- /**
- * Returns the name of the file represented by this object. The name
- * is everything in the pathame after the last occurrence of the
- * separator character.
- *
- * @return the name of the file (without any directory components)
- * represented by this <code>File</code> object.
- * @see java.io.File#getPath()
- * @see java.io.File#separator
- */
- public String getName() {
- int index = path.lastIndexOf(separatorChar);
- return (index < 0) ? path : path.substring(index + 1);
- }
-
- /**
- * Returns the pathname of the file represented by this object.
- *
- * @return the pathname represented by this <code>File</code> object.
- */
- public String getPath() {
- return path;
- }
-
- /**
- * Returns the absolute pathname of the file represented by this object.
- * If this object represents an absolute pathname, then return the
- * pathname. Otherwise, return a pathname that is a concatenation of
- * the current user directory, the separator character, and the
- * pathname of this file object.
- * <p>
- * The system property <code>user.dir</code> contains the current
- * user directory.
- *
- * @return a system-dependent absolute pathname for this <code>File</code>.
- * @see java.io.File#getPath()
- * @see java.io.File#isAbsolute()
- * @see java.lang.System#getProperty(java.lang.String)
- */
- public String getAbsolutePath() {
- return isAbsolute() ? path : System.getProperty("user.dir") +
- separator + path;
- }
-
- /**
- * Returns the canonical form of this <code>File</code> object's pathname.
- * The precise definition of canonical form is system-dependent, but it
- * usually specifies an absolute pathname in which all relative references
- * and references to the current user directory have been completely
- * resolved. The canonical form of a pathname of a nonexistent file may
- * not be defined.
- *
- * @exception IOException If an I/O error occurs, which is possible because
- * the construction of the canonical path may require filesystem queries.
- *
- * @since JDK1.1
- */
- public String getCanonicalPath() throws IOException {
- return isAbsolute() ? canonPath(path) :
- canonPath(System.getProperty("user.dir") + separator + path);
- }
-
- /**
- * Returns the parent part of the pathname of this <code>File</code>
- * object, or <code>null</code> if the name has no parent part. The parent
- * part is generally everything leading up to the last occurrence of the
- * separator character, although the precise definition is system
- * dependent. On UNIX, for example, the parent part of
- * <code>"/usr/lib"</code> is <code>"/usr"</code>, whose parent part is
- * <code>"/"</code>, which in turn has no parent. On Windows platforms,
- * the parent part of <code>"c:\java"</code> is <code>"c:\"</code>, which
- * in turn has no parent.
- *
- * @see java.io.File#getPath()
- * @see java.io.File#getCanonicalPath()
- * @see java.io.File#separator
- */
- public String getParent() {
- /* This is correct for Unix and Win32; other platforms may require a
- different algorithm */
- int index = path.lastIndexOf(separatorChar);
- if (index < 0)
- return null;
- if (!isAbsolute() || (path.indexOf(separatorChar) != index))
- return path.substring(0, index);
- if (index < path.length() - 1)
- return path.substring(0, index + 1);
- return null;
- }
-
- private native boolean exists0();
- private native boolean canWrite0();
- private native boolean canRead0();
- private native boolean isFile0();
- private native boolean isDirectory0();
- private native long lastModified0();
- private native long length0();
- private native boolean mkdir0();
- private native boolean renameTo0(File dest);
- private native boolean delete0();
- private native boolean rmdir0(); // remove empty directory
- private native String[] list0();
- private native String canonPath(String p) throws IOException;
- private static native void initIDs(); // set up JNI field/method IDs
- private static native boolean exclusiveCreateFile(String filename);
- private static native boolean addPathToDeletionList(String filename);
-
-
- /*
- * Support for temp file and on-exit deletion
- */
-
- private static final int separatorLen = separator.length();
-
- /* if the directory doesn't end in a file separator, add one */
- private static String canonicalizeDirectory(String directory) {
- int separatorIndex = directory.length() - separatorLen;
- if (!directory.substring(separatorIndex).equals(separator)) {
- directory += separator;
- }
- return directory;
- }
-
- private static File generateFile(String pattern, File directory)
- throws IOException {
- String prefix = "";
- char prefixFirstLetter;
- String suffix = ".tmp";
- int wildCardIndex = pattern.indexOf("#");
- while (wildCardIndex > 0 && pattern.charAt(wildCardIndex-1) == '\\') {
- wildCardIndex = pattern.indexOf("#", wildCardIndex + 1);
- }
- if (wildCardIndex != -1) {
- prefix = pattern.substring(0, wildCardIndex);
- suffix = pattern.substring(wildCardIndex + 1, pattern.length());
- } else {
- prefix = pattern;
- }
- if (prefix.length() < 3) {
- throw new IllegalArgumentException("prefix too short: " + prefix);
- }
- prefixFirstLetter = prefix.charAt(0);
- if (prefixFirstLetter < 'A' && prefixFirstLetter > 'Z' &&
- prefixFirstLetter < 'a' && prefixFirstLetter > 'z') {
- throw new IllegalArgumentException("prefix doesn't begin with " +
- "letter: " + prefix);
- }
- if (counter == 0) {
- counter = (new Random().nextInt()) >> 16;
- }
- return new File(directory, prefix + counter++ + suffix);
- }
-
- private static String tmpdir;
-
- private static String getTempDir() {
- if (tmpdir == null) {
- tmpdir = System.getProperty("java.io.tmpdir");
- }
- return tmpdir;
- }
-
- private static int counter = 0;
-
- private static final Object lock = new Object();
-
- /**
- * <p>Creates an empty temporary file in the specified directory
- * after a specified pattern.
- *
- * <ul>
- *
- * <li>The file returned did not exist prior to this method
- * creating it.
- *
- * <li>This method or any of its variations will never returned a file
- * with the same name twice during the lifetime of the runtime.
- *
- * </ul>
- *
- * <p>Temporary files are created in the specified directory. If
- * the specified directory is null, the default temporary
- * directory is used. The default temporary directory may be
- * specified using the java.io.tmpdir property. If it is not
- * explicitely specified, it is defaulted to the underlying
- * platform's temporary directory (such as /tmp or /var/tmp on
- * UNIX and c:\temp on Windows).
- *
- * @param pattern a patterns after which to create the temp file
- * name. The pattern is a string with the following syntax:
- * prefix{#suffix}. Assuming that the # sign may be substituted
- * with 8 characters, the complete pattern must result in a legal
- * Java filename. The prefix is mandatory. It is recommended that
- * the prefix be a short, meaningful string such as "hjb" or
- * "mail". The suffix is optional, and must be delimited with a #
- * character (Unicode 35). If a delimited character is to be
- * included in either the prefix or the suffix, it must be escaped
- * with the \ (Unicode 92) character. If no suffix is specified,
- * the file will have the ".tmp" extension. Case may not be
- * preserved, and if a suffix is provided, the suffix may be
- * truncated. If the suffix contains an extension, only the first
- * three characters in the extension are guaranteed to be
- * preserved.
- *
- * @param directory the directory in which the temporary file is
- * to be created. If this argument is not null, the directory must
- * exist or an exception will be thrown. If the argument is null,
- * it will be defaulted to the standard temporary directory.
- *
- * @returns an empty temporary file.
- *
- * @exception IllegalArgumentException if the pattern is not valid.
- *
- * @exception IOException if the runtime is unable to create the
- * temporary file because of an unexpected condition, such as when
- * the directory specified does not exist, there is no space left
- * on the device, etc.
- *
- * @exception SecurityException if a security manager exists, its
- * <code>checkWrite</code> method is called with the result argument
- * to see if the application is allowed read access to the file.
- *
- * @since JDK1.2
- */
- public static File createTempFile(String pattern, File directory)
- throws IOException {
- synchronized (lock) {
- if (directory == null) {
- directory = new File(getTempDir());
- }
- File file = generateFile(pattern, directory);
- String filename = file.getPath();
- while (!exclusiveCreateFile(filename)) {
- file = generateFile(pattern, directory);
- filename = file.getPath();
- }
- SecurityManager security = System.getSecurityManager();
- if (security != null) {
- security.checkWrite(filename);
- }
- return new File(filename);
- }
- }
-
- /**
- * Creates an empty temporary file in the default temporary
- * directory, after the specified pattern.
- *
- * <p>This call is equivalent to calling <code><a
- * href="#createTempFile(java.lang.String,
- * java.io.File)">createTempFile(String, File)<a></code> with a
- * null argument for the directory.
- *
- * @param pattern a patterns after which to create the temp file
- * name.
- *
- * @returns an empty temporary file.
- *
- * @exception IllegalArgumentException if the pattern is not valid.
- *
- * @exception IOException if the runtime is unable to create the
- * temporary file because of an unexpected condition, such as when
- * the directory specified does not exist, there is no space left
- * on the device, etc.
- *
- * @see #createTempFile(java.lang.String, java.io.File)
- *
- * @exception SecurityException if a security manager exists, its
- * <code>checkWrite</code> method is called with the result argument
- * to see if the application is allowed read access to the file.
- *
- * @since JDK1.2
- */
- public static File createTempFile(String prefix) throws IOException {
- return createTempFile(prefix, null);
- }
-
- /**
- * Requests that this File be deleted when the virtual machine
- * terminates. Deletion will be attempted only for normal
- * termination of the virtual machine, as defined by the Java
- * Language Specification (12.9).
- *
- * <p>Once deletion has been requested, it is not possible to
- * cancel the request. This method should therefore be used
- * carefully.
- *
- * @exception SecurityException if a security manager exists, its
- * <code>checkDelete</code> method is called with the pathname of
- * this <code>File</code> to see if the application is allowed to
- * delete the file.
- *
- * @see #delete
- *
- * @since JDK1.2
- */
- public void deleteOnExit() {
- SecurityManager security = System.getSecurityManager();
- if (security != null) {
- security.checkDelete(path);
- }
- addPathToDeletionList(getPath());
- }
-
-
- /**
- * Tests if this <code>File</code> exists.
- *
- * @return <code>true</code> if the file specified by this object
- * exists; <code>false</code> otherwise.
- * @exception SecurityException if a security manager exists, its
- * <code>checkRead</code> method is called with the pathname
- * of this <code>File</code> to see if the application is
- * allowed read access to the file.
- * @see java.io.File#getPath()
- * @see java.lang.SecurityManager#checkRead(java.lang.String)
- */
- public boolean exists() {
- SecurityManager security = System.getSecurityManager();
- if (security != null) {
- security.checkRead(path);
- }
- return exists0();
- }
-
- /**
- * Tests if the application can write to this file.
- *
- * @return <code>true</code> if and only if the file system
- * actually contains a file specified by the path of
- * this file <em>and</em> the application is allowed
- * to write to the file;
- * <code>false</code> otherwise.
- * @exception SecurityException if a security manager exists, its
- * <code>checkWrite</code> method is called with the pathname
- * of this <code>File</code> to see if the application is
- * allowed write access to the file.
- * @see java.io.File#getPath()
- * @see java.lang.SecurityManager#checkWrite(java.lang.String)
- */
- public boolean canWrite() {
- SecurityManager security = System.getSecurityManager();
- if (security != null) {
- security.checkWrite(path);
- }
- return canWrite0();
- }
-
- /**
- * Tests if the application can read from the specified file.
- *
- * @return <code>true</code> if the file specified by this object exists
- * and the application can read the file;
- * <code>false</code> otherwise.
- * @exception SecurityException if a security manager exists, its
- * <code>checkRead</code> method is called with the pathname
- * of this <code>File</code> to see if the application is
- * allowed read access to the file.
- * @see java.io.File#getPath()
- * @see java.lang.SecurityManager#checkRead(java.lang.String)
- */
- public boolean canRead() {
- SecurityManager security = System.getSecurityManager();
- if (security != null) {
- security.checkRead(path);
- }
- return canRead0();
- }
-
- /**
- * Tests if the file represented by this <code>File</code>
- * object is a "normal" file.
- * <p>
- * 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.
- *
- * @return <code>true</code> if the file specified by this object
- * exists and is a "normal" file; <code>false</code> otherwise.
- * @exception SecurityException If a security manager exists, its
- * <code>checkRead</code> method is called with the pathname
- * of this <code>File</code> to see if the application is
- * allowed read access to the file.
- * @see java.io.File#getPath()
- * @see java.lang.SecurityManager#checkRead(java.lang.String)
- */
- public boolean isFile() {
- SecurityManager security = System.getSecurityManager();
- if (security != null) {
- security.checkRead(path);
- }
- return isFile0();
- }
-
- /**
- * Tests if the file represented by this <code>File</code>
- * object is a directory.
- *
- * @return <code>true</code> if this <code>File</code> exists and is a
- * directory; <code>false</code> otherwise.
- * @exception SecurityException if a security manager exists, its
- * <code>checkRead</code> method is called with the pathname
- * of this <code>File</code> to see if the application is
- * allowed read access to the file.
- * @see java.io.File#getPath()
- * @see java.lang.SecurityManager#checkRead(java.lang.String)
- */
- public boolean isDirectory() {
- SecurityManager security = System.getSecurityManager();
- if (security != null) {
- security.checkRead(path);
- }
- return isDirectory0();
- }
-
- /**
- * Tests if the file represented by this <code>File</code> object is an
- * absolute pathname. The definition of an absolute pathname is system
- * dependent. For example, on UNIX, a pathname is absolute if its
- * first character is the separator character. On Windows platforms,
- * a pathname is absolute if its first character is an ASCII '\' or
- * '/', or if it begins with a letter followed by a colon.
- *
- * @return <code>true</code> if the pathname indicated by the
- * <code>File</code> object is an absolute pathname;
- * <code>false</code> otherwise.
- * @see java.io.File#getPath()
- * @see java.io.File#separator
- */
- public native boolean isAbsolute();
-
- /**
- * Returns the time that the file represented by this
- * <code>File</code> object was last modified.
- * <p>
- * The return value is system dependent and should only be used to
- * compare with other values returned by last modified. It should not
- * be interpreted as an absolute time.
- *
- * @return the time the file specified by this object was last modified,
- * or <code>0L</code> if the specified file does not exist.
- * @exception SecurityException if a security manager exists, its
- * <code>checkRead</code> method is called with the pathname
- * of this <code>File</code> to see if the application is
- * allowed read access to the file.
- * @see java.io.File#getPath()
- * @see java.lang.SecurityManager#checkRead(java.lang.String)
- */
- public long lastModified() {
- SecurityManager security = System.getSecurityManager();
- if (security != null) {
- security.checkRead(path);
- }
- return lastModified0();
- }
-
- /**
- * Returns the length of the file represented by this
- * <code>File</code> object.
- *
- * @return the length, in bytes, of the file specified by this object,
- * or <code>0L</code> if the specified file does not exist.
- * @exception SecurityException if a security manager exists, its
- * <code>checkRead</code> method is called with the pathname
- * of this <code>File</code> to see if the application is
- * allowed read access to the file.
- * @see java.io.File#getPath()
- * @see java.lang.SecurityManager#checkRead(java.lang.String)
- */
- public long length() {
- SecurityManager security = System.getSecurityManager();
- if (security != null) {
- security.checkRead(path);
- }
- return length0();
- }
-
- /**
- * Creates a directory whose pathname is specified by this
- * <code>File</code> object.
- *
- * @return <code>true</code> if the directory could be created;
- * <code>false</code> otherwise.
- * @exception SecurityException if a security manager exists, its
- * <code>checkWrite</code> method is called with the pathname
- * of this <code>File</code> to see if the application is
- * allowed write access to the file.
- * @see java.io.File#getPath()
- * @see java.lang.SecurityManager#checkWrite(java.lang.String)
- */
- public boolean mkdir() {
- SecurityManager security = System.getSecurityManager();
- if (security != null) {
- security.checkWrite(path);
- }
- return mkdir0();
- }
-
- /**
- * Renames the file specified by this <code>File</code> object to
- * have the pathname given by the <code>File</code> argument.
- *
- * @param dest the new filename.
- * @return <code>true</code> if the renaming succeeds;
- * <code>false</code> otherwise.
- * @exception SecurityException if a security manager exists, its
- * <code>checkWrite</code> method is called both with the
- * pathname of this file object and with the pathname of the
- * destination target object to see if the application is
- * allowed to write to both files.
- * @see java.io.File#getPath()
- * @see java.lang.SecurityManager#checkWrite(java.lang.String)
- */
- public boolean renameTo(File dest) {
- SecurityManager security = System.getSecurityManager();
- if (security != null) {
- security.checkWrite(path);
- security.checkWrite(dest.path);
- }
- return renameTo0(dest);
- }
-
- /**
- * Creates a directory whose pathname is specified by this
- * <code>File</code> object, including any necessary parent directories.
- *
- * @return <code>true</code> if the directory (or directories) could be
- * created; <code>false</code> otherwise.
- * @exception SecurityException if a security manager exists, its
- * <code>checkWrite</code> method is called with the pathname
- * of each of the directories that is to be created, before
- * any of the directories are created.
- * @see java.io.File#getPath()
- * @see java.lang.SecurityManager#checkWrite(java.lang.String)
- */
- public boolean mkdirs() {
- if(exists()) {
- return false;
- }
- if (mkdir()) {
- return true;
- }
-
- String parent = getParent();
- return (parent != null) && (new File(parent).mkdirs() && mkdir());
- }
-
- /**
- * Returns a list of the files in the directory specified by this
- * <code>File</code> object.
- *
- * @return an array of file names in the specified directory.
- * This list does not include the current directory or the
- * parent directory ("<code>.</code>" and "<code>..</code>"
- * on Unix systems).
- * @exception SecurityException If a security manager exists, its
- * <code>checkRead</code> method is called with the pathname
- * of this <code>File</code> to see if the application is
- * allowed read access to the file.
- * @see java.io.File#getPath()
- * @see java.lang.SecurityManager#checkRead(java.lang.String)
- */
- public String[] list() {
- SecurityManager security = System.getSecurityManager();
- if (security != null) {
- security.checkRead(path);
- }
- return list0();
- }
-
- /**
- * Returns a list of the files in the directory specified by this
- * <code>File</code> that satisfy the specified filter.
- *
- * @param filter a filename filter.
- * @return an array of file names in the specified directory.
- * This list does not include the current directory or the
- * parent directory ("<code>.</code>" and "<code>..</code>"
- * on Unix systems).
- * @exception SecurityException If a security manager exists, its
- * <code>checkRead</code> method is called with the pathname
- * of this <code>File</code> to see if the application is
- * allowed read access to the file.
- * @see java.io.File#getPath()
- * @see java.io.FilenameFilter
- * @see java.lang.SecurityManager#checkRead(java.lang.String)
- */
- public String[] list(FilenameFilter filter) {
- String names[] = list();
-
- if (names == null) {
- return null;
- }
-
- // Fill in the Vector
- Vector v = new Vector();
- for (int i = 0 ; i < names.length ; i++) {
- if ((filter == null) || filter.accept(this, names[i])) {
- v.addElement(names[i]);
- }
- }
-
- // Create the array
- String files[] = new String[v.size()];
- v.copyInto(files);
-
- return files;
- }
-
- /**
- * Deletes the file specified by this object. If the target
- * file to be deleted is a directory, it must be empty for deletion
- * to succeed.
- *
- * @return <code>true</code> if the file is successfully deleted;
- * <code>false</code> otherwise.
- * @exception SecurityException if a security manager exists, its
- * <code>checkDelete</code> method is called with the
- * pathname of this <code>File</code> to see if the
- * application is allowed to delete the file.
- * @see java.io.File#getPath()
- * @see java.lang.SecurityManager#checkDelete(java.lang.String)
- */
- public boolean delete() {
- SecurityManager security = System.getSecurityManager();
- if (security != null) {
- security.checkDelete(path);
- }
- if(isDirectory0())
- return rmdir0();
- else
- return delete0();
- }
-
- /**
- * Computes a hashcode for the file.
- *
- * @return a hash code value for this <code>File</code> object.
- */
- public int hashCode() {
- return path.hashCode() ^ 1234321;
- }
-
- /**
- * Compares this object against the specified object.
- * Returns <code>true</code> if and only if the argument is
- * not <code>null</code> and is a <code>File</code> object whose
- * pathname is equal to the pathname of this object.
- *
- * @param obj the object to compare with.
- * @return <code>true</code> if the objects are the same;
- * <code>false</code> otherwise.
- */
- public boolean equals(Object obj) {
- if ((obj != null) && (obj instanceof File)) {
- return path.equals(((File)obj).path);
- }
- return false;
- }
-
- /**
- * Compares two Files lexicographically according to their canonical
- * paths.
- *
- * @param file the <code>File</code> to be compared.
- * @return the value <code>0</code> if the argument File's pathname
- * is equal to this File's; a value less than <code>0</code> if
- * this File's pathname is lexicographically less than the File
- * argument's; and a value greater than <code>0</code> if this
- * File's pathname is lexicographically greater than the File
- * argument's.
- *
- * @since JDK1.2
- */
- public int compareTo(File file) {
- return getPath().compareTo(file.getPath());
- }
-
- /**
- * Compares this File to another Object according to their
- * canonical paths. If the Object is a File, this function
- * behaves like <code>compareTo(File)</code>. Otherwise, it
- * throws a <code>ClassCastException</code> (as Files are
- * comparable only to other Files).
- *
- * @param o the <code>Object</code> to be compared.
- * @return the value <code>0</code> if the argument is a File whose
- * pathname is equal to this File's; a value less than
- * <code>0</code> if the argument is a File whose pathname
- * is lexicographically greater than this File's; and a value
- * greater than <code>0</code> if the argument is a File
- * whosse pathname is lexicographically less than this File's.
- * @exception <code>ClassCastException</code> if the argument is not a
- * <code>File</code>.
- * @see java.lang.Comparable
- * @since JDK1.2 */
- public int compareTo(Object o) {
- return compareTo((File)o);
- }
-
- /**
- * Returns a string representation of this object.
- *
- * @return a string giving the pathname of this object.
- * @see java.io.File#getPath()
- */
- public String toString() {
- return getPath();
- }
-
- /**
- * WriteObject is called to save this filename.
- * The separator character is saved also so it can be replaced
- * in case the path is reconstituted on a different host type.
- */
- private synchronized void writeObject(java.io.ObjectOutputStream s)
- throws IOException
- {
- s.defaultWriteObject();
- s.writeChar(separatorChar); // Add the separator character
- }
-
- /**
- * readObject is called to restore this filename.
- * The original separator character is read. If it is different
- * than the separator character on this system. The old seperator
- * is replaced by the current separator.
- */
- private synchronized void readObject(java.io.ObjectInputStream s)
- throws IOException, ClassNotFoundException
- {
- s.defaultReadObject();
- char sep = s.readChar(); // read the previous seperator char
- if (sep != separatorChar)
- path = path.replace(sep, separatorChar);
- }
-
- /** use serialVersionUID from JDK 1.0.2 for interoperability */
- private static final long serialVersionUID = 301077366599181567L;
-
- /**
- * call initIDs in the static initializer to set up JNI field IDs
- * used by native methods.
- */
- static {
- initIDs();
- }
- }
-