home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / io / File.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  33.4 KB  |  918 lines

  1. /*
  2.  * @(#)File.java    1.61 98/03/18
  3.  *
  4.  * Copyright 1994-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.io;
  16.  
  17. import java.util.Vector;
  18. import java.util.Map;
  19. import java.util.Hashtable;
  20. import java.util.Random;
  21.  
  22. /**
  23.  * Instances of this class represent the name of a file or directory 
  24.  * on the host file system. A file is specified by a pathname, which 
  25.  * can either be an absolute pathname or a pathname relative to the 
  26.  * current working directory. The pathname must follow the naming 
  27.  * conventions of the host platform. 
  28.  * <p>
  29.  * The <code>File</code> class is intended to provide an abstraction 
  30.  * that deals with most of the machine dependent complexities of 
  31.  * files and pathnames in a machine-independent fashion. 
  32.  * <p>
  33.  * Note that whenever a filename or path is used it is
  34.  * assumed that the host's file naming conventions are used.
  35.  *
  36.  * @version 1.61, 03/18/98
  37.  * @author  Arthur van Hoff
  38.  * @author  Jonathan Payne
  39.  * @since   JDK1.0
  40.  */
  41. public class File implements java.io.Serializable, Comparable {
  42.     /**
  43.      * The path of the file. The host's file separator is used.
  44.      */
  45.     private String path;
  46.  
  47.     /**
  48.      * The system-dependent path separator string. This field is 
  49.      * initialized to contain the value of the system property 
  50.      * <code>file.separator</code>. 
  51.      *
  52.      * @see     java.lang.System#getProperty(java.lang.String)
  53.      */
  54.     public static final String separator = initSeparator();
  55.  
  56.     private static String initSeparator() {
  57.     try {
  58.         java.security.AccessController.beginPrivileged();
  59.         return System.getProperty("file.separator");
  60.     } finally {
  61.             java.security.AccessController.endPrivileged();
  62.     }
  63.     }
  64.  
  65.     /**
  66.      * The system-dependent path separator character. This field is 
  67.      * initialized to contain the first character of the value of the 
  68.      * system property <code>file.separator</code>. This character 
  69.      * separates the directory and file components in a filename. 
  70.      *
  71.      * @see     java.lang.System#getProperty(java.lang.String)
  72.      */
  73.     public static final char separatorChar = separator.charAt(0);
  74.     
  75.     /**
  76.      * The system-dependent path separator string. This field is 
  77.      * initialized to contain the value of the system property 
  78.      * <code>path.separator</code>. 
  79.      *
  80.      * @see     java.lang.System#getProperty(java.lang.String)
  81.      */
  82.     public static final String pathSeparator = initPathSeparator();
  83.  
  84.     private static String initPathSeparator() {
  85.     try {
  86.         java.security.AccessController.beginPrivileged();
  87.         return System.getProperty("path.separator");
  88.     } finally {
  89.             java.security.AccessController.endPrivileged();
  90.     }
  91.     }
  92.  
  93.     /**
  94.      * The system-dependent path separator character. This field is 
  95.      * initialized to contain the first character of the value of the 
  96.      * system property <code>path.separator</code>. This character is 
  97.      * often used to separate filenames in a sequence of files given as a 
  98.      * "path list".
  99.      *
  100.      * @see     java.lang.System#getProperty(java.lang.String)
  101.      */
  102.     public static final char pathSeparatorChar = pathSeparator.charAt(0);
  103.     
  104.     /**
  105.      * Creates a <code>File</code> instance that represents the file 
  106.      * whose pathname is the given path argument. 
  107.      *
  108.      * @param      path   the file pathname.
  109.      * @exception  NullPointerException  if the file path is equal to
  110.      *               <code>null</code>.
  111.      * @see        java.io.File#getPath()
  112.      */
  113.     public File(String path) {
  114.     if (path == null) {
  115.         throw new NullPointerException();
  116.     }
  117.     this.path = path;
  118.     }    
  119.  
  120.     /**
  121.      * Creates a <code>File</code> instance whose pathname is the 
  122.      * pathname of the specified directory, followed by the separator 
  123.      * character, followed by the <code>name</code> argument. 
  124.      *
  125.      * @param   path   the directory pathname.
  126.      * @param   name   the file pathname.
  127.      * @see     java.io.File#getPath()
  128.      * @see     java.io.File#separator
  129.      */
  130.     public File(String path, String name) {
  131.     if (name == null) {
  132.         /* raise exception, per Java Language Spec
  133.          * 22.24.6 & 22.24.7
  134.          */
  135.         throw new NullPointerException();
  136.     }
  137.     if (path != null) {
  138.         if (path.endsWith(separator)) {
  139.         this.path = path + name;
  140.         } else {
  141.         this.path = path + separator + name;
  142.         } 
  143.     } else {
  144.         this.path = name;
  145.     }
  146.     }
  147.  
  148.     /**
  149.      * Creates a <code>File</code> instance that represents the file 
  150.      * with the specified name in the specified directory. 
  151.      * <p>
  152.      * If the directory argument is <code>null</code>, the resulting 
  153.      * <code>File</code> instance represents a file in the 
  154.      * (system-dependent) current directory whose pathname is the 
  155.      * <code>name</code> argument. Otherwise, the <code>File</code> 
  156.      * instance represents a file whose pathname is the pathname of the 
  157.      * directory, followed by the separator character, followed by the 
  158.      * <code>name</code> argument. 
  159.      *
  160.      * @param   dir   the directory.
  161.      * @param   name   the file pathname.
  162.      * @see     java.io.File#getPath()
  163.      * @see     java.io.File#separator
  164.      */
  165.     public File(File dir, String name) {
  166.     this(dir.getPath(), name);
  167.     }
  168.  
  169.     /**
  170.      * Returns the name of the file represented by this object. The name 
  171.      * is everything in the pathame after the last occurrence of the 
  172.      * separator character. 
  173.      *
  174.      * @return  the name of the file (without any directory components)
  175.      *          represented by this <code>File</code> object.
  176.      * @see     java.io.File#getPath()
  177.      * @see     java.io.File#separator
  178.      */
  179.     public String getName() {
  180.     int index = path.lastIndexOf(separatorChar);
  181.     return (index < 0) ? path : path.substring(index + 1);
  182.     }
  183.  
  184.     /**
  185.      * Returns the pathname of the file represented by this object.
  186.      *
  187.      * @return  the pathname represented by this <code>File</code> object.
  188.      */
  189.     public String getPath() {
  190.     return path;
  191.     }
  192.  
  193.     /**
  194.      * Returns the absolute pathname of the file represented by this object.
  195.      * If this object represents an absolute pathname, then return the 
  196.      * pathname. Otherwise, return a pathname that is a concatenation of 
  197.      * the current user directory, the separator character, and the 
  198.      * pathname of this file object. 
  199.      * <p>
  200.      * The system property <code>user.dir</code> contains the current 
  201.      * user directory. 
  202.      *
  203.      * @return  a system-dependent absolute pathname for this <code>File</code>.
  204.      * @see     java.io.File#getPath()
  205.      * @see     java.io.File#isAbsolute()
  206.      * @see     java.lang.System#getProperty(java.lang.String)
  207.      */
  208.     public String getAbsolutePath() {
  209.     return isAbsolute() ? path : System.getProperty("user.dir") + 
  210.         separator + path;
  211.     }
  212.  
  213.     /**
  214.      * Returns the canonical form of this <code>File</code> object's pathname.
  215.      * The precise definition of canonical form is system-dependent, but it
  216.      * usually specifies an absolute pathname in which all relative references
  217.      * and references to the current user directory have been completely
  218.      * resolved.  The canonical form of a pathname of a nonexistent file may
  219.      * not be defined.
  220.      *
  221.      * @exception IOException If an I/O error occurs, which is possible because
  222.      * the construction of the canonical path may require filesystem queries.
  223.      *
  224.      * @since   JDK1.1
  225.      */
  226.      public String getCanonicalPath() throws IOException {
  227.     return isAbsolute() ? canonPath(path) :
  228.         canonPath(System.getProperty("user.dir") + separator + path);
  229.     }
  230.  
  231.     /**
  232.      * Returns the parent part of the pathname of this <code>File</code>
  233.      * object, or <code>null</code> if the name has no parent part.  The parent
  234.      * part is generally everything leading up to the last occurrence of the
  235.      * separator character, although the precise definition is system
  236.      * dependent.  On UNIX, for example, the parent part of
  237.      * <code>"/usr/lib"</code> is <code>"/usr"</code>, whose parent part is
  238.      * <code>"/"</code>, which in turn has no parent.  On Windows platforms,
  239.      * the parent part of <code>"c:\java"</code> is <code>"c:\"</code>, which
  240.      * in turn has no parent.
  241.      *
  242.      * @see     java.io.File#getPath()
  243.      * @see     java.io.File#getCanonicalPath()
  244.      * @see     java.io.File#separator
  245.      */
  246.     public String getParent() {
  247.     /* This is correct for Unix and Win32; other platforms may require a
  248.            different algorithm */
  249.     int index = path.lastIndexOf(separatorChar);
  250.     if (index < 0)
  251.         return null;
  252.     if (!isAbsolute() || (path.indexOf(separatorChar) != index))
  253.         return path.substring(0, index);
  254.     if (index < path.length() - 1)
  255.         return path.substring(0, index + 1);
  256.     return null;
  257.     }
  258.  
  259.     private native boolean exists0();
  260.     private native boolean canWrite0();
  261.     private native boolean canRead0();
  262.     private native boolean isFile0();
  263.     private native boolean isDirectory0();
  264.     private native long lastModified0();
  265.     private native long length0();
  266.     private native boolean mkdir0();
  267.     private native boolean renameTo0(File dest);
  268.     private native boolean delete0();
  269.     private native boolean rmdir0(); // remove empty directory
  270.     private native String[] list0();
  271.     private native String canonPath(String p) throws IOException;
  272.     private static native void initIDs(); // set up JNI field/method IDs
  273.     private static native boolean exclusiveCreateFile(String filename);
  274.     private static native boolean addPathToDeletionList(String filename);
  275.  
  276.  
  277.     /*
  278.      * Support for temp file and on-exit deletion
  279.      */
  280.  
  281.     private static final int separatorLen = separator.length();
  282.  
  283.     /* if the directory doesn't end in a file separator, add one */
  284.     private static String canonicalizeDirectory(String directory) {
  285.     int separatorIndex = directory.length() - separatorLen;
  286.     if (!directory.substring(separatorIndex).equals(separator)) {
  287.         directory += separator;
  288.     }
  289.     return directory;
  290.     }
  291.  
  292.     private static File generateFile(String pattern, File directory) 
  293.     throws IOException {
  294.     String prefix = "";
  295.     char prefixFirstLetter;
  296.     String suffix = ".tmp";
  297.     int wildCardIndex = pattern.indexOf("#");
  298.     while (wildCardIndex > 0 && pattern.charAt(wildCardIndex-1) == '\\') {
  299.         wildCardIndex = pattern.indexOf("#", wildCardIndex + 1);
  300.     }
  301.     if (wildCardIndex != -1) {
  302.         prefix = pattern.substring(0, wildCardIndex);
  303.         suffix = pattern.substring(wildCardIndex + 1, pattern.length());
  304.     } else {
  305.         prefix = pattern;
  306.     }
  307.     if (prefix.length() < 3) {
  308.         throw new IllegalArgumentException("prefix too short: " + prefix);
  309.     }
  310.     prefixFirstLetter = prefix.charAt(0);
  311.     if (prefixFirstLetter < 'A' && prefixFirstLetter > 'Z' &&
  312.         prefixFirstLetter < 'a' && prefixFirstLetter > 'z') {
  313.         throw new IllegalArgumentException("prefix doesn't begin with " +
  314.                            "letter: " + prefix);
  315.     }
  316.     if (counter == 0) {
  317.         counter = (new Random().nextInt()) >> 16;
  318.     }
  319.     return new File(directory, prefix + counter++ + suffix);
  320.     }
  321.  
  322.     private static String tmpdir;
  323.  
  324.     private static String getTempDir() {
  325.     if (tmpdir == null) {
  326.         tmpdir = System.getProperty("java.io.tmpdir");
  327.     }
  328.     return tmpdir;
  329.     }
  330.  
  331.     private static int counter = 0;
  332.  
  333.     private static final Object lock = new Object();
  334.  
  335.     /**   
  336.      * <p>Creates an empty temporary file in the specified directory
  337.      * after a specified pattern.
  338.      * 
  339.      * <ul>
  340.      * 
  341.      * <li>The file returned did not exist prior to this method
  342.      * creating it.
  343.      * 
  344.      * <li>This method or any of its variations will never returned a file
  345.      * with the same name twice during the lifetime of the runtime.
  346.      *
  347.      * </ul>
  348.      * 
  349.      * <p>Temporary files are created in the specified directory. If
  350.      * the specified directory is null, the default temporary
  351.      * directory is used. The default temporary directory may be
  352.      * specified using the java.io.tmpdir property. If it is not
  353.      * explicitely specified, it is defaulted to the underlying
  354.      * platform's temporary directory (such as /tmp or /var/tmp on
  355.      * UNIX and c:\temp on Windows).
  356.      * 
  357.      * @param pattern a patterns after which to create the temp file
  358.      * name. The pattern is a string with the following syntax:
  359.      * prefix{#suffix}. Assuming that the # sign may be substituted
  360.      * with 8 characters, the complete pattern must result in a legal
  361.      * Java filename. The prefix is mandatory. It is recommended that
  362.      * the prefix be a short, meaningful string such as "hjb" or
  363.      * "mail". The suffix is optional, and must be delimited with a #
  364.      * character (Unicode 35). If a delimited character is to be
  365.      * included in either the prefix or the suffix, it must be escaped
  366.      * with the \ (Unicode 92) character. If no suffix is specified,
  367.      * the file will have the ".tmp" extension. Case may not be
  368.      * preserved, and if a suffix is provided, the suffix may be
  369.      * truncated. If the suffix contains an extension, only the first
  370.      * three characters in the extension are guaranteed to be
  371.      * preserved.
  372.      *
  373.      * @param directory the directory in which the temporary file is
  374.      * to be created. If this argument is not null, the directory must
  375.      * exist or an exception will be thrown. If the argument is null,
  376.      * it will be defaulted to the standard temporary directory.
  377.      * 
  378.      * @returns an empty temporary file.
  379.      * 
  380.      * @exception IllegalArgumentException if the pattern is not valid.
  381.      *
  382.      * @exception IOException if the runtime is unable to create the
  383.      * temporary file because of an unexpected condition, such as when
  384.      * the directory specified does not exist, there is no space left
  385.      * on the device, etc.  
  386.      *
  387.      * @exception SecurityException if a security manager exists, its
  388.      * <code>checkWrite</code> method is called with the result argument
  389.      * to see if the application is allowed read access to the file.  
  390.      *
  391.      * @since JDK1.2 
  392.      */
  393.     public static File createTempFile(String pattern, File directory) 
  394.     throws IOException {
  395.     synchronized (lock) {
  396.         if (directory == null) {
  397.         directory = new File(getTempDir());
  398.         }
  399.         File file = generateFile(pattern, directory);
  400.         String filename = file.getPath();
  401.         while (!exclusiveCreateFile(filename)) {
  402.         file = generateFile(pattern, directory);
  403.         filename = file.getPath();
  404.         }
  405.         SecurityManager security = System.getSecurityManager();
  406.         if (security != null) {
  407.         security.checkWrite(filename);
  408.         }
  409.         return new File(filename);
  410.     }
  411.     }
  412.  
  413.     /**     
  414.      * Creates an empty temporary file in the default temporary
  415.      * directory, after the specified pattern.
  416.      * 
  417.      * <p>This call is equivalent to calling <code><a
  418.      * href="#createTempFile(java.lang.String,
  419.      * java.io.File)">createTempFile(String, File)<a></code> with a
  420.      * null argument for the directory.
  421.      * 
  422.      * @param pattern a patterns after which to create the temp file
  423.      * name.
  424.      *
  425.      * @returns an empty temporary file.
  426.      * 
  427.      * @exception IllegalArgumentException if the pattern is not valid.
  428.      *
  429.      * @exception IOException if the runtime is unable to create the
  430.      * temporary file because of an unexpected condition, such as when
  431.      * the directory specified does not exist, there is no space left
  432.      * on the device, etc.  
  433.      *
  434.      * @see #createTempFile(java.lang.String, java.io.File) 
  435.      *
  436.      * @exception SecurityException if a security manager exists, its
  437.      * <code>checkWrite</code> method is called with the result argument
  438.      * to see if the application is allowed read access to the file.  
  439.      *
  440.      * @since JDK1.2 
  441.      */
  442.     public static File createTempFile(String prefix) throws IOException {
  443.     return createTempFile(prefix, null);
  444.     }
  445.  
  446.     /**    
  447.      * Requests that this File be deleted when the virtual machine
  448.      * terminates. Deletion will be attempted only for normal
  449.      * termination of the virtual machine, as defined by the Java
  450.      * Language Specification (12.9).
  451.      *
  452.      * <p>Once deletion has been requested, it is not possible to
  453.      * cancel the request. This method should therefore be used
  454.      * carefully.
  455.      *
  456.      * @exception SecurityException if a security manager exists, its
  457.      * <code>checkDelete</code> method is called with the pathname of
  458.      * this <code>File</code> to see if the application is allowed to
  459.      * delete the file.
  460.      *
  461.      * @see #delete
  462.      *
  463.      * @since JDK1.2
  464.      */
  465.     public void deleteOnExit() {
  466.     SecurityManager security = System.getSecurityManager();
  467.     if (security != null) {
  468.         security.checkDelete(path);
  469.     }
  470.     addPathToDeletionList(getPath());
  471.     }
  472.     
  473.  
  474.     /**
  475.      * Tests if this <code>File</code> exists. 
  476.      *
  477.      * @return     <code>true</code> if the file specified by this object
  478.      *             exists; <code>false</code> otherwise.
  479.      * @exception  SecurityException  if a security manager exists, its
  480.      *               <code>checkRead</code> method is called with the pathname
  481.      *               of this <code>File</code> to see if the application is
  482.      *               allowed read access to the file.
  483.      * @see        java.io.File#getPath()
  484.      * @see        java.lang.SecurityManager#checkRead(java.lang.String)
  485.      */
  486.     public boolean exists() {
  487.     SecurityManager security = System.getSecurityManager();
  488.     if (security != null) {
  489.         security.checkRead(path);
  490.     }
  491.     return exists0();
  492.     }
  493.  
  494.     /**
  495.      * Tests if the application can write to this file. 
  496.      *
  497.      * @return     <code>true</code> if and only if the file system 
  498.      *             actually contains a file specified by the path of 
  499.      *             this file <em>and</em> the application is allowed 
  500.      *             to write to the file; 
  501.      *            <code>false</code> otherwise.
  502.      * @exception  SecurityException  if a security manager exists, its
  503.      *               <code>checkWrite</code> method is called with the pathname
  504.      *               of this <code>File</code> to see if the application is
  505.      *               allowed write access to the file.
  506.      * @see        java.io.File#getPath()
  507.      * @see        java.lang.SecurityManager#checkWrite(java.lang.String)
  508.      */
  509.     public boolean canWrite() {
  510.     SecurityManager security = System.getSecurityManager();
  511.     if (security != null) {
  512.         security.checkWrite(path);
  513.     }
  514.     return canWrite0();
  515.     }
  516.  
  517.     /**
  518.      * Tests if the application can read from the specified file. 
  519.      *
  520.      * @return     <code>true</code> if the file specified by this object exists
  521.      *             and the application can read the file;
  522.      *             <code>false</code> otherwise.
  523.      * @exception  SecurityException  if a security manager exists, its
  524.      *               <code>checkRead</code> method is called with the pathname
  525.      *               of this <code>File</code> to see if the application is
  526.      *               allowed read access to the file.
  527.      * @see        java.io.File#getPath()
  528.      * @see        java.lang.SecurityManager#checkRead(java.lang.String)
  529.      */
  530.     public boolean canRead() {
  531.     SecurityManager security = System.getSecurityManager();
  532.     if (security != null) {
  533.         security.checkRead(path);
  534.     }
  535.     return canRead0();
  536.     }
  537.  
  538.     /**
  539.      * Tests if the file represented by this <code>File</code> 
  540.      * object is a "normal" file. 
  541.      * <p>
  542.      * A file is "normal" if it is not a directory and, in 
  543.      * addition, satisfies other system-dependent criteria. Any 
  544.      * non-directory file created by a Java application is guaranteed to 
  545.      * be a normal file. 
  546.      *
  547.      * @return     <code>true</code> if the file specified by this object
  548.      *             exists and is a "normal" file; <code>false</code> otherwise.
  549.      * @exception  SecurityException  If a security manager exists, its
  550.      *               <code>checkRead</code> method is called with the pathname
  551.      *               of this <code>File</code> to see if the application is
  552.      *               allowed read access to the file.
  553.      * @see        java.io.File#getPath()
  554.      * @see        java.lang.SecurityManager#checkRead(java.lang.String)
  555.      */
  556.     public boolean isFile() {
  557.     SecurityManager security = System.getSecurityManager();
  558.     if (security != null) {
  559.         security.checkRead(path);
  560.     }
  561.     return isFile0();
  562.     }
  563.  
  564.     /**
  565.      * Tests if the file represented by this <code>File</code> 
  566.      * object is a directory. 
  567.      *
  568.      * @return     <code>true</code> if this <code>File</code> exists and is a
  569.      *             directory; <code>false</code> otherwise.
  570.      * @exception  SecurityException  if a security manager exists, its
  571.      *               <code>checkRead</code> method is called with the pathname
  572.      *               of this <code>File</code> to see if the application is
  573.      *               allowed read access to the file.
  574.      * @see        java.io.File#getPath()
  575.      * @see        java.lang.SecurityManager#checkRead(java.lang.String)
  576.      */
  577.     public boolean isDirectory() {
  578.     SecurityManager security = System.getSecurityManager();
  579.     if (security != null) {
  580.         security.checkRead(path);
  581.     }
  582.     return isDirectory0();
  583.     }
  584.  
  585.     /**
  586.      * Tests if the file represented by this <code>File</code> object is an
  587.      * absolute pathname. The definition of an absolute pathname is system 
  588.      * dependent. For example, on UNIX, a pathname is absolute if its 
  589.      * first character is the separator character. On Windows platforms, 
  590.      * a pathname is absolute if its first character is an ASCII '\' or 
  591.      * '/', or if it begins with a letter followed by a colon. 
  592.      *
  593.      * @return  <code>true</code> if the pathname indicated by the
  594.      *          <code>File</code> object is an absolute pathname;
  595.      *          <code>false</code> otherwise.
  596.      * @see     java.io.File#getPath()
  597.      * @see     java.io.File#separator
  598.      */
  599.     public native boolean isAbsolute();
  600.  
  601.     /**
  602.      * Returns the time that the file represented by this 
  603.      * <code>File</code> object was last modified. 
  604.      * <p>
  605.      * The return value is system dependent and should only be used to 
  606.      * compare with other values returned by last modified. It should not 
  607.      * be interpreted as an absolute time. 
  608.      *
  609.      * @return     the time the file specified by this object was last modified,
  610.      *             or <code>0L</code> if the specified file does not exist.
  611.      * @exception  SecurityException  if a security manager exists, its
  612.      *               <code>checkRead</code> method is called with the pathname
  613.      *               of this <code>File</code> to see if the application is
  614.      *               allowed read access to the file.
  615.      * @see        java.io.File#getPath()
  616.      * @see        java.lang.SecurityManager#checkRead(java.lang.String)
  617.      */
  618.     public long lastModified() {
  619.     SecurityManager security = System.getSecurityManager();
  620.     if (security != null) {
  621.         security.checkRead(path);
  622.     }
  623.     return lastModified0();
  624.     }
  625.  
  626.     /**
  627.      * Returns the length of the file represented by this 
  628.      * <code>File</code> object. 
  629.      *
  630.      * @return     the length, in bytes, of the file specified by this object,
  631.      *             or <code>0L</code> if the specified file does not exist.
  632.      * @exception  SecurityException  if a security manager exists, its
  633.      *               <code>checkRead</code> method is called with the pathname
  634.      *               of this <code>File</code> to see if the application is
  635.      *               allowed read access to the file.
  636.      * @see        java.io.File#getPath()
  637.      * @see        java.lang.SecurityManager#checkRead(java.lang.String)
  638.      */
  639.     public long length() {
  640.     SecurityManager security = System.getSecurityManager();
  641.     if (security != null) {
  642.         security.checkRead(path);
  643.     }
  644.     return length0();
  645.     }
  646.  
  647.     /**
  648.      * Creates a directory whose pathname is specified by this 
  649.      * <code>File</code> object. 
  650.      *
  651.      * @return     <code>true</code> if the directory could be created;
  652.      *             <code>false</code> otherwise.
  653.      * @exception  SecurityException  if a security manager exists, its
  654.      *               <code>checkWrite</code> method is called with the pathname
  655.      *               of this <code>File</code> to see if the application is
  656.      *               allowed write access to the file.
  657.      * @see        java.io.File#getPath()
  658.      * @see        java.lang.SecurityManager#checkWrite(java.lang.String)
  659.      */
  660.     public boolean mkdir() {
  661.     SecurityManager security = System.getSecurityManager();
  662.     if (security != null) {
  663.         security.checkWrite(path);
  664.     }
  665.     return mkdir0();
  666.     }
  667.  
  668.     /**
  669.      * Renames the file specified by this <code>File</code> object to 
  670.      * have the pathname given by the <code>File</code> argument. 
  671.      *
  672.      * @param      dest   the new filename.
  673.      * @return     <code>true</code> if the renaming succeeds;
  674.      *             <code>false</code> otherwise.
  675.      * @exception  SecurityException  if a security manager exists, its
  676.      *               <code>checkWrite</code> method is called both with the
  677.      *               pathname of this file object and with the pathname of the
  678.      *               destination target object to see if the application is
  679.      *               allowed to write to both files.
  680.      * @see        java.io.File#getPath()
  681.      * @see        java.lang.SecurityManager#checkWrite(java.lang.String)
  682.      */
  683.     public boolean renameTo(File dest) {
  684.     SecurityManager security = System.getSecurityManager();
  685.     if (security != null) {
  686.         security.checkWrite(path);
  687.         security.checkWrite(dest.path);
  688.     }
  689.     return renameTo0(dest);
  690.     }
  691.  
  692.     /**
  693.      * Creates a directory whose pathname is specified by this 
  694.      * <code>File</code> object, including any necessary parent directories.
  695.      *
  696.      * @return     <code>true</code> if the directory (or directories) could be
  697.      *             created; <code>false</code> otherwise.
  698.      * @exception  SecurityException  if a security manager exists, its
  699.      *               <code>checkWrite</code> method is called with the pathname
  700.      *               of each of the directories that is to be created, before
  701.      *               any of the directories are created.
  702.      * @see        java.io.File#getPath()
  703.      * @see        java.lang.SecurityManager#checkWrite(java.lang.String)
  704.      */
  705.     public boolean mkdirs() {
  706.     if(exists()) {
  707.         return false;
  708.     }
  709.     if (mkdir()) {
  710.          return true;
  711.      }
  712.  
  713.     String parent = getParent();
  714.     return (parent != null) && (new File(parent).mkdirs() && mkdir());
  715.     }
  716.  
  717.     /**
  718.      * Returns a list of the files in the directory specified by this
  719.      * <code>File</code> object. 
  720.      *
  721.      * @return     an array of file names in the specified directory.
  722.      *             This list does not include the current directory or the
  723.      *             parent directory ("<code>.</code>" and "<code>..</code>"
  724.      *             on Unix systems).
  725.      * @exception  SecurityException  If a security manager exists, its
  726.      *               <code>checkRead</code> method is called with the pathname
  727.      *               of this <code>File</code> to see if the application is
  728.      *               allowed read access to the file.
  729.      * @see        java.io.File#getPath()
  730.      * @see        java.lang.SecurityManager#checkRead(java.lang.String)
  731.      */
  732.     public String[] list() {
  733.     SecurityManager security = System.getSecurityManager();
  734.     if (security != null) {
  735.         security.checkRead(path);
  736.     }
  737.     return list0();
  738.     }
  739.  
  740.     /**
  741.      * Returns a list of the files in the directory specified by this 
  742.      * <code>File</code> that satisfy the specified filter. 
  743.      *
  744.      * @param      filter   a filename filter.
  745.      * @return     an array of file names in the specified directory.
  746.      *             This list does not include the current directory or the
  747.      *             parent directory ("<code>.</code>" and "<code>..</code>"
  748.      *             on Unix systems).
  749.      * @exception  SecurityException  If a security manager exists, its
  750.      *               <code>checkRead</code> method is called with the pathname
  751.      *               of this <code>File</code> to see if the application is
  752.      *               allowed read access to the file.
  753.      * @see        java.io.File#getPath()
  754.      * @see        java.io.FilenameFilter
  755.      * @see        java.lang.SecurityManager#checkRead(java.lang.String)
  756.      */
  757.     public String[] list(FilenameFilter filter) {
  758.     String names[] = list();
  759.  
  760.     if (names == null) {
  761.         return null;
  762.     }
  763.  
  764.     // Fill in the Vector
  765.     Vector v = new Vector();
  766.     for (int i = 0 ; i < names.length ; i++) {
  767.         if ((filter == null) || filter.accept(this, names[i])) {
  768.         v.addElement(names[i]);
  769.         }
  770.     }
  771.  
  772.     // Create the array
  773.     String files[] = new String[v.size()];
  774.     v.copyInto(files);
  775.  
  776.     return files;
  777.     }
  778.  
  779.     /**
  780.      * Deletes the file specified by this object.  If the target
  781.      * file to be deleted is a directory, it must be empty for deletion
  782.      * to succeed.
  783.      *
  784.      * @return     <code>true</code> if the file is successfully deleted;
  785.      *             <code>false</code> otherwise.
  786.      * @exception  SecurityException  if a security manager exists, its
  787.      *               <code>checkDelete</code> method is called with the
  788.      *               pathname of this <code>File</code> to see if the
  789.      *               application is allowed to delete the file.
  790.      * @see        java.io.File#getPath()
  791.      * @see        java.lang.SecurityManager#checkDelete(java.lang.String)
  792.      */
  793.     public boolean delete() {
  794.     SecurityManager security = System.getSecurityManager();
  795.     if (security != null) {
  796.         security.checkDelete(path);
  797.     }
  798.     if(isDirectory0())
  799.         return rmdir0();
  800.     else
  801.         return delete0();
  802.     }
  803.  
  804.     /**
  805.      * Computes a hashcode for the file.
  806.      *
  807.      * @return  a hash code value for this <code>File</code> object.
  808.      */
  809.     public int hashCode() {
  810.     return path.hashCode() ^ 1234321;
  811.     }
  812.  
  813.     /**
  814.      * Compares this object against the specified object.
  815.      * Returns <code>true</code> if and only if the argument is 
  816.      * not <code>null</code> and is a <code>File</code> object whose 
  817.      * pathname is equal to the pathname of this object. 
  818.      *
  819.      * @param   obj   the object to compare with.
  820.      * @return  <code>true</code> if the objects are the same;
  821.      *          <code>false</code> otherwise.
  822.      */
  823.     public boolean equals(Object obj) {
  824.     if ((obj != null) && (obj instanceof File)) {
  825.         return path.equals(((File)obj).path);
  826.     }
  827.     return false;
  828.     }
  829.  
  830.     /**
  831.      * Compares two Files lexicographically according to their canonical
  832.      * paths.
  833.      *
  834.      * @param   file   the <code>File</code> to be compared.
  835.      * @return  the value <code>0</code> if the argument File's pathname
  836.      *        is equal to this File's; a value less than <code>0</code> if
  837.      *        this File's pathname is lexicographically less than the File
  838.      *        argument's; and a value greater than <code>0</code> if this
  839.      *        File's pathname is lexicographically greater than the File
  840.      *        argument's.
  841.      *
  842.      * @since   JDK1.2
  843.      */
  844.     public int compareTo(File file) {
  845.     return getPath().compareTo(file.getPath());
  846.     }
  847.  
  848.     /** 
  849.      * Compares this File to another Object according to their
  850.      * canonical paths.  If the Object is a File, this function
  851.      * behaves like <code>compareTo(File)</code>.  Otherwise, it
  852.      * throws a <code>ClassCastException</code> (as Files are
  853.      * comparable only to other Files).
  854.      *
  855.      * @param   o the <code>Object</code> to be compared.
  856.      * @return  the value <code>0</code> if the argument is a File whose
  857.      *        pathname is equal to this File's; a value less than
  858.      *        <code>0</code> if the argument is a File whose pathname
  859.      *        is lexicographically greater than this File's; and a value
  860.      *        greater than <code>0</code> if the argument is a File
  861.      *        whosse pathname is lexicographically less than this File's.
  862.      * @exception <code>ClassCastException</code> if the argument is not a
  863.      *          <code>File</code>. 
  864.      * @see     java.lang.Comparable
  865.      * @since JDK1.2 */
  866.     public int compareTo(Object o) {
  867.     return compareTo((File)o);
  868.     }
  869.  
  870.     /**
  871.      * Returns a string representation of this object. 
  872.      *
  873.      * @return  a string giving the pathname of this object. 
  874.      * @see     java.io.File#getPath()
  875.      */
  876.     public String toString() {
  877.     return getPath();
  878.     }
  879.  
  880.     /**
  881.      * WriteObject is called to save this filename.
  882.      * The separator character is saved also so it can be replaced
  883.      * in case the path is reconstituted on a different host type.
  884.      */
  885.     private synchronized void writeObject(java.io.ObjectOutputStream s)
  886.         throws IOException
  887.     {
  888.     s.defaultWriteObject();
  889.     s.writeChar(separatorChar); // Add the separator character
  890.     }
  891.  
  892.     /**
  893.      * readObject is called to restore this filename.
  894.      * The original separator character is read.  If it is different
  895.      * than the separator character on this system. The old seperator
  896.      * is replaced by the current separator.
  897.      */
  898.     private synchronized void readObject(java.io.ObjectInputStream s)
  899.          throws IOException, ClassNotFoundException
  900.     {
  901.     s.defaultReadObject();
  902.     char sep = s.readChar(); // read the previous seperator char
  903.     if (sep != separatorChar)
  904.         path = path.replace(sep, separatorChar);
  905.     }
  906.  
  907.     /** use serialVersionUID from JDK 1.0.2 for interoperability */
  908.     private static final long serialVersionUID = 301077366599181567L;
  909.  
  910.     /**
  911.      * call initIDs in the static initializer to set up JNI field IDs
  912.      * used by native methods.
  913.      */
  914.     static {
  915.         initIDs();
  916.     }
  917. }
  918.