home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / solaris2 / jdk / src / java / io / file.jav < prev    next >
Encoding:
Text File  |  1995-10-30  |  8.9 KB  |  336 lines

  1. /*
  2.  * @(#)File.java    1.34 95/10/22 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.34, 10/22/95
  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() throws IOException;
  140.     private native long length0() throws IOException;
  141.     private native boolean mkdir0() throws IOException;
  142.     private native boolean renameTo0(File dest) throws IOException;
  143.     private native String[] list0() throws IOException;
  144.  
  145.     /**
  146.      * Returns a boolean indicating whether or not a file exists.
  147.      */
  148.     public boolean exists() {
  149.     SecurityManager security = System.getSecurityManager();
  150.     if (security != null) {
  151.         security.checkRead(path);
  152.     }
  153.     return exists0();
  154.     }
  155.  
  156.     /**
  157.      * Returns a boolean indicating whether or not a writable file 
  158.      * exists. 
  159.      */
  160.     public boolean canWrite() {
  161.     SecurityManager security = System.getSecurityManager();
  162.     if (security != null) {
  163.         security.checkWrite(path);
  164.     }
  165.     return canWrite0();
  166.     }
  167.  
  168.     /**
  169.      * Returns a boolean indicating whether or not a readable file 
  170.      * exists.
  171.      */
  172.     public boolean canRead() {
  173.     SecurityManager security = System.getSecurityManager();
  174.     if (security != null) {
  175.         security.checkRead(path);
  176.     }
  177.     return canRead0();
  178.     }
  179.  
  180.     /**
  181.      * Returns a boolean indicating whether or not a normal file 
  182.      * exists.
  183.      */
  184.     public boolean isFile() {
  185.     SecurityManager security = System.getSecurityManager();
  186.     if (security != null) {
  187.         security.checkRead(path);
  188.     }
  189.     return isFile0();
  190.     }
  191.  
  192.     /**
  193.      * Returns a boolean indicating whether or not a directory file 
  194.      * exists.
  195.      */
  196.     public boolean isDirectory() {
  197.     SecurityManager security = System.getSecurityManager();
  198.     if (security != null) {
  199.         security.checkRead(path);
  200.     }
  201.     return isDirectory0();
  202.     }
  203.  
  204.     /**
  205.      * Returns a boolean indicating if the file name is absolute.
  206.      */
  207.     public native boolean isAbsolute();
  208.  
  209.     /**
  210.      * Returns the last modification time. The return value should
  211.      * only be used to compare modification dates. It is meaningless
  212.      * as an absolute time.
  213.      */
  214.     public long lastModified() throws IOException {
  215.     SecurityManager security = System.getSecurityManager();
  216.     if (security != null) {
  217.         security.checkRead(path);
  218.     }
  219.     return lastModified0();
  220.     }
  221.  
  222.     /**
  223.      * Returns the length of the file. 
  224.      */
  225.     public long length() throws IOException {
  226.     SecurityManager security = System.getSecurityManager();
  227.     if (security != null) {
  228.         security.checkRead(path);
  229.     }
  230.     return length0();
  231.     }
  232.  
  233.  
  234.     /**
  235.      * Creates a directory and returns a boolean indicating the
  236.      * success of the creation.
  237.      */
  238.     public boolean mkdir() throws IOException {
  239.     SecurityManager security = System.getSecurityManager();
  240.     if (security != null) {
  241.         security.checkWrite(path);
  242.     }
  243.     return mkdir0();
  244.     }
  245.  
  246.     /**
  247.      * Renames a file and returns a boolean indicating whether 
  248.      * or not this method was successful.
  249.      * @param dest the new file name
  250.      */
  251.     public boolean renameTo(File dest) throws IOException {
  252.     SecurityManager security = System.getSecurityManager();
  253.     if (security != null) {
  254.         security.checkWrite(path);
  255.         security.checkWrite(dest.path);
  256.     }
  257.     return renameTo0(dest);
  258.     }
  259.  
  260.     /**
  261.      * Creates all directories in this path.  This method 
  262.      * returns true if all directories in this path are created.
  263.      */
  264.     public boolean mkdirs() throws IOException {
  265.     if (mkdir()) {
  266.          return true;
  267.      }
  268.  
  269.     String parent = getParent();
  270.     return (parent != null) && (new File(parent).mkdirs() && mkdir());
  271.     }
  272.  
  273.     /**
  274.      * Lists the files in a directory. Works only on directories.
  275.      * @return an array of file names.  This list will include all
  276.      * files in the directory except the equivalent of "." and ".." .
  277.      */
  278.     public String[] list() throws IOException {
  279.     SecurityManager security = System.getSecurityManager();
  280.     if (security != null) {
  281.         security.checkRead(path);
  282.     }
  283.     return list0();
  284.     }
  285.  
  286.     /**
  287.      * Uses the specified filter to list files in a directory. 
  288.      * @param filter the filter used to select file names
  289.      * @return the filter selected files in this directory.
  290.      * @see FilenameFilter
  291.      */
  292.     public String[] list(FilenameFilter filter) throws IOException {
  293.     String names[] = list();
  294.  
  295.     // Fill in the Vector
  296.     Vector v = new Vector();
  297.     for (int i = 0 ; i < names.length ; i++) {
  298.         if ((filter == null) || filter.accept(this, names[i])) {
  299.         v.addElement(names[i]);
  300.         }
  301.     }
  302.  
  303.     // Create the array
  304.     String files[] = new String[v.size()];
  305.     v.copyInto(files);
  306.  
  307.     return files;
  308.     }
  309.  
  310.     /**
  311.      * Computes a hashcode for the file.
  312.      */
  313.     public int hashCode() {
  314.     return path.hashCode() ^ 1234321;
  315.     }
  316.  
  317.     /**
  318.      * Compares this object against the specified object.
  319.      * @param obj  the object to compare with
  320.      * @return     true if the objects are the same; false otherwise.
  321.      */
  322.     public boolean equals(Object obj) {
  323.     if ((obj != null) && (obj instanceof File)) {
  324.         return path.equals(((File)obj).path);
  325.     }
  326.     return false;
  327.     }
  328.  
  329.     /**
  330.      * Returns a String object representing this file's path.
  331.      */
  332.     public String toString() {
  333.     return getPath();
  334.     }
  335. }
  336.