home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1998 January / PCO0198.ISO / 1&1 / java.z / java_301 / java / io / File.class (.txt) < prev    next >
Encoding:
Java Class File  |  1996-10-20  |  4.8 KB  |  217 lines

  1. package java.io;
  2.  
  3. import java.util.Vector;
  4.  
  5. public class File {
  6.    private String path;
  7.    public static final String separator = System.getProperty("file.separator");
  8.    public static final char separatorChar;
  9.    public static final String pathSeparator;
  10.    public static final char pathSeparatorChar;
  11.  
  12.    public File(String path) {
  13.       if (path == null) {
  14.          throw new NullPointerException();
  15.       } else {
  16.          this.path = path;
  17.       }
  18.    }
  19.  
  20.    public File(String path, String name) {
  21.       this(path != null ? path + separator + name : name);
  22.    }
  23.  
  24.    public File(File dir, String name) {
  25.       this(dir.getPath(), name);
  26.    }
  27.  
  28.    public String getName() {
  29.       int index = this.path.lastIndexOf(separatorChar);
  30.       return index < 0 ? this.path : this.path.substring(index + 1);
  31.    }
  32.  
  33.    public String getPath() {
  34.       return this.path;
  35.    }
  36.  
  37.    public String getAbsolutePath() {
  38.       if (this.isAbsolute()) {
  39.          return this.path;
  40.       } else {
  41.          String key = "user.dir";
  42.          return System.getProperty(key) + separator + this.path;
  43.       }
  44.    }
  45.  
  46.    public String getParent() {
  47.       int index = this.path.lastIndexOf(separatorChar);
  48.       return index <= 0 ? null : this.path.substring(0, index);
  49.    }
  50.  
  51.    private native boolean exists0();
  52.  
  53.    private native boolean canWrite0();
  54.  
  55.    private native boolean canRead0();
  56.  
  57.    private native boolean isFile0();
  58.  
  59.    private native boolean isDirectory0();
  60.  
  61.    private native long lastModified0();
  62.  
  63.    private native long length0();
  64.  
  65.    private native boolean mkdir0();
  66.  
  67.    private native boolean renameTo0(File var1);
  68.  
  69.    private native boolean delete0();
  70.  
  71.    private native String[] list0();
  72.  
  73.    public boolean exists() {
  74.       SecurityManager security = System.getSecurityManager();
  75.       if (security != null) {
  76.          security.checkRead(this.path);
  77.       }
  78.  
  79.       return this.exists0();
  80.    }
  81.  
  82.    public boolean canWrite() {
  83.       SecurityManager security = System.getSecurityManager();
  84.       if (security != null) {
  85.          security.checkWrite(this.path);
  86.       }
  87.  
  88.       return this.canWrite0();
  89.    }
  90.  
  91.    public boolean canRead() {
  92.       SecurityManager security = System.getSecurityManager();
  93.       if (security != null) {
  94.          security.checkRead(this.path);
  95.       }
  96.  
  97.       return this.canRead0();
  98.    }
  99.  
  100.    public boolean isFile() {
  101.       SecurityManager security = System.getSecurityManager();
  102.       if (security != null) {
  103.          security.checkRead(this.path);
  104.       }
  105.  
  106.       return this.isFile0();
  107.    }
  108.  
  109.    public boolean isDirectory() {
  110.       SecurityManager security = System.getSecurityManager();
  111.       if (security != null) {
  112.          security.checkRead(this.path);
  113.       }
  114.  
  115.       return this.isDirectory0();
  116.    }
  117.  
  118.    public native boolean isAbsolute();
  119.  
  120.    public long lastModified() {
  121.       SecurityManager security = System.getSecurityManager();
  122.       if (security != null) {
  123.          security.checkRead(this.path);
  124.       }
  125.  
  126.       return this.lastModified0();
  127.    }
  128.  
  129.    public long length() {
  130.       SecurityManager security = System.getSecurityManager();
  131.       if (security != null) {
  132.          security.checkRead(this.path);
  133.       }
  134.  
  135.       return this.length0();
  136.    }
  137.  
  138.    public boolean mkdir() {
  139.       SecurityManager security = System.getSecurityManager();
  140.       if (security != null) {
  141.          security.checkWrite(this.path);
  142.       }
  143.  
  144.       return this.mkdir0();
  145.    }
  146.  
  147.    public boolean renameTo(File dest) {
  148.       SecurityManager security = System.getSecurityManager();
  149.       if (security != null) {
  150.          security.checkWrite(this.path);
  151.          security.checkWrite(dest.path);
  152.       }
  153.  
  154.       return this.renameTo0(dest);
  155.    }
  156.  
  157.    public boolean mkdirs() {
  158.       if (this.mkdir()) {
  159.          return true;
  160.       } else {
  161.          String parent = this.getParent();
  162.          return parent != null && (new File(parent)).mkdirs() && this.mkdir();
  163.       }
  164.    }
  165.  
  166.    public String[] list() {
  167.       SecurityManager security = System.getSecurityManager();
  168.       if (security != null) {
  169.          security.checkRead(this.path);
  170.       }
  171.  
  172.       return this.list0();
  173.    }
  174.  
  175.    public String[] list(FilenameFilter filter) {
  176.       String[] names = this.list();
  177.       Vector v = new Vector();
  178.  
  179.       for(int i = 0; i < names.length; ++i) {
  180.          if (filter == null || filter.accept(this, names[i])) {
  181.             v.addElement(names[i]);
  182.          }
  183.       }
  184.  
  185.       String[] files = new String[v.size()];
  186.       v.copyInto(files);
  187.       return files;
  188.    }
  189.  
  190.    public boolean delete() {
  191.       SecurityManager security = System.getSecurityManager();
  192.       if (security != null) {
  193.          security.checkDelete(this.path);
  194.       }
  195.  
  196.       return this.delete0();
  197.    }
  198.  
  199.    public int hashCode() {
  200.       return this.path.hashCode() ^ 1234321;
  201.    }
  202.  
  203.    public boolean equals(Object obj) {
  204.       return obj != null && obj instanceof File ? this.path.equals(((File)obj).path) : false;
  205.    }
  206.  
  207.    public String toString() {
  208.       return this.getPath();
  209.    }
  210.  
  211.    static {
  212.       separatorChar = separator.charAt(0);
  213.       pathSeparator = System.getProperty("path.separator");
  214.       pathSeparatorChar = pathSeparator.charAt(0);
  215.    }
  216. }
  217.