/[classpath]/classpath/java/io/File.java
ViewVC logotype

Diff of /classpath/java/io/File.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.22 by mark, Wed Feb 12 21:35:16 2003 UTC revision 1.23 by arenn, Mon Mar 3 00:31:13 2003 UTC
# Line 1  Line 1 
1  /* File.java -- Class representing a file on disk  /* File.java -- Class representing a file on disk
2     Copyright (C) 1998, 1999, 2001 Free Software Foundation, Inc.     Copyright (C) 1998, 1999, 2001, 2003 Free Software Foundation, Inc.
3    
4  This file is part of GNU Classpath.  This file is part of GNU Classpath.
5    
# Line 51  import gnu.java.io.PlatformHelper; Line 51  import gnu.java.io.PlatformHelper;
51    * types of path separators ("/" versus "\", for example).  It also    * types of path separators ("/" versus "\", for example).  It also
52    * contains method useful for creating and deleting files and directories.    * contains method useful for creating and deleting files and directories.
53    *    *
   * @version 0.0  
   *  
54    * @author Aaron M. Renn (arenn@urbanophile.com)    * @author Aaron M. Renn (arenn@urbanophile.com)
55    */    */
56  public class File implements Serializable, Comparable  public class File implements Serializable, Comparable
57  {  {
58    static final long serialVersionUID = 301077366599181567L;    static final long serialVersionUID = 301077366599181567L;
59    
60  /*************************************************************************/    /*************************************************************************/
61    
62  /*    /*
63   * Class Variables     * Class Variables
64   */     */
65    
66    /**      /**
67     * This is the path separator string for the current host. This field       * This is the path separator string for the current host. This field
68     * contains the value of the <code>file.separator</code> system property.       * contains the value of the <code>file.separator</code> system property.
69     * An example separator string would be "/" on the GNU system.       * An example separator string would be "/" on the GNU system.
70         */
71        public static final String separator = System.getProperty("file.separator");
72    
73        /**
74         * This is the first character of the file separator string.  On many
75         * hosts (for example, on the GNU system), this represents the entire
76         * separator string.  The complete separator string is obtained from the
77         * <code>file.separator</code>system property.
78         */
79        public static final char separatorChar = separator.charAt(0);
80    
81        /**
82         * This is the string that is used to separate the host name from the
83         * path name in paths than include the host name.  It is the value of
84         * the <code>path.separator</code> system property.
85         */
86        public static final String pathSeparator =
87           System.getProperty("path.separator");
88        
89        /**
90         * This is the first character of the string used to separate the host name
91         * from the path name in paths that include a host.  The separator string
92         * is taken from the <code>path.separator</code> system property.
93         */
94        public static final char pathSeparatorChar = pathSeparator.charAt(0);
95        
96        static
97        {
98          if (Configuration.INIT_LOAD_LIBRARY)
99            {
100              System.loadLibrary ("javaio");
101            }
102        }
103        
104      /*************************************************************************/
105    
106      /*
107       * Instance Variables
108     */     */
   public static final String separator = System.getProperty("file.separator");  
109    
110    /**    /**
111     * This is the first character of the file separator string.  On many      * This is the path to the file set when the object is created.  It
112     * hosts (for example, on the GNU system), this represents the entire      * may be an absolute or relative path name.
113     * separator string.  The complete separator string is obtained from the      */
114     * <code>file.separator</code>system property.    private String path;
115    
116      /*************************************************************************/
117    
118      /*
119       * Class Methods
120     */     */
   public static final char separatorChar = separator.charAt(0);  
121    
122    /**    /**
123     * This is the string that is used to separate the host name from the      * This method creates a temporary file in the system temporary directory.
124     * path name in paths than include the host name.  It is the value of      * The files created are guaranteed not to currently exist and the same file
125     * the <code>path.separator</code> system property.      * name will never be used twice in the same virtual machine instance.  The
126     */      * system temporary directory is determined by examinging the
127    public static final String pathSeparator = System.getProperty("path.separator");      * <code>java.io.tmpdir</code> system property.
128          * <p>
129        * The <code>prefix</code> parameter is a sequence of at least three
130        * characters that are used as the start of the generated filename.  The
131        * <code>suffix</code> parameter is a sequence of characters that is used
132        * to terminate the file name.  This parameter may be <code>null</code>
133        * and if it is, the suffix defaults to ".tmp".
134        * <p>
135        * If a <code>SecurityManager</code> exists, then its <code>checkWrite</code>
136        * method is used to verify that this operation is permitted.
137        * <p>
138        * This method is identical to calling
139        * <code>createTempFile(prefix, suffix, null)</code>.
140        *
141        * @param prefix The character prefix to use in generating the path name.
142        * @param suffix The character suffix to use in generating the path name.
143        *
144        * @exception IllegalArgumentException If the prefix or suffix are not valid.
145        * @exception SecurityException If there is no permission to perform
146        * this operation
147        * @exception IOException If an error occurs
148        */
149      public static File createTempFile(String prefix, String suffix)
150         throws IllegalArgumentException, SecurityException, IOException
151      {
152        return(createTempFile(prefix, suffix, null));
153      }
154    
155      /*************************************************************************/
156    
157    /**    /**
158     * This is the first character of the string used to separate the host name      * This method creates a temporary file in the specified directory.  If
159     * from the path name in paths that include a host.  The separator string      * the directory name is null, then this method uses the system temporary
160     * is taken from the <code>path.separator</code> system property.      * directory. The files created are guaranteed not to currently exist and
161     */      * the same file name will never be used twice in the same virtual
162    public static final char pathSeparatorChar = pathSeparator.charAt(0);      * machine instance.  
163          * The system temporary directory is determined by examinging the
164    static      * <code>java.io.tmpdir</code> system property.
165        * <p>
166        * The <code>prefix</code> parameter is a sequence of at least three
167        * characters that are used as the start of the generated filename.  The
168        * <code>suffix</code> parameter is a sequence of characters that is used
169        * to terminate the file name.  This parameter may be <code>null</code>
170        * and if it is, the suffix defaults to ".tmp".
171        * <p>
172        * If a <code>SecurityManager</code> exists, then its <code>checkWrite</code>
173        * method is used to verify that this operation is permitted.
174        *
175        * @param prefix The character prefix to use in generating the path name.
176        * @param suffix The character suffix to use in generating the path name.
177        * @param directory The directory to create the file in, or
178        * <code>null</code> for the default temporary directory
179        *
180        * @exception IllegalArgumentException If the patterns is not valid
181        * @exception SecurityException If there is no permission to perform
182        * this operation
183        * @exception IOException If an error occurs
184        */
185      public static synchronized File createTempFile(String prefix,
186                                                     String suffix, File directory)
187        throws IllegalArgumentException, SecurityException, IOException
188    {    {
189      if (Configuration.INIT_LOAD_LIBRARY)      // Grab the system temp directory if necessary
190        if (directory == null)
191        {        {
192          System.loadLibrary ("javaio");          String dirname = System.getProperty("java.io.tmpdir");
193            if (dirname == null)
194              throw new IOException("Cannot determine system temporary directory");
195    
196            directory = new File(dirname);
197            if (!directory.exists())
198              throw new IOException("System temporary directory " +
199                                    directory.getName() + " does not exist.");
200            if (!directory.isDirectory())
201              throw new IOException("System temporary directory " +
202                                    directory.getName() +
203                                    " is not really a directory.");
204        }        }
   }  
     
 /*************************************************************************/  
205    
206  /*      // Now process the prefix and suffix.
207   * Instance Variables      if (prefix.length() < 3)
208   */        throw new IllegalArgumentException("Prefix too short: " + prefix);
209    
210  /**      if (suffix == null)
211    * This is the path to the file set when the object is created.  It        suffix = ".tmp";
212    * may be an absolute or relative path name.  
213    */      // Now identify a file name and make sure it doesn't exist
214  private String path;      File f;
215        for(;;)
216          {
217            String filename = prefix + System.currentTimeMillis() + suffix;
218            f = new File(directory, filename);
219    
220  /*************************************************************************/          if (f.exists())
221              continue;
222            else
223              break;
224          }
225    
226  /*      // Verify that we are allowed to create this file
227   * Class Methods      SecurityManager sm = System.getSecurityManager();
228   */      if (sm != null)
229          sm.checkWrite(f.getAbsolutePath());
230    
231        // Now create the file and return our file object
232        createInternal(f.getAbsolutePath());
233        return(f);
234      }
235    
236  /**    /*************************************************************************/
   * This method creates a temporary file in the system temporary directory. The  
   * files created are guaranteed not to currently exist and the same file name  
   * will never be used twice in the same virtual machine instance.  The  
   * system temporary directory is determined by examinging the  
   * <code>java.io.tmpdir</code> system property.  
   * <p>  
   * The <code>prefix</code> parameter is a sequence of at least three  
   * characters that are used as the start of the generated filename.  The  
   * <code>suffix</code> parameter is a sequence of characters that is used  
   * to terminate the file name.  This parameter may be <code>null</code>  
   * and if it is, the suffix defaults to ".tmp".  
   * <p>  
   * If a <code>SecurityManager</code> exists, then its <code>checkWrite</code>  
   * method is used to verify that this operation is permitted.  
   * <p>  
   * This method is identical to calling  
   * <code>createTempFile(prefix, suffix, null)</code>.  
   *  
   * @param prefix The character prefix to use in generating the path name.  
   * @param suffix The character suffix to use in generating the path name.  
   *  
   * @exception IllegalArgumentException If the prefix or suffix are not valid.  
   * @exception SecurityException If there is no permission to perform this operation  
   * @exception IOException If an error occurs  
   */  
 public static File  
 createTempFile(String prefix, String suffix) throws IllegalArgumentException,  
                                                     SecurityException,  
                                                     IOException  
 {  
   return(createTempFile(prefix, suffix, null));  
 }  
237    
238  /*************************************************************************/    /**
239        * This method is used to create a temporary file
240        */
241      private static native boolean createInternal(String name) throws IOException;
242    
243  /**    /*************************************************************************/
   * This method creates a temporary file in the specified directory.  If  
   * the directory name is null, then this method uses the system temporary  
   * directory. The files created are guaranteed not to currently exist and the  
   * same file name will never be used twice in the same virtual machine instance.    
   * The system temporary directory is determined by examinging the  
   * <code>java.io.tmpdir</code> system property.  
   * <p>  
   * The <code>prefix</code> parameter is a sequence of at least three  
   * characters that are used as the start of the generated filename.  The  
   * <code>suffix</code> parameter is a sequence of characters that is used  
   * to terminate the file name.  This parameter may be <code>null</code>  
   * and if it is, the suffix defaults to ".tmp".  
   * <p>  
   * If a <code>SecurityManager</code> exists, then its <code>checkWrite</code>  
   * method is used to verify that this operation is permitted.  
   *  
   * @param prefix The character prefix to use in generating the path name.  
   * @param suffix The character suffix to use in generating the path name.  
   * @param directory The directory to create the file in, or <code>null</code> for the default temporary directory  
   *  
   * @exception IllegalArgumentException If the patterns is not valid  
   * @exception SecurityException If there is no permission to perform this operation  
   * @exception IOException If an error occurs  
   */  
 public static synchronized File  
 createTempFile(String prefix, String suffix, File directory)  
                throws IllegalArgumentException, SecurityException, IOException  
 {  
   // Grab the system temp directory if necessary  
   if (directory == null)  
     {  
       String dirname = System.getProperty("java.io.tmpdir");  
       if (dirname == null)  
         throw new IOException("Cannot determine system temporary directory");  
   
       directory = new File(dirname);  
       if (!directory.exists())  
         throw new IOException("System temporary directory " +  
                               directory.getName() + " does not exist.");  
       if (!directory.isDirectory())  
         throw new IOException("System temporary directory " +  
                               directory.getName() + " is not really a directory.");  
     }  
244    
245    // Now process the prefix and suffix.    /**
246    if (prefix.length() < 3)      * This method returns an array of filesystem roots.  Some operating systems
247      throw new IllegalArgumentException("Prefix too short: " + prefix);      * have volume oriented filesystem.  This method provides a mechanism for
248        * determining which volumes exist.  GNU systems use a single hierarchical
249    if (suffix == null)      * filesystem, so will have only one "/" filesystem root.
250      suffix = ".tmp";      *
251        * @return An array of <code>File</code> objects for each filesystem root
252    // Now identify a file name and make sure it doesn't exist      * available.
253    File f;      */
254    for(;;)    public static File[] listRoots()
255      {    {
256        String filename = prefix + System.currentTimeMillis() + suffix;      File[] f = new File[1];
257        f = new File(directory, filename);      f[0] = new File("/");
258    
259        if (f.exists())      return(f);
260          continue;    }
       else  
         break;  
     }  
261    
262    // Verify that we are allowed to create this file    /*************************************************************************/
   SecurityManager sm = System.getSecurityManager();  
   if (sm != null)  
     sm.checkWrite(f.getAbsolutePath());  
   
   // Now create the file and return our file object  
   createInternal(f.getAbsolutePath());  
   return(f);  
 }  
263    
264  /*************************************************************************/    /*
265       * Constructors
266       */
267    
268  /**    /**
269    * This method is used to create a temporary file      * This method initializes a new <code>File</code> object to represent
270    */      * a file in the specified directory.  If the <code>directory</code>
271  private static native boolean createInternal(String name) throws IOException;      * argument is <code>null</code>, the file is assumed to be in the
272        * current directory as specified by the <code>user.dir</code> system
273        * property
274        *
275        * @param directory The directory this file resides in
276        * @param name The name of the file
277        */
278      public File(File directory, String name)
279      {
280        if (directory == null)
281          {
282            String dirname = System.getProperty("user.dir");
283            if (dirname == null)
284              throw new IllegalArgumentException
285                    ("Cannot determine default user directory");
286    
287  /*************************************************************************/          directory = new File(dirname);
288          }
289    
290  /**      String dirpath = directory.getPath();
291    * This method returns an array of filesystem roots.  Some operating systems      if (PlatformHelper.isRootDirectory(dirpath))
292    * have volume oriented filesystem.  This method provides a mechanism for        path = dirpath + name;
293    * determining which volumes exist.  GNU systems use a single hierarchical      else
294    * filesystem, so will have only one "/" filesystem root.        path = dirpath + separator + name;
295    *    }
   * @return An array of <code>File</code> objects for each filesystem root  
   * available.  
   */  
 public static File[]  
 listRoots()  
 {  
   File[] f = new File[1];  
   f[0] = new File("/");  
296    
297    return(f);    /*************************************************************************/
 }  
298    
299  /*************************************************************************/    /**
300        * This method initializes a new <code>File</code> object to represent
301        * a file in the specified named directory.  The path name to the file
302        * will be the directory name plus the separator string plus the file
303        * name.  If the directory path name ends in the separator string, another
304        * separator string will still be appended.
305        *
306        * @param dirname The path to the directory the file resides in
307        * @param name The name of the file
308        */
309      public File(String dirname, String name)
310      {
311          this(dirname==null? (File)null: new File(dirname), name);
312      }
313    
314  /*    /*************************************************************************/
  * Constructors  
  */  
315    
316  /**    /**
317    * This method initializes a new <code>File</code> object to represent      * This method initializes a new <code>File</code> object to represent
318    * a file in the specified directory.  If the <code>directory</code>      * a file with the specified path.
319    * argument is <code>null</code>, the file is assumed to be in the      *
320    * current directory as specified by the <code>user.dir</code> system      * @param name The path name of the file
321    * property      */
322    *    public File(String name)
323    * @param directory The directory this file resides in    {
324    * @param name The name of the file      path = name;
   */  
 public  
 File(File directory, String name)  
 {  
   if (directory == null)  
     {  
       String dirname = System.getProperty("user.dir");  
       if (dirname == null)  
         throw new IllegalArgumentException  
                 ("Cannot determine default user directory");  
325    
326        directory = new File(dirname);      // Per the spec
327      }      if (path == null)
328          throw new NullPointerException("File name is null");
329    
330        while (!PlatformHelper.isRootDirectory(path)
331             && PlatformHelper.endWithSeparator(path))
332            path = PlatformHelper.removeTailSeparator(path);
333      }
334    
335    String dirpath = directory.getPath();    /*************************************************************************/
   if (PlatformHelper.isRootDirectory(dirpath))  
     path = dirpath + name;  
   else  
     path = dirpath + separator + name;  
 }  
336    
337  /*************************************************************************/    /*
338       * Instance Methods
339       */
340    
341  /**    /**
342    * This method initializes a new <code>File</code> object to represent      * This method returns the name of the file.  This is everything in the
343    * a file in the specified named directory.  The path name to the file      * complete path of the file after the last instance of the separator
344    * will be the directory name plus the separator string plus the file      * string.
345    * name.  If the directory path name ends in the separator string, another      *
346    * separator string will still be appended.      * @return The file name
347    *      */
348    * @param dirname The path to the directory the file resides in    public String getName()
349    * @param name The name of the file    {
350    */      int pos = PlatformHelper.lastIndexOfSeparator (path);
351  public      if (pos == -1)
352  File(String dirname, String name)        return(path);
 {  
     this(  
       dirname==null? (File)null: new File(dirname),  
       name);  
 }  
353    
354  /*************************************************************************/      if (PlatformHelper.endWithSeparator (path))
355          return("");
356    
357  /**      return(path.substring(pos + separator.length()));
358    * This method initializes a new <code>File</code> object to represent    }
   * a file with the specified path.  
   *  
   * @param name The path name of the file  
   */  
 public  
 File(String name)  
 {  
   path = name;  
359    
360    // Per the spec    /*************************************************************************/
   if (path == null)  
     throw new NullPointerException("File name is null");  
   
   while (!PlatformHelper.isRootDirectory(path)  
          && PlatformHelper.endWithSeparator(path))  
       path = PlatformHelper.removeTailSeparator(path);  
 }  
   
 /*************************************************************************/  
   
 /*  
  * Instance Methods  
  */  
361    
362  /**    /**
363    * This method returns the name of the file.  This is everything in the      * Returns the path name that represents this file.  May be a relative
364    * complete path of the file after the last instance of the separator      * or an absolute path name
365    * string.      *
366    *      * @return The pathname of this file
367    * @return The file name      */
368    */    public String getPath()
369  public String    {
 getName()  
 {  
   int pos = PlatformHelper.lastIndexOfSeparator (path);  
   if (pos == -1)  
370      return(path);      return(path);
371      }
372    
373    if (PlatformHelper.endWithSeparator (path))    /*************************************************************************/
     return("");  
374    
375    return(path.substring(pos + separator.length()));    /**
376  }      * This method returns the path of this file as an absolute path name.
377        * If the path name is already absolute, then it is returned.  Otherwise
378        * the value returned is the current directory plus the separatory
379        * string plus the path of the file.  The current directory is determined
380        * from the <code>user.dir</code> system property.
381        *
382        * @return The absolute path of this file
383        */
384      public String getAbsolutePath()
385      {
386        if (isAbsolute ())
387          return path;
388        
389        String dir = System.getProperty ("user.dir");
390        if (dir == null)
391          return path;
392    
393  /*************************************************************************/      if (PlatformHelper.endWithSeparator (dir))
394          return dir + path;
395    
396  /**      return dir + separator + path;
397    * Returns the path name that represents this file.  May be a relative    }
   * or an absolute path name  
   *  
   * @return The pathname of this file  
   */  
 public String  
 getPath()  
 {  
   return(path);  
 }  
398    
399  /*************************************************************************/    /*************************************************************************/
400    
401  /**    /**
402    * This method returns the path of this file as an absolute path name.      * This method returns a <code>File</code> object representing the
403    * If the path name is already absolute, then it is returned.  Otherwise      * absolute path of this object.
404    * the value returned is the current directory plus the separatory      *
405    * string plus the path of the file.  The current directory is determined      * @return A <code>File</code> with the absolute path of the object.
406    * from the <code>user.dir</code> system property.      */
407    *    public File getAbsoluteFile()
408    * @return The absolute path of this file    {
409    */      return(new File(getAbsolutePath()));
410  public String    }
 getAbsolutePath()  
 {  
   if (isAbsolute ())  
     return path;  
     
   String dir = System.getProperty ("user.dir");  
   if (dir == null)  
     return path;  
411    
412    if (PlatformHelper.endWithSeparator (dir))    /*************************************************************************/
     return dir + path;  
413    
414    return dir + separator + path;    /**
415  }      * This method returns a canonical representation of the pathname of
416        * this file.  The actual form of the canonical representation is
417        * different.  On the GNU system, the canonical form differs from the
418        * absolute form in that all relative file references to "." and ".."
419        * are resolved and removed.
420        * <p>
421        * Note that this method, unlike the other methods which return path
422        * names, can throw an IOException.  This is because native method
423        * might be required in order to resolve the canonical path
424        *
425        * @exception IOException If an error occurs
426        */
427      public String getCanonicalPath() throws IOException
428      {
429        String abspath = getAbsolutePath();
430        return PlatformHelper.toCanonicalForm(abspath);
431      }
432    
433  /*************************************************************************/    /*************************************************************************/
434    
435  /**    /**
436    * This method returns a <code>File</code> object representing the      * This method returns a <code>File</code> object representing the
437    * absolute path of this object.      * canonical path of this object.
438    *      *
439    * @return A <code>File</code> with the absolute path of the object.      * @return A <code>File</code> instance representing the canonical path of
440    */      * this object.
441  public File      *
442  getAbsoluteFile()      * @exception IOException If an error occurs.
443  {      */
444    return(new File(getAbsolutePath()));    public File getCanonicalFile() throws IOException
445  }    {
446        return(new File(getCanonicalPath()));
447      }
448    
449  /*************************************************************************/    /*************************************************************************/
450    
451  /**    /**
452    * This method returns a canonical representation of the pathname of      * This method returns a <code>String</code> the represents this file's
453    * this file.  The actual form of the canonical representation is      * parent.  <code>null</code> is returned if the file has no parent.  The
454    * different.  On the GNU system, the canonical form differs from the      * parent is determined via a simple operation which removes the
455    * absolute form in that all relative file references to "." and ".."      *
456    * are resolved and removed.      * @return The parent directory of this file
457    * <p>      */
458    * Note that this method, unlike the other methods which return path    public String getParent()
459    * names, can throw an IOException.  This is because native method    {
460    * might be required in order to resolve the canonical path      if (PlatformHelper.isRootDirectory(path))
461    *        return null;
   * @exception IOException If an error occurs  
   */  
 public String  
 getCanonicalPath() throws IOException  
 {  
   String abspath = getAbsolutePath();  
   return PlatformHelper.toCanonicalForm(abspath);  
 }  
462    
463  /*************************************************************************/      String par_path = path;
464    
465  /**      int pos = PlatformHelper.lastIndexOfSeparator(par_path);
466    * This method returns a <code>File</code> object representing the      if (pos == -1)
467    * canonical path of this object.        return null;
   *  
   * @return A <code>File</code> instance representing the canonical path of  
   * this object.  
   *  
   * @exception IOException If an error occurs.  
   */  
 public File  
 getCanonicalFile() throws IOException  
 {  
   return(new File(getCanonicalPath()));  
 }  
468    
469  /*************************************************************************/      return(par_path.substring(0, pos));
470      }
471    
472  /**    /*************************************************************************/
   * This method returns a <code>String</code> the represents this file's  
   * parent.  <code>null</code> is returned if the file has no parent.  The  
   * parent is determined via a simple operation which removes the  
   *  
   * @return The parent directory of this file  
   */  
 public String  
 getParent()  
 {  
   if (PlatformHelper.isRootDirectory(path))  
     return null;  
473    
474    String par_path = path;    /**
475        * This method returns a <code>File</code> object representing the parent
476        * file of this one.
477        *
478        * @param A <code>File</code> for the parent of this object.  
479        * <code>null</code>
480        * will be returned if this object does not have a parent.
481        */
482      public File getParentFile()
483      {
484        String parent = getParent();
485        if (parent == null)
486          return(null);
487    
488    int pos = PlatformHelper.lastIndexOfSeparator(par_path);      return(new File(parent));
489    if (pos == -1)    }
     return null;  
490    
491    return(par_path.substring(0, pos));    /*************************************************************************/
 }  
492    
493  /*************************************************************************/    /**
494        * This method returns true if this object represents an absolute file
495        * path and false if it does not.  The definition of an absolute path varies
496        * by system.  As an example, on GNU systems, a path is absolute if it starts
497        * with a "/".
498        *
499        * @return <code>true</code> if this object represents an absolute
500        * file name, <code>false</code> otherwise.
501        */
502      public boolean isAbsolute()
503      {
504        if (PlatformHelper.beginWithRootPathPrefix (path) > 0)
505          return(true);
506        else
507          return(false);
508      }
509    
510  /**    /*************************************************************************/
   * This method returns a <code>File</code> object representing the parent  
   * file of this one.  
   *  
   * @param A <code>File</code> for the parent of this object.  <code>null</code>  
   * will be returned if this object does not have a parent.  
   */  
 public File  
 getParentFile()  
 {  
   String parent = getParent();  
   if (parent == null)  
     return(null);  
511    
512    return(new File(parent));    /**
513  }      * This method tests whether or not the current thread is allowed to
514        * to read the file pointed to by this object.  This will be true if and
515        * and only if 1) the file exists and 2) the <code>SecurityManager</code>
516        * (if any) allows access to the file via it's <code>checkRead</code>
517        * method 3) the file is readable.
518        *
519        * @return <code>true</code> if reading is allowed,
520        * <code>false</code> otherwise
521        *
522        * @exception SecurityException If the <code>SecurityManager</code>
523        * does not allow access to the file
524        */
525      public boolean canRead() throws SecurityException
526      {
527        // Test for existence. This also does the SecurityManager check
528        if (!exists())
529          return(false);
530    
531  /*************************************************************************/      return(canReadInternal(path));
532      }
533    
534  /**    /*************************************************************************/
   * This method returns true if this object represents an absolute file  
   * path and false if it does not.  The definition of an absolute path varies  
   * by system.  As an example, on GNU systems, a path is absolute if it starts  
   * with a "/".  
   *  
   * @return <code>true</code> if this object represents an absolute file name, <code>false</code> otherwise.  
   */  
 public boolean  
 isAbsolute()  
 {  
   if (PlatformHelper.beginWithRootPathPrefix (path) > 0)  
     return(true);  
   else  
     return(false);  
 }  
535    
536  /*************************************************************************/    /**
537        * This native method checks file permissions for reading
538        */
539      private synchronized native boolean canReadInternal(String path);
540    
541  /**    /*************************************************************************/
   * This method tests whether or not the current thread is allowed to  
   * to read the file pointed to by this object.  This will be true if and  
   * and only if 1) the file exists and 2) the <code>SecurityManager</code>  
   * (if any) allows access to the file via it's <code>checkRead</code>  
   * method 3) the file is readable.  
   *  
   * @return <code>true</code> if reading is allowed, <code>false</code> otherwise  
   *  
   * @exception SecurityException If the <code>SecurityManager</code> does not allow access to the file  
   */  
 public boolean  
 canRead() throws SecurityException  
 {  
   // Test for existence. This also does the SecurityManager check  
   if (!exists())  
     return(false);  
542    
543    return(canReadInternal(path));    /**
544  }      * This method test whether or not the current thread is allowed to
545        * write to this object.  This will be true if and only if 1) The
546        * <code>SecurityManager</code> (if any) allows write access to the
547        * file and 2) The file exists and 3) The file is writable.  To determine
548        * whether or not a non-existent file can be created, check the parent
549        * directory for write access.
550        *
551        * @return <code>true</code> if writing is allowed, <code>false</code>
552        * otherwise
553        *
554        * @exception SecurityException If the <code>SecurityManager</code>
555        * does not allow access to the file
556        */
557      public boolean canWrite() throws SecurityException
558      {
559        // We still need to do a SecurityCheck since exists() only checks
560        // for read access
561        SecurityManager sm = System.getSecurityManager();
562        if (sm != null)
563          sm.checkWrite(path);
564        
565        // Test for existence.  This is required by the spec
566        if (!exists())
567          return(false);
568    
569        if (!isDirectory())
570          return(canWriteInternal(path));
571        else
572          try
573            {
574              File test = createTempFile("test-dir-write", null, this);
575              return (test != null && test.delete());
576            }
577          catch (IOException ioe)
578            {
579              return(false);
580            }
581      }
582    
583  /*************************************************************************/    /*************************************************************************/
584    
585  /**    /**
586    * This native method checks file permissions for reading      * This native method checks file permissions for writing
587    */      */
588  private synchronized native boolean canReadInternal(String path);    private synchronized native boolean canWriteInternal(String path);
589    
590  /*************************************************************************/    /*************************************************************************/
591    
592  /**    /**
593    * This method test whether or not the current thread is allowed to      * This method sets the file represented by this object to be read only.
594    * write to this object.  This will be true if and only if 1) The      * A read only file or directory cannot be modified.  Please note that
595    * <code>SecurityManager</code> (if any) allows write access to the      * GNU systems allow read only files to be deleted if the directory it
596    * file and 2) The file exists and 3) The file is writable.  To determine      * is contained in is writable.
597    * whether or not a non-existent file can be created, check the parent      *
598    * directory for write access.      * @return <code>true</code> if the operation succeeded, <code>false</code>
599    *      * otherwise.
600    * @return <code>true</code> if writing is allowed, <code>false</code> otherwise      *
601    *      * @exception SecurityException If the <code>SecurityManager</code> does
602    * @exception SecurityException If the <code>SecurityManager</code> does not allow access to the file      * not allow this operation.
603    */      */
604  public boolean    public boolean setReadOnly() throws SecurityException
605  canWrite() throws SecurityException    {
606  {      // Test for existence.
607    // We still need to do a SecurityCheck since exists() only checks      if (!exists())
608    // for read access        return(false);
609    SecurityManager sm = System.getSecurityManager();  
610    if (sm != null)      // We still need to do a SecurityCheck since exists() only checks
611      sm.checkWrite(path);      // for read access
612          SecurityManager sm = System.getSecurityManager();
613    // Test for existence.  This is required by the spec      if (sm != null)
614    if (!exists())        sm.checkWrite(path);
615      return(false);      
616        return(setReadOnlyInternal(path));
617    if (!isDirectory())    }
     return(canWriteInternal(path));  
   else  
     try  
       {  
         File test = createTempFile("test-dir-write", null, this);  
         return (test != null && test.delete());  
       }  
     catch (IOException ioe)  
       {  
         return(false);  
       }  
 }  
618    
619  /*************************************************************************/    /*************************************************************************/
620    
621  /**    /*
622    * This native method checks file permissions for writing     * This native method sets the permissions to make the file read only.
623    */     */
624  private synchronized native boolean canWriteInternal(String path);    private native boolean setReadOnlyInternal(String path);
625    
626  /*************************************************************************/    /*************************************************************************/
627    
628  /**    /**
629    * This method sets the file represented by this object to be read only.      * This method tests whether or not the file represented by the object
630    * A read only file or directory cannot be modified.  Please note that      * actually exists on the filesystem.
631    * GNU systems allow read only files to be deleted if the directory it      *
632    * is contained in is writable.      * @return <code>true</code> if the file exists, <code>false</code>otherwise.
633    *      *
634    * @return <code>true</code> if the operation succeeded, <code>false</code>      * @exception SecurityException If reading of the file is not permitted
635    * otherwise.      */
636    *    public boolean exists() throws SecurityException
637    * @exception SecurityException If the <code>SecurityManager</code> does    {
638    * not allow this operation.      // Check the SecurityManager
639    */      SecurityManager sm = System.getSecurityManager();
640  public boolean      if (sm != null)
641  setReadOnly() throws SecurityException        sm.checkRead(path);
642  {      
643    // Test for existence.      return(existsInternal(path));
644    if (!exists())    }
     return(false);  
   
   // We still need to do a SecurityCheck since exists() only checks  
   // for read access  
   SecurityManager sm = System.getSecurityManager();  
   if (sm != null)  
     sm.checkWrite(path);  
     
   return(setReadOnlyInternal(path));  
 }  
   
 /*************************************************************************/  
   
 /*  
  * This native method sets the permissions to make the file read only.  
  */  
 private native boolean setReadOnlyInternal(String path);  
645    
646  /*************************************************************************/    /*************************************************************************/
647    
648  /**    /**
649    * This method tests whether or not the file represented by the object      * This native method does the actual checking of file existence.
650    * actually exists on the filesystem.      */
651    *    private native boolean existsInternal(String path);
   * @return <code>true</code> if the file exists, <code>false</code>otherwise.  
   *  
   * @exception SecurityException If reading of the file is not permitted  
   */  
 public boolean  
 exists() throws SecurityException  
 {  
   // Check the SecurityManager  
   SecurityManager sm = System.getSecurityManager();  
   if (sm != null)  
     sm.checkRead(path);  
     
   return(existsInternal(path));  
 }  
652    
653  /*************************************************************************/    /*************************************************************************/
654    
655  /**    /**
656    * This native method does the actual checking of file existence.      * This method tests whether or not the file represented by this object
657    */      * is a "plain" file.  A file is a plain file if and only if it 1) Exists,
658  private native boolean existsInternal(String path);      * 2) Is not a directory or other type of special file.
659        *
660        * @return <code>true</code> if this is a plain file, <code>false</code>
661        * otherwise
662        *
663        * @exception SecurityException If reading of the file is not permitted
664        */
665      public boolean isFile() throws SecurityException
666      {
667        // Check the SecurityManager
668        SecurityManager sm = System.getSecurityManager();
669        if (sm != null)
670          sm.checkRead(path);
671    
672  /*************************************************************************/      return(isFileInternal(path));
673      }
674    
675  /**    /*************************************************************************/
   * This method tests whether or not the file represented by this object  
   * is a "plain" file.  A file is a plain file if and only if it 1) Exists,  
   * 2) Is not a directory or other type of special file.  
   *  
   * @return <code>true</code> if this is a plain file, <code>false</code> otherwise  
   *  
   * @exception SecurityException If reading of the file is not permitted  
   */  
 public boolean isFile() throws SecurityException  
 {  
   // Check the SecurityManager  
   SecurityManager sm = System.getSecurityManager();  
   if (sm != null)  
     sm.checkRead(path);  
676    
677    return(isFileInternal(path));    /**
678  }      * This native method does the actual check of whether or not a file
679        * is a plain file or not.  It also handles the existence check to
680        * eliminate the overhead of a call to exists()
681        */
682      private native boolean isFileInternal(String path);
683    
684  /*************************************************************************/    /*************************************************************************/
685    
686  /**    /**
687    * This native method does the actual check of whether or not a file      * This method tests whether or not the file represented by this object
688    * is a plain file or not.  It also handles the existence check to      * is a directory.  In order for this method to return <code>true</code>,
689    * eliminate the overhead of a call to exists()      * the file represented by this object must exist and be a directory.
690    */      *
691  private native boolean isFileInternal(String path);      * @return <code>true</code> if this file is a directory, <code>false</code>
692        * otherwise
693        *
694        * @exception SecurityException If reading of the file is not permitted
695        */
696      public boolean isDirectory() throws SecurityException
697      {
698        // Check the SecurityManager
699        SecurityManager sm = System.getSecurityManager();
700        if (sm != null)
701          sm.checkRead(path);
702    
703  /*************************************************************************/      return(isDirectoryInternal(path));
704      }
705    
706  /**    /*************************************************************************/
   * This method tests whether or not the file represented by this object  
   * is a directory.  In order for this method to return <code>true</code>,  
   * the file represented by this object must exist and be a directory.  
   *  
   * @return <code>true</code> if this file is a directory, <code>false</code> otherwise  
   *  
   * @exception SecurityException If reading of the file is not permitted  
   */  
 public boolean isDirectory() throws SecurityException  
 {  
   // Check the SecurityManager  
   SecurityManager sm = System.getSecurityManager();  
   if (sm != null)  
     sm.checkRead(path);  
707    
708    return(isDirectoryInternal(path));    /**
709  }      * This method does the actual check of whether or not a file is a
710        * directory or not.  It also handle the existence check to eliminate
711        * the overhead of a call to exists()
712        */
713      private native boolean isDirectoryInternal(String path);
714    
715  /*************************************************************************/    /*************************************************************************/
716    
717  /**    /**
718    * This method does the actual check of whether or not a file is a      * This method tests whether or not this file represents a "hidden" file.
719    * directory or not.  It also handle the existence check to eliminate      * On GNU systems, a file is hidden if its name begins with a "."
720    * the overhead of a call to exists()      * character.  Files with these names are traditionally not shown with
721    */      * directory listing tools.
722  private native boolean isDirectoryInternal(String path);      *
723        * @return <code>true</code> if the file is hidden, <code>false</code>
724        * otherwise.
725        */
726      public boolean isHidden()
727      {
728        if (getName().startsWith("."))
729          return(true);
730        else
731          return(false);
732      }
733    
734  /*************************************************************************/    /*************************************************************************/
735    
736  /**    /**
737    * This method tests whether or not this file represents a "hidden" file.      * This method returns the length of the file represented by this object,
738    * On GNU systems, a file is hidden if its name begins with a "."      * or 0 if the specified file does not exist.
739    * character.  Files with these names are traditionally not shown with      *
740    * directory listing tools.      * @return The length of the file
741    *      *
742    * @return <code>true</code> if the file is hidden, <code>false</code>      * @exception SecurityException If reading of the file is not permitted
743    * otherwise.      */
744    */    public long length() throws SecurityException
745  public boolean    {
746  isHidden()      // Check the SecurityManager
747  {      SecurityManager sm = System.getSecurityManager();
748    if (getName().startsWith("."))      if (sm != null)
749      return(true);        sm.checkRead(path);
   else  
     return(false);  
 }  
750    
751  /*************************************************************************/      return(lengthInternal(path));
752      }
753    
754  /**    /*************************************************************************/
   * This method returns the length of the file represented by this object,  
   * or 0 if the specified file does not exist.  
   *  
   * @return The length of the file  
   *  
   * @exception SecurityException If reading of the file is not permitted  
   */  
 public long  
 length() throws SecurityException  
 {  
   // Check the SecurityManager  
   SecurityManager sm = System.getSecurityManager();  
   if (sm != null)  
     sm.checkRead(path);  
755    
756    return(lengthInternal(path));    /**
757  }      * This native method actually determines the length of the file and
758        * handles the existence check
759        */
760      private native long lengthInternal(String path);
761    
762  /*************************************************************************/    /*************************************************************************/
763    
764  /**    /**
765    * This native method actually determines the length of the file and      * This method returns the last modification time of this file.  The
766    * handles the existence check      * time value returned is an abstract value that should not be interpreted
767    */      * as a specified time value.  It is only useful for comparing to other
768  private native long lengthInternal(String path);      * such time values returned on the same system.  In that case, the larger
769        * value indicates a more recent modification time.
770        * <p>
771        * If the file does not exist, then a value of 0 is returned.
772        *
773        * @return The last modification time of the file
774        *
775        * @exception SecurityException If reading of the file is not permitted
776        */
777      public long lastModified() throws SecurityException
778      {
779        // Check the SecurityManager
780        SecurityManager sm = System.getSecurityManager();
781        if (sm != null)
782          sm.checkRead(path);
783    
784  /*************************************************************************/      return(lastModifiedInternal(path));
785      }
786    
787  /**    /*************************************************************************/
   * This method returns the last modification time of this file.  The  
   * time value returned is an abstract value that should not be interpreted  
   * as a specified time value.  It is only useful for comparing to other  
   * such time values returned on the same system.  In that case, the larger  
   * value indicates a more recent modification time.  
   * <p>  
   * If the file does not exist, then a value of 0 is returned.  
   *  
   * @return The last modification time of the file  
   *  
   * @exception SecurityException If reading of the file is not permitted  
   */  
 public long  
 lastModified() throws SecurityException  
 {  
   // Check the SecurityManager  
   SecurityManager sm = System.getSecurityManager();  
   if (sm != null)  
     sm.checkRead(path);  
788    
789    return(lastModifiedInternal(path));    /**
790  }      * This native method does the actual work of getting the last file
791        * modification time.  It also does the existence check to avoid the
792        * overhead of a call to exists()
793        */
794      private native long lastModifiedInternal(String path);
795    
796  /*************************************************************************/    /*************************************************************************/
797    
798  /**    /**
799    * This native method does the actual work of getting the last file      * This method sets the modification time on the file to the specified
800    * modification time.  It also does the existence check to avoid the      * value.  This is specified as the number of seconds since midnight
801    * overhead of a call to exists()      * on January 1, 1970 GMT.
802    */      *
803  private native long lastModifiedInternal(String path);      * @param time The desired modification time.
804        *
805        * @return <code>true</code> if the operation succeeded, <code>false</code>
806        * otherwise.
807        *
808        * @exception IllegalArgumentException If the specified time is negative.
809        * @exception SecurityException If the <code>SecurityManager</code> will
810        * not allow this operation.
811        */
812      public boolean setLastModified(long time)
813        throws IllegalArgumentException, SecurityException
814      {
815        if (time < 0)
816          throw new IllegalArgumentException("Negative modification time: " + time);
817    
818  /*************************************************************************/      // Check the SecurityManager
819        SecurityManager sm = System.getSecurityManager();
820        if (sm != null)
821          sm.checkWrite(path);
822        
823        return(setLastModifiedInternal(path, time));
824      }
825    
826  /**    /*************************************************************************/
   * This method sets the modification time on the file to the specified  
   * value.  This is specified as the number of seconds since midnight  
   * on January 1, 1970 GMT.  
   *  
   * @param time The desired modification time.  
   *  
   * @return <code>true</code> if the operation succeeded, <code>false</code>  
   * otherwise.  
   *  
   * @exception IllegalArgumentException If the specified time is negative.  
   * @exception SecurityException If the <code>SecurityManager</code> will  
   * not allow this operation.  
   */  
 public boolean  
 setLastModified(long time) throws IllegalArgumentException, SecurityException  
 {  
   if (time < 0)  
     throw new IllegalArgumentException("Negative modification time: " + time);  
827    
828    // Check the SecurityManager    /*
829    SecurityManager sm = System.getSecurityManager();     * This method does the actual setting of the modification time.
830    if (sm != null)     */
831      sm.checkWrite(path);    private native boolean setLastModifiedInternal(String path, long time);
     
   return(setLastModifiedInternal(path, time));  
 }  
   
 /*************************************************************************/  
   
 /*  
  * This method does the actual setting of the modification time.  
  */  
 private native boolean setLastModifiedInternal(String path, long time);  
832    
833  /*************************************************************************/    /*************************************************************************/
834    
835  /**    /**
836    * This method creates a new file of zero length with the same name as      * This method creates a new file of zero length with the same name as
837    * the path of this <code>File</code> object if an only if that file      * the path of this <code>File</code> object if an only if that file
838    * does not already exist.      * does not already exist.
839    * <p>      * <p>
840    * A <code>SecurityManager</code>checkWrite</code> check is done prior      * A <code>SecurityManager</code>checkWrite</code> check is done prior
841    * to performing this action.      * to performing this action.
842    *      *
843    * @return <code>true</code> if the file was created, <code>false</code> if      * @return <code>true</code> if the file was created, <code>false</code> if
844    * the file alread existed.      * the file alread existed.
845    *      *
846    * @exception IOException If an I/O error occurs      * @exception IOException If an I/O error occurs
847    * @exception SecurityException If the <code>SecurityManager</code> will      * @exception SecurityException If the <code>SecurityManager</code> will
848    * not allow this operation to be performed.      * not allow this operation to be performed.
849    */      */
850  public boolean    public boolean createNewFile() throws IOException, SecurityException
851  createNewFile() throws IOException, SecurityException    {
852  {      SecurityManager sm = System.getSecurityManager();
853    SecurityManager sm = System.getSecurityManager();      if (sm != null)
854    if (sm != null)        sm.checkWrite(path);
855      sm.checkWrite(path);      
856          return(createInternal(getPath()));
857    return(createInternal(getPath()));    }
 }  
858    
859  /*************************************************************************/    /*************************************************************************/
860    
861  /**    /**
862    * This method deletes the file represented by this object.  If this file      * This method deletes the file represented by this object.  If this file
863    * is a directory, it must be empty in order for the delete to succeed.      * is a directory, it must be empty in order for the delete to succeed.
864    *      *
865    * @return <code>true</code> if the file was deleted, <code>false</code> otherwise      * @return <code>true</code> if the file was deleted, <code>false</code>
866    *      * otherwise
867    * @exception SecurityException If deleting of the file is not allowed      *
868    */      * @exception SecurityException If deleting of the file is not allowed
869  public synchronized boolean      */
870  delete() throws SecurityException    public synchronized boolean delete() throws SecurityException
871  {    {
872    // Check the SecurityManager      // Check the SecurityManager
873    SecurityManager sm = System.getSecurityManager();      SecurityManager sm = System.getSecurityManager();
874    if (sm != null)      if (sm != null)
875      sm.checkDelete(path);        sm.checkDelete(path);
876    
877    return(deleteInternal(path));      return(deleteInternal(path));
878  }    }
879    
880  /*************************************************************************/    /*************************************************************************/
881    
882  /**    /**
883    * This native method handles the actual deleting of the file      * This native method handles the actual deleting of the file
884    */      */
885  private native boolean deleteInternal(String path);    private native boolean deleteInternal(String path);
886    
887  /*************************************************************************/    /*************************************************************************/
888    
889  /**    /**
890    * Calling this method requests that the file represented by this object      * Calling this method requests that the file represented by this object
891    * be deleted when the virtual machine exits.  Note that this request cannot      * be deleted when the virtual machine exits.  Note that this request cannot
892    * be cancelled.  Also, it will only be carried out if the virtual machine      * be cancelled.  Also, it will only be carried out if the virtual machine
893    * exits normally.      * exits normally.
894    *      *
895    * @exception SecurityException If deleting of the file is not allowed      * @exception SecurityException If deleting of the file is not allowed
896    */      */
897  public void    public void deleteOnExit() throws SecurityException
898  deleteOnExit() throws SecurityException    {
899  {      // Check the SecurityManager
900    // Check the SecurityManager      SecurityManager sm = System.getSecurityManager();
901    SecurityManager sm = System.getSecurityManager();      if (sm != null)
902    if (sm != null)        sm.checkDelete(path);
903      sm.checkDelete(path);  
904        // Sounds like we need to do some VM specific stuff here. We could delete
905    // Sounds like we need to do some VM specific stuff here. We could delete      // the file in finalize() and set FinalizeOnExit to true, but delete on
906    // the file in finalize() and set FinalizeOnExit to true, but delete on      // finalize != delete on exit and we should not be setting up system
907    // finalize != delete on exit and we should not be setting up system      // parameters without the user's knowledge.
908    // parameters without the user's knowledge.      // FIXME: ********IMPLEMENT ME!!!!!!***************
909    //********IMPLEMENT ME!!!!!!***************      return;
910    return;    }
 }  
911    
912  /*************************************************************************/    /*************************************************************************/
913    
914  /**    /**
915    * This method creates a directory for the path represented by this object.      * This method creates a directory for the path represented by this object.
916    *      *
917    * @return <code>true</code> if the directory was created, <code>false</code> otherwise      * @return <code>true</code> if the directory was created,
918    *      * <code>false</code> otherwise
919    * @exception SecurityException If write access is not allowed to this file      *
920    */      * @exception SecurityException If write access is not allowed to this file
921  public boolean      */
922  mkdir() throws SecurityException    public boolean mkdir() throws SecurityException
923  {    {
924    // Check the SecurityManager      // Check the SecurityManager
925    SecurityManager sm = System.getSecurityManager();      SecurityManager sm = System.getSecurityManager();
926    if (sm != null)      if (sm != null)
927      sm.checkWrite(path);        sm.checkWrite(path);
   
   String mk_path;  
   mk_path = PlatformHelper.removeTailSeparator(path);  
     
   return(mkdirInternal(mk_path));  
 }  
928    
929  /*************************************************************************/      String mk_path;
930        mk_path = PlatformHelper.removeTailSeparator(path);
931        
932        return(mkdirInternal(mk_path));
933      }
934    
935  /**    /*************************************************************************/
   * This native method actually creates the directory  
   */  
 private native boolean mkdirInternal(String path);  
936    
937  /*************************************************************************/    /**
938        * This native method actually creates the directory
939        */
940      private native boolean mkdirInternal(String path);
941    
942  /**    /*************************************************************************/
   * This method creates a directory for the path represented by this file.  
   * It will also create any intervening parent directories if necessary.  
   *  
   * @return <code>true</code> if the directory was created, <code>false</code> otherwise  
   *  
   * @exception SecurityException If write access is not allowed to this file  
   */  
 public boolean  
 mkdirs() throws SecurityException  
 {  
   String parent = getParent();  
   if (parent == null)  
     {  
       return(mkdir());  
     }  
       
   File f = new File(parent);  
   if (!f.exists())  
     {  
       boolean rc = f.mkdirs();  
       if (rc == false)  
         return(false);  
     }  
943    
944    return(mkdir());    /**
945  }      * This method creates a directory for the path represented by this file.
946        * It will also create any intervening parent directories if necessary.
947        *
948        * @return <code>true</code> if the directory was created,
949        * <code>false</code> otherwise
950        *
951        * @exception SecurityException If write access is not allowed to this file
952        */
953      public boolean mkdirs() throws SecurityException
954      {
955        String parent = getParent();
956        if (parent == null)
957          {
958            return(mkdir());
959          }
960          
961        File f = new File(parent);
962        if (!f.exists())
963          {
964            boolean rc = f.mkdirs();
965            if (rc == false)
966              return(false);
967          }
968    
969  /*************************************************************************/      return(mkdir());
970      }
971    
972  /**    /*************************************************************************/
   * This method renames the file represented by this object to the path  
   * of the file represented by the argument <code>File</code>.  
   *  
   * @param dest The <code>File</code> object representing the target name  
   *  
   * @return <code>true</code> if the rename succeeds, <code>false</code> otherwise.  
   *  
   * @exception SecurityException If write access is not allowed to the file by the <code>SecurityMananger</code>.  
   */  
 public synchronized boolean  
 renameTo(File dest) throws SecurityException  
 {  
   // Check the SecurityManager  
   SecurityManager sm = System.getSecurityManager();  
   if (sm != null)  
     sm.checkWrite(path);  
973    
974    // Call our native rename method    /**
975    boolean rc = renameToInternal(path, dest.getPath());      * This method renames the file represented by this object to the path
976        * of the file represented by the argument <code>File</code>.
977        *
978        * @param dest The <code>File</code> object representing the target name
979        *
980        * @return <code>true</code> if the rename succeeds, <code>false</code>
981        * otherwise.
982        *
983        * @exception SecurityException If write access is not allowed to the
984        * file by the <code>SecurityMananger</code>.
985        */
986      public synchronized boolean renameTo(File dest) throws SecurityException
987      {
988        // Check the SecurityManager
989        SecurityManager sm = System.getSecurityManager();
990        if (sm != null)
991          sm.checkWrite(path);
992    
993    return(rc);      // Call our native rename method
994  }      boolean rc = renameToInternal(path, dest.getPath());
995    
996  /*************************************************************************/      return(rc);
997      }
998    
999  /**    /*************************************************************************/
   * This native method actually performs the rename.  
   */  
 private native boolean  
 renameToInternal(String target, String dest);  
1000    
1001  /*************************************************************************/    /**
1002        * This native method actually performs the rename.
1003        */
1004      private native boolean renameToInternal(String target, String dest);
1005    
1006  /**    /*************************************************************************/
   * This method returns a array of <code>String</code>'s representing the  
   * list of files is then directory represented by this object.  If this  
   * object represents a non-directory file or a non-existent file, then  
   * <code>null</code> is returned.  The list of files will not contain  
   * any names such as "." or ".." which indicate the current or parent  
   * directory.  Also, the names are not guaranteed to be sorted.  
   * <p>  
   * A <code>SecurityManager</code> check is made prior to reading the  
   * directory.  If read access to the directory is denied, an exception  
   * will be thrown.  
   *  
   * @return An array of files in the directory, or <code>null</code> if this object does not represent a valid directory.  
   *  
   * @exception SecurityException If read access is not allowed to the directory by the <code>SecurityManager</code>  
   */  
 public String[]  
 list()  
 {  
   return(list(null));  
 }  
1007    
1008  /*************************************************************************/    /**
1009        * This method returns a array of <code>String</code>'s representing the
1010        * list of files is then directory represented by this object.  If this
1011        * object represents a non-directory file or a non-existent file, then
1012        * <code>null</code> is returned.  The list of files will not contain
1013        * any names such as "." or ".." which indicate the current or parent
1014        * directory.  Also, the names are not guaranteed to be sorted.
1015        * <p>
1016        * A <code>SecurityManager</code> check is made prior to reading the
1017        * directory.  If read access to the directory is denied, an exception
1018        * will be thrown.
1019        *
1020        * @return An array of files in the directory, or <code>null</code> if
1021        * this object does not represent a valid directory.
1022        *
1023        * @exception SecurityException If read access is not allowed to the
1024        * directory by the <code>SecurityManager</code>
1025        */
1026      public String[] list()
1027      {
1028        return(list(null));
1029      }
1030    
1031  /**    /*************************************************************************/
   * This method returns a array of <code>String</code>'s representing the  
   * list of files is then directory represented by this object.  If this  
   * object represents a non-directory file or a non-existent file, then  
   * <code>null</code> is returned.  The list of files will not contain  
   * any names such as "." or ".." which indicate the current or parent  
   * directory.  Also, the names are not guaranteed to be sorted.  
   * <p>  
   * In this form of the <code>list()</code> method, a filter is specified  
   * that allows the caller to control which files are returned in the  
   * list.  The <code>FilenameFilter</code> specified is called for each  
   * file returned to determine whether or not that file should be included  
   * in the list.  
   * <p>  
   * A <code>SecurityManager</code> check is made prior to reading the  
   * directory.  If read access to the directory is denied, an exception  
   * will be thrown.  
   *  
   * @param filter An object which will identify files to exclude from the directory listing.  
   *  
   * @return An array of files in the directory, or <code>null</code> if this object does not represent a valid directory.  
   *  
   * @exception SecurityException If read access is not allowed to the directory by the <code>SecurityManager</code>  
   */  
 public String[]  
 list(FilenameFilter filter)  
 {  
   // Check the SecurityManager  
   SecurityManager sm = System.getSecurityManager();  
   if (sm != null)  
     sm.checkRead(path);  
   
   // Get the list of files  
   String list_path = PlatformHelper.removeTailSeparator(path);  
   File dir = new File(list_path);  
   
   if (! dir.exists() || ! dir.isDirectory() ) return null;  
     
   String files[] = listInternal(list_path);  
     
   if (files == null)  
     return new String[0];  
   if (filter == null)  
     return(files);  
     
   // Apply the filter  
   int count = 0;  
   for (int i = 0; i < files.length; i++)  
     {  
       if (filter.accept(this, files[i]))  
         ++count;  
       else  
         files[i] = null;  
     }  
1032    
1033    String[] retfiles = new String[count];    /**
1034    count = 0;      * This method returns a array of <code>String</code>'s representing the
1035    for (int i = 0; i < files.length; i++)      * list of files is then directory represented by this object.  If this
1036      if (files[i] != null)      * object represents a non-directory file or a non-existent file, then
1037        retfiles[count++] = files[i];      * <code>null</code> is returned.  The list of files will not contain
1038        * any names such as "." or ".." which indicate the current or parent
1039        * directory.  Also, the names are not guaranteed to be sorted.
1040        * <p>
1041        * In this form of the <code>list()</code> method, a filter is specified
1042        * that allows the caller to control which files are returned in the
1043        * list.  The <code>FilenameFilter</code> specified is called for each
1044        * file returned to determine whether or not that file should be included
1045        * in the list.
1046        * <p>
1047        * A <code>SecurityManager</code> check is made prior to reading the
1048        * directory.  If read access to the directory is denied, an exception
1049        * will be thrown.
1050        *
1051        * @param filter An object which will identify files to exclude from
1052        * the directory listing.
1053        *
1054        * @return An array of files in the directory, or <code>null</code>
1055        * if this object does not represent a valid directory.
1056        *
1057        * @exception SecurityException If read access is not allowed to the
1058        * directory by the <code>SecurityManager</code>
1059        */
1060      public String[] list(FilenameFilter filter)
1061      {
1062        // Check the SecurityManager
1063        SecurityManager sm = System.getSecurityManager();
1064        if (sm != null)
1065          sm.checkRead(path);
1066    
1067        // Get the list of files
1068        String list_path = PlatformHelper.removeTailSeparator(path);
1069        File dir = new File(list_path);
1070    
1071    return(retfiles);      if (! dir.exists() || ! dir.isDirectory() ) return null;
1072  }      
1073        String files[] = listInternal(list_path);
1074        
1075        if (files == null)
1076          return new String[0];
1077        if (filter == null)
1078          return(files);
1079        
1080        // Apply the filter
1081        int count = 0;
1082        for (int i = 0; i < files.length; i++)
1083          {
1084            if (filter.accept(this, files[i]))
1085              ++count;
1086            else
1087              files[i] = null;
1088          }
1089    
1090  /*************************************************************************/      String[] retfiles = new String[count];
1091        count = 0;
1092        for (int i = 0; i < files.length; i++)
1093          if (files[i] != null)
1094            retfiles[count++] = files[i];
1095    
1096  /**      return(retfiles);
1097    * This native function actually produces the list of file in this    }
   * directory  
   */  
 private native String[] listInternal(String dirname);  
1098    
1099  /*************************************************************************/    /*************************************************************************/
1100    
1101  /**    /**
1102    * This method returns an array of <code>File</code> objects representing      * This native function actually produces the list of file in this
1103    * all the files in the directory represented by this object. If this      * directory
1104    * object does not represent a directory, <code>null</code> is returned.      */
1105    * Each of the returned <code>File</code> object is constructed with this    private native String[] listInternal(String dirname);
   * object as its parent.  
   * <p>  
   * A <code>SecurityManager</code> check is made prior to reading the  
   * directory.  If read access to the directory is denied, an exception  
   * will be thrown.  
   *  
   * @return An array of <code>File</code> objects for this directory.  
   *  
   * @exception SecurityException If the <code>SecurityManager</code> denies  
   * access to this directory.  
   */  
 public File[]  
 listFiles()  
 {  
   return(listFiles((FilenameFilter)null));  
 }  
1106    
1107  /*************************************************************************/    /*************************************************************************/
1108    
1109  /**    /**
1110    * This method returns an array of <code>File</code> objects representing      * This method returns an array of <code>File</code> objects representing
1111    * all the files in the directory represented by this object. If this      * all the files in the directory represented by this object. If this
1112    * object does not represent a directory, <code>null</code> is returned.      * object does not represent a directory, <code>null</code> is returned.
1113    * Each of the returned <code>File</code> object is constructed with this      * Each of the returned <code>File</code> object is constructed with this
1114    * object as its parent.      * object as its parent.
1115    * <p>      * <p>
1116    * In this form of the <code>listFiles()</code> method, a filter is specified      * A <code>SecurityManager</code> check is made prior to reading the
1117    * that allows the caller to control which files are returned in the      * directory.  If read access to the directory is denied, an exception
1118    * list.  The <code>FilenameFilter</code> specified is called for each      * will be thrown.
1119    * file returned to determine whether or not that file should be included      *
1120    * in the list.      * @return An array of <code>File</code> objects for this directory.
1121    * <p>      *
1122    * A <code>SecurityManager</code> check is made prior to reading the      * @exception SecurityException If the <code>SecurityManager</code> denies
1123    * directory.  If read access to the directory is denied, an exception      * access to this directory.
1124    * will be thrown.      */
1125    *    public File[] listFiles()
1126    * @return An array of <code>File</code> objects for this directory.    {
1127    *      return(listFiles((FilenameFilter)null));
1128    * @exception SecurityException If the <code>SecurityManager</code> denies    }
   * access to this directory.  
   */  
 public File[]  
 listFiles(FilenameFilter filter)  
 {  
   String[] filelist = list(filter);  
   if (filelist == null)  
     return(null);  
1129    
1130    File[] fobjlist = new File[filelist.length];    /*************************************************************************/
1131    
1132    for (int i = 0; i < filelist.length; i++)    /**
1133      fobjlist[i] = new File(this, filelist[i]);      * This method returns an array of <code>File</code> objects representing
1134        * all the files in the directory represented by this object. If this
1135        * object does not represent a directory, <code>null</code> is returned.
1136        * Each of the returned <code>File</code> object is constructed with this
1137        * object as its parent.
1138        * <p>
1139        * In this form of the <code>listFiles()</code> method, a filter is specified
1140        * that allows the caller to control which files are returned in the
1141        * list.  The <code>FilenameFilter</code> specified is called for each
1142        * file returned to determine whether or not that file should be included
1143        * in the list.
1144        * <p>
1145        * A <code>SecurityManager</code> check is made prior to reading the
1146        * directory.  If read access to the directory is denied, an exception
1147        * will be thrown.
1148        *
1149        * @return An array of <code>File</code> objects for this directory.
1150        *
1151        * @exception SecurityException If the <code>SecurityManager</code> denies
1152        * access to this directory.
1153        */
1154      public File[] listFiles(FilenameFilter filter)
1155      {
1156        String[] filelist = list(filter);
1157        if (filelist == null)
1158          return(null);
1159    
1160    return(fobjlist);      File[] fobjlist = new File[filelist.length];
 }  
1161    
1162  /*************************************************************************/      for (int i = 0; i < filelist.length; i++)
1163          fobjlist[i] = new File(this, filelist[i]);
1164    
1165  /**      return(fobjlist);
1166    * This method returns an array of <code>File</code> objects representing    }
   * all the files in the directory represented by this object. If this  
   * object does not represent a directory, <code>null</code> is returned.  
   * Each of the returned <code>File</code> object is constructed with this  
   * object as its parent.  
   * <p>  
   * In this form of the <code>listFiles()</code> method, a filter is specified  
   * that allows the caller to control which files are returned in the  
   * list.  The <code>FileFilter</code> specified is called for each  
   * file returned to determine whether or not that file should be included  
   * in the list.  
   * <p>  
   * A <code>SecurityManager</code> check is made prior to reading the  
   * directory.  If read access to the directory is denied, an exception  
   * will be thrown.  
   *  
   * @return An array of <code>File</code> objects for this directory.  
   *  
   * @exception SecurityException If the <code>SecurityManager</code> denies  
   * access to this directory.  
   */  
 public File[]  
 listFiles(FileFilter filter)  
 {  
   File[] fobjlist = listFiles((FilenameFilter)null);  
1167    
1168    if (fobjlist == null)    /*************************************************************************/
     return(null);  
1169    
1170    if (filter == null)    /**
1171      return(fobjlist);      * This method returns an array of <code>File</code> objects representing
1172        * all the files in the directory represented by this object. If this
1173        * object does not represent a directory, <code>null</code> is returned.
1174        * Each of the returned <code>File</code> object is constructed with this
1175        * object as its parent.
1176        * <p>
1177        * In this form of the <code>listFiles()</code> method, a filter is specified
1178        * that allows the caller to control which files are returned in the
1179        * list.  The <code>FileFilter</code> specified is called for each
1180        * file returned to determine whether or not that file should be included
1181        * in the list.
1182        * <p>
1183        * A <code>SecurityManager</code> check is made prior to reading the
1184        * directory.  If read access to the directory is denied, an exception
1185        * will be thrown.
1186        *
1187        * @return An array of <code>File</code> objects for this directory.
1188        *
1189        * @exception SecurityException If the <code>SecurityManager</code> denies
1190        * access to this directory.
1191        */
1192      public File[] listFiles(FileFilter filter)
1193      {
1194        File[] fobjlist = listFiles((FilenameFilter)null);
1195    
1196    int count = 0;      if (fobjlist == null)
1197    for (int i = 0; i < fobjlist.length; i++)        return(null);
1198      if (filter.accept(fobjlist[i]) == true)  
1199        ++count;      if (filter == null)
1200          return(fobjlist);
1201    File[] final_list = new File[count];  
1202    count = 0;      int count = 0;
1203    for (int i = 0; i < fobjlist.length; i++)      for (int i = 0; i < fobjlist.length; i++)
1204      if (filter.accept(fobjlist[i]) == true)        if (filter.accept(fobjlist[i]) == true)
       {  
         final_list[count] = fobjlist[i];  
1205          ++count;          ++count;
       }  
1206    
1207    return(final_list);      File[] final_list = new File[count];
1208  }      count = 0;
1209        for (int i = 0; i < fobjlist.length; i++)
1210          if (filter.accept(fobjlist[i]) == true)
1211            {
1212              final_list[count] = fobjlist[i];
1213              ++count;
1214            }
1215    
1216  /*************************************************************************/      return(final_list);
1217      }
1218    
1219  /**    /*************************************************************************/
   * This method compares the specified <code>Object</code> to this one  
   * to test for equality.  It does this by comparing the canonical path names  
   * of the files.  This method is identical to <code>compareTo(File)</code>  
   * except that if the <code>Object</code> passed to it is not a  
   * <code>File</code>, it throws a <code>ClassCastException</code>  
   * <p>  
   * The canonical paths of the files are determined by calling the  
   * <code>getCanonicalPath</code> method on each object.  
   * <p>  
   * This method returns a 0 if the specified <code>Object</code> is equal  
   * to this one, a negative value if it is less than this one  
   * a positive value if it is greater than this one.  
   *  
   * @return An integer as described above  
   *  
   * @exception ClassCastException If the passed <code>Object</code> is not a <code>File</code>  
   */  
 public int  
 compareTo(Object obj) throws ClassCastException  
 {  
   return(compareTo((File)obj));  
 }  
1220    
1221  /*************************************************************************/    /**
1222        * This method compares the specified <code>Object</code> to this one
1223        * to test for equality.  It does this by comparing the canonical path names
1224        * of the files.  This method is identical to <code>compareTo(File)</code>
1225        * except that if the <code>Object</code> passed to it is not a
1226        * <code>File</code>, it throws a <code>ClassCastException</code>
1227        * <p>
1228        * The canonical paths of the files are determined by calling the
1229        * <code>getCanonicalPath</code> method on each object.
1230        * <p>
1231        * This method returns a 0 if the specified <code>Object</code> is equal
1232        * to this one, a negative value if it is less than this one
1233        * a positive value if it is greater than this one.
1234        *
1235        * @return An integer as described above
1236        *
1237        * @exception ClassCastException If the passed <code>Object</code> is
1238        * not a <code>File</code>
1239        */
1240      public int compareTo(Object obj) throws ClassCastException
1241      {
1242        return(compareTo((File)obj));
1243      }
1244    
1245  /**    /*************************************************************************/
   * This method compares the specified <code>File</code> to this one  
   * to test for equality.  It does this by comparing the canonical path names  
   * of the files.  
   * <p>  
   * The canonical paths of the files are determined by calling the  
   * <code>getCanonicalPath</code> method on each object.  
   * <p>  
   * This method returns a 0 if the specified <code>Object</code> is equal  
   * to this one, a negative value if it is less than this one  
   * a positive value if it is greater than this one.  
   *  
   * @return An integer as described above  
   */  
 public int  
 compareTo(File file)  
 {  
   String p1, p2;  
   try  
     {    
       p1 = getCanonicalPath();  
       p2 = file.getCanonicalPath();  
     }  
   catch(IOException e)  
     {  
       // What do we do here?  The spec requires the canonical path.  Even  
       // if we don't call the method, we must replicate the functionality  
       // which per the spec can fail.  What happens in that situation?  
       // I just assume the files are equal!  
       //  
       return(0);  
     }  
1246    
1247    return(p1.compareTo(p2));    /**
1248  }      * This method compares the specified <code>File</code> to this one
1249        * to test for equality.  It does this by comparing the canonical path names
1250        * of the files.
1251        * <p>
1252        * The canonical paths of the files are determined by calling the
1253        * <code>getCanonicalPath</code> method on each object.
1254        * <p>
1255        * This method returns a 0 if the specified <code>Object</code> is equal
1256        * to this one, a negative value if it is less than this one
1257        * a positive value if it is greater than this one.
1258        *
1259        * @return An integer as described above
1260        */
1261      public int compareTo(File file)
1262      {
1263        String p1, p2;
1264        try
1265          {  
1266            p1 = getCanonicalPath();
1267            p2 = file.getCanonicalPath();
1268          }
1269        catch(IOException e)
1270          {
1271            // FIXME: What do we do here?  The spec requires the canonical path.
1272            // Even if we don't call the method, we must replicate the functionality
1273            // which per the spec can fail.  What happens in that situation?
1274            // I just assume the files are equal!
1275            //
1276            return(0);
1277          }
1278    
1279  /*************************************************************************/      return(p1.compareTo(p2));
1280      }
1281    
1282  /**    /*************************************************************************/
   * This method tests two <code>File</code> objects for equality by  
   * comparing the path of the specified <code>File</code> against the path  
   * of this object.  The two objects are equal if an only if 1) The  
   * argument is not null 2) The argument is a <code>File</code> object and  
   * 3) The path of the <code>File</code>argument is equal to the path  
   * of this object.  
   * <p>  
   * The paths of the files are determined by calling the <code>getPath()</code>  
   * method on each object.  
   *  
   * @return <code>true</code> if the two objects are equal, <code>false</code> otherwise.  
   */  
 public boolean  
 equals(Object obj)  
 {  
   if (obj == null)  
     return(false);  
1283    
1284    if (!(obj instanceof File))    /**
1285      return(false);      * This method tests two <code>File</code> objects for equality by
1286        * comparing the path of the specified <code>File</code> against the path
1287        * of this object.  The two objects are equal if an only if 1) The
1288        * argument is not null 2) The argument is a <code>File</code> object and
1289        * 3) The path of the <code>File</code>argument is equal to the path
1290        * of this object.
1291        * <p>
1292        * The paths of the files are determined by calling the
1293        * <code>getPath()</code>
1294        * method on each object.
1295        *
1296        * @return <code>true</code> if the two objects are equal,
1297        * <code>false</code> otherwise.
1298        */
1299      public boolean equals(Object obj)
1300      {
1301        if (obj == null)
1302          return(false);
1303    
1304    File f = (File)obj;      if (!(obj instanceof File))
1305          return(false);
1306    
1307    return(f.getPath().equals(getPath()));      File f = (File)obj;
 }  
1308    
1309  /*************************************************************************/      return(f.getPath().equals(getPath()));
1310      }
1311    
1312  /**    /*************************************************************************/
   * This method returns a hash code representing this file.  It is the  
   * hash code of the path of this file (as returned by <code>getPath()</code>)  
   * exclusived or-ed with the value 1234321.  
   *  
   * @return The hash code for this object  
   */  
 public int  
 hashCode()  
 {  
   return(getPath().hashCode() ^ 1234321);  
 }  
1313    
1314  /*************************************************************************/    /**
1315        * This method returns a hash code representing this file.  It is the
1316        * hash code of the path of this file (as returned by <code>getPath()</code>)
1317        * exclusived or-ed with the value 1234321.
1318        *
1319        * @return The hash code for this object
1320        */
1321      public int hashCode()
1322      {
1323        return(getPath().hashCode() ^ 1234321);
1324      }
1325    
1326  /**    /*************************************************************************/
   * This method returns a <code>String</code> that is the path name of the  
   * file as returned by <code>getPath</code>.  
   *  
   * @return A <code>String</code> representation of this file  
   */  
 public String  
 toString()  
 {  
   return(path);  
 }  
1327    
1328  /*************************************************************************/    /**
1329        * This method returns a <code>String</code> that is the path name of the
1330        * file as returned by <code>getPath</code>.
1331        *
1332        * @return A <code>String</code> representation of this file
1333        */
1334      public String toString()
1335      {
1336        return(path);
1337      }
1338    
1339  /**    /*************************************************************************/
1340    * This method returns a <code>URL</code> with the <code>file:</code>  
1341    * protocol that represents this file.  The exact form of this URL is    /**
1342    * system dependent.      * This method returns a <code>URL</code> with the <code>file:</code>
1343    *      * protocol that represents this file.  The exact form of this URL is
1344    * @return A <code>URL</code> for this object.      * system dependent.
1345    *      *
1346    * @exception MalformedURLException If the URL cannot be created successfully.      * @return A <code>URL</code> for this object.
1347    */      *
1348  public URL      * @exception MalformedURLException If the URL cannot be created
1349  toURL() throws MalformedURLException      * successfully.
1350  {      */
1351    String abspath = getAbsolutePath();    public URL toURL() throws MalformedURLException
1352    try    {
1353      {      String abspath = getAbsolutePath();
1354        if(new File(abspath).isDirectory())      try
1355          abspath = abspath + separator;        {
1356      }          if(new File(abspath).isDirectory())
1357    catch(Exception _) { }            abspath = abspath + separator;
1358            }
1359    String url_string = "file://" + abspath;      catch(Exception _) { }
1360          
1361    return(new URL(url_string));      String url_string = "file://" + abspath;
1362  }      
1363        return(new URL(url_string));
1364      }
1365    
1366  } // class File  } // class File
1367    

Legend:
Removed from v.1.22  
changed lines
  Added in v.1.23

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26