home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1997 May / Pcwk0597.iso / sybase / starbuck / java.z / File.java < prev    next >
Text File  |  1996-05-03  |  9KB  |  349 lines

  1. /*
  2.  * @(#)File.java    1.37 95/12/19 Jonathan Payne, Arthur van Hoff
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. package java.io;
  21.  
  22. import java.util.Vector;
  23.  
  24. /**
  25.  * This class represents a file name of the host file system.
  26.  * The file name can be relative or absolute. It must use
  27.  * the file name conventions of the host platform. <p>
  28.  *
  29.  * The intention is to provide an abstraction that deals
  30.  * with most of the system-dependent file name features such
  31.  * as the separator character, root, device name, etc.
  32.  * Not all features are currently fully implemented.<p>
  33.  *
  34.  * Note that whenever a file name or path is  used it is
  35.  * assumed that the host's file name conventions are used.
  36.  *
  37.  * @version     1.37, 19 Dec 1995
  38.  * @author    Jonathan Payne
  39.  * @author    Arthur van Hoff
  40.  */
  41. public
  42. class File {
  43.     /**
  44.      * The path of the file. The host's file separator is used.
  45.      */
  46.     private String path;
  47.  
  48.     /**
  49.      * The system dependent file separator String.
  50.      */
  51.     public static final String separator = System.getProperty("file.separator");
  52.  
  53.     /**
  54.      * The system dependent file separator character.
  55.      */
  56.     public static final char separatorChar = separator.charAt(0);
  57.     
  58.     /**
  59.      * The system dependent path separator string.
  60.      */
  61.     public static final String pathSeparator = System.getProperty("path.separator");
  62.  
  63.     /**
  64.      * The system dependent path separator character.
  65.      */
  66.     public static final char pathSeparatorChar = pathSeparator.charAt(0);
  67.     
  68.     /**
  69.      * Creates a File object.
  70.      * @param path the file path
  71.      * @exception NullPointerException If the file path is equal to 
  72.      * null.
  73.      */
  74.     public File(String path) {
  75.     if (path == null) {
  76.         throw new NullPointerException();
  77.     }
  78.     this.path = path;
  79.     }    
  80.  
  81.     /**
  82.      * Creates a File object from the specified directory.
  83.      * @param path the directory path
  84.      * @param name the file name
  85.      */
  86.     public File(String path, String name) {
  87.     this((path != null) ? path + separator + name : name);
  88.     }
  89.  
  90.     /**
  91.      * Creates a File object (given a directory File object).
  92.      * @param dir the directory
  93.      * @param name the file name
  94.      */
  95.     public File(File dir, String name) {
  96.     this(dir.getPath(), name);
  97.     }
  98.  
  99.     /**
  100.      * Gets the name of the file. This method does not include the
  101.      * directory.
  102.      * @return the file name.
  103.      */
  104.     public String getName() {
  105.     int index = path.lastIndexOf(separatorChar);
  106.     return (index < 0) ? path : path.substring(index + 1);
  107.     }
  108.  
  109.     /**
  110.      * Gets the path of the file.
  111.      * @return the file path.
  112.      */
  113.     public String getPath() {
  114.     return path;
  115.     }
  116.  
  117.     /**
  118.      * Gets the absolute path of the file.
  119.      * @return the absolute file path.
  120.      */
  121.     public String getAbsolutePath() {
  122.     return isAbsolute() ? path : System.getProperty("user.dir") + separator + path;
  123.     }
  124.  
  125.     /**
  126.      * Gets the name of the parent directory.
  127.      * @return the parent directory, or null if one is not found.
  128.      */
  129.     public String getParent() {
  130.     int index = path.lastIndexOf(separatorChar);
  131.     return (index <= 0) ? null : path.substring(0, index);
  132.     }
  133.  
  134.     private native boolean exists0();
  135.     private native boolean canWrite0();
  136.     private native boolean canRead0();
  137.     private native boolean isFile0();
  138.     private native boolean isDirectory0();
  139.     private native long lastModified0();
  140.     private native long length0();
  141.     private native boolean mkdir0();
  142.     private native boolean renameTo0(File dest);
  143.     private native boolean delete0();
  144.     private native String[] list0();
  145.  
  146.     /**
  147.      * Returns a boolean indicating whether or not a file exists.
  148.      */
  149.     public boolean exists() {
  150.     SecurityManager security = System.getSecurityManager();
  151.     if (security != null) {
  152.         security.checkRead(path);
  153.     }
  154.     return exists0();
  155.     }
  156.  
  157.     /**
  158.      * Returns a boolean indicating whether or not a writable file 
  159.      * exists. 
  160.      */
  161.     public boolean canWrite() {
  162.     SecurityManager security = System.getSecurityManager();
  163.     if (security != null) {
  164.         security.checkWrite(path);
  165.     }
  166.     return canWrite0();
  167.     }
  168.  
  169.     /**
  170.      * Returns a boolean indicating whether or not a readable file 
  171.      * exists.
  172.      */
  173.     public boolean canRead() {
  174.     SecurityManager security = System.getSecurityManager();
  175.     if (security != null) {
  176.         security.checkRead(path);
  177.     }
  178.     return canRead0();
  179.     }
  180.  
  181.     /**
  182.      * Returns a boolean indicating whether or not a normal file 
  183.      * exists.
  184.      */
  185.     public boolean isFile() {
  186.     SecurityManager security = System.getSecurityManager();
  187.     if (security != null) {
  188.         security.checkRead(path);
  189.     }
  190.     return isFile0();
  191.     }
  192.  
  193.     /**
  194.      * Returns a boolean indicating whether or not a directory file 
  195.      * exists.
  196.      */
  197.     public boolean isDirectory() {
  198.     SecurityManager security = System.getSecurityManager();
  199.     if (security != null) {
  200.         security.checkRead(path);
  201.     }
  202.     return isDirectory0();
  203.     }
  204.  
  205.     /**
  206.      * Returns a boolean indicating whether the file name is absolute.
  207.      */
  208.     public native boolean isAbsolute();
  209.  
  210.     /**
  211.      * Returns the last modification time. The return value should
  212.      * only be used to compare modification dates. It is meaningless
  213.      * as an absolute time.
  214.      */
  215.     public long lastModified() {
  216.     SecurityManager security = System.getSecurityManager();
  217.     if (security != null) {
  218.         security.checkRead(path);
  219.     }
  220.     return lastModified0();
  221.     }
  222.  
  223.     /**
  224.      * Returns the length of the file. 
  225.      */
  226.     public long length() {
  227.     SecurityManager security = System.getSecurityManager();
  228.     if (security != null) {
  229.         security.checkRead(path);
  230.     }
  231.     return length0();
  232.     }
  233.  
  234.  
  235.     /**
  236.      * Creates a directory and returns a boolean indicating the
  237.      * success of the creation.
  238.      */
  239.     public boolean mkdir() {
  240.     SecurityManager security = System.getSecurityManager();
  241.     if (security != null) {
  242.         security.checkWrite(path);
  243.     }
  244.     return mkdir0();
  245.     }
  246.  
  247.     /**
  248.      * Renames a file and returns a boolean indicating whether 
  249.      * or not this method was successful.
  250.      * @param dest the new file name
  251.      */
  252.     public boolean renameTo(File dest) {
  253.     SecurityManager security = System.getSecurityManager();
  254.     if (security != null) {
  255.         security.checkWrite(path);
  256.         security.checkWrite(dest.path);
  257.     }
  258.     return renameTo0(dest);
  259.     }
  260.  
  261.     /**
  262.      * Creates all directories in this path.  This method 
  263.      * returns true if all directories in this path are created.
  264.      */
  265.     public boolean mkdirs() {
  266.     if (mkdir()) {
  267.          return true;
  268.      }
  269.  
  270.     String parent = getParent();
  271.     return (parent != null) && (new File(parent).mkdirs() && mkdir());
  272.     }
  273.  
  274.     /**
  275.      * Lists the files in a directory. Works only on directories.
  276.      * @return an array of file names.  This list will include all
  277.      * files in the directory except the equivalent of "." and ".." .
  278.      */
  279.     public String[] list() {
  280.     SecurityManager security = System.getSecurityManager();
  281.     if (security != null) {
  282.         security.checkRead(path);
  283.     }
  284.     return list0();
  285.     }
  286.  
  287.     /**
  288.      * Uses the specified filter to list files in a directory. 
  289.      * @param filter the filter used to select file names
  290.      * @return the filter selected files in this directory.
  291.      * @see FilenameFilter
  292.      */
  293.     public String[] list(FilenameFilter filter) {
  294.     String names[] = list();
  295.  
  296.     // Fill in the Vector
  297.     Vector v = new Vector();
  298.     for (int i = 0 ; i < names.length ; i++) {
  299.         if ((filter == null) || filter.accept(this, names[i])) {
  300.         v.addElement(names[i]);
  301.         }
  302.     }
  303.  
  304.     // Create the array
  305.     String files[] = new String[v.size()];
  306.     v.copyInto(files);
  307.  
  308.     return files;
  309.     }
  310.  
  311.     /**
  312.      * Deletes the specified file. Returns true
  313.      * if the file could be deleted.
  314.      */
  315.     public boolean delete() {
  316.     SecurityManager security = System.getSecurityManager();
  317.     if (security != null) {
  318.         security.checkDelete(path);
  319.     }
  320.     return delete0();
  321.     }
  322.  
  323.     /**
  324.      * Computes a hashcode for the file.
  325.      */
  326.     public int hashCode() {
  327.     return path.hashCode() ^ 1234321;
  328.     }
  329.  
  330.     /**
  331.      * Compares this object against the specified object.
  332.      * @param obj  the object to compare with
  333.      * @return     true if the objects are the same; false otherwise.
  334.      */
  335.     public boolean equals(Object obj) {
  336.     if ((obj != null) && (obj instanceof File)) {
  337.         return path.equals(((File)obj).path);
  338.     }
  339.     return false;
  340.     }
  341.  
  342.     /**
  343.      * Returns a String object representing this file's path.
  344.      */
  345.     public String toString() {
  346.     return getPath();
  347.     }
  348. }
  349.