| 1 7 8 package java.io; 9 10 import java.net.URI ; 11 import java.net.URL ; 12 import java.net.MalformedURLException ; 13 import java.net.URISyntaxException ; 14 import java.util.ArrayList ; 15 import java.util.Map ; 16 import java.util.Hashtable ; 17 import java.util.Random ; 18 import java.security.AccessController ; 19 import java.security.AccessControlException ; 20 import sun.security.action.GetPropertyAction; 21 22 23 91 92 public class File 93 implements Serializable , Comparable <File > 94 { 95 96 99 static private FileSystem fs = FileSystem.getFileSystem(); 100 101 108 private String path; 109 110 114 private transient int prefixLength; 115 116 120 int getPrefixLength() { 121 return prefixLength; 122 } 123 124 132 public static final char separatorChar = fs.getSeparator(); 133 134 139 public static final String separator = "" + separatorChar; 140 141 151 public static final char pathSeparatorChar = fs.getPathSeparator(); 152 153 158 public static final String pathSeparator = "" + pathSeparatorChar; 159 160 161 162 163 166 private File(String pathname, int prefixLength) { 167 this.path = pathname; 168 this.prefixLength = prefixLength; 169 } 170 171 176 private File(String child, File parent) { 177 assert parent.path != null; 178 assert (!parent.path.equals("")); 179 this.path = fs.resolve(parent.path, child); 180 this.prefixLength = parent.prefixLength; 181 } 182 183 192 public File(String pathname) { 193 if (pathname == null) { 194 throw new NullPointerException (); 195 } 196 this.path = fs.normalize(pathname); 197 this.prefixLength = fs.prefixLength(this.path); 198 } 199 200 206 207 232 public File(String parent, String child) { 233 if (child == null) { 234 throw new NullPointerException (); 235 } 236 if (parent != null) { 237 if (parent.equals("")) { 238 this.path = fs.resolve(fs.getDefaultParent(), 239 fs.normalize(child)); 240 } else { 241 this.path = fs.resolve(fs.normalize(parent), 242 fs.normalize(child)); 243 } 244 } else { 245 this.path = fs.normalize(child); 246 } 247 this.prefixLength = fs.prefixLength(this.path); 248 } 249 250 275 public File(File parent, String child) { 276 if (child == null) { 277 throw new NullPointerException (); 278 } 279 if (parent != null) { 280 if (parent.path.equals("")) { 281 this.path = fs.resolve(fs.getDefaultParent(), 282 fs.normalize(child)); 283 } else { 284 this.path = fs.resolve(parent.path, 285 fs.normalize(child)); 286 } 287 } else { 288 this.path = fs.normalize(child); 289 } 290 this.prefixLength = fs.prefixLength(this.path); 291 } 292 293 329 public File(URI uri) { 330 331 if (!uri.isAbsolute()) 333 throw new IllegalArgumentException ("URI is not absolute"); 334 if (uri.isOpaque()) 335 throw new IllegalArgumentException ("URI is not hierarchical"); 336 String scheme = uri.getScheme(); 337 if ((scheme == null) || !scheme.equalsIgnoreCase("file")) 338 throw new IllegalArgumentException ("URI scheme is not \"file\""); 339 if (uri.getAuthority() != null) 340 throw new IllegalArgumentException ("URI has an authority component"); 341 if (uri.getFragment() != null) 342 throw new IllegalArgumentException ("URI has a fragment component"); 343 if (uri.getQuery() != null) 344 throw new IllegalArgumentException ("URI has a query component"); 345 String p = uri.getPath(); 346 if (p.equals("")) 347 throw new IllegalArgumentException ("URI path component is empty"); 348 349 p = fs.fromURIPath(p); 351 if (File.separatorChar != '/') 352 p = p.replace('/', File.separatorChar); 353 this.path = fs.normalize(p); 354 this.prefixLength = fs.prefixLength(this.path); 355 } 356 357 358 359 360 370 public String getName() { 371 int index = path.lastIndexOf(separatorChar); 372 if (index < prefixLength) return path.substring(prefixLength); 373 return path.substring(index + 1); 374 } 375 376 389 public String getParent() { 390 int index = path.lastIndexOf(separatorChar); 391 if (index < prefixLength) { 392 if ((prefixLength > 0) && (path.length() > prefixLength)) 393 return path.substring(0, prefixLength); 394 return null; 395 } 396 return path.substring(0, index); 397 } 398 399 415 public File getParentFile() { 416 String p = this.getParent(); 417 if (p == null) return null; 418 return new File (p, this.prefixLength); 419 } 420 421 428 public String getPath() { 429 return path; 430 } 431 432 433 434 435 445 public boolean isAbsolute() { 446 return fs.isAbsolute(this); 447 } 448 449 472 public String getAbsolutePath() { 473 return fs.resolve(this); 474 } 475 476 488 public File getAbsoluteFile() { 489 String absPath = getAbsolutePath(); 490 return new File (absPath, fs.prefixLength(absPath)); 491 } 492 493 530 public String getCanonicalPath() throws IOException { 531 return fs.canonicalize(fs.resolve(this)); 532 } 533 534 554 public File getCanonicalFile() throws IOException { 555 String canonPath = getCanonicalPath(); 556 return new File (canonPath, fs.prefixLength(canonPath)); 557 } 558 559 private static String slashify(String path, boolean isDirectory) { 560 String p = path; 561 if (File.separatorChar != '/') 562 p = p.replace(File.separatorChar, '/'); 563 if (!p.startsWith("/")) 564 p = "/" + p; 565 if (!p.endsWith("/") && isDirectory) 566 p = p + "/"; 567 return p; 568 } 569 570 593 public URL toURL() throws MalformedURLException { 594 return new URL ("file", "", slashify(getAbsolutePath(), isDirectory())); 595 } 596 597 627 public URI toURI() { 628 try { 629 File f = getAbsoluteFile(); 630 String sp = slashify(f.getPath(), f.isDirectory()); 631 if (sp.startsWith("//")) 632 sp = "//" + sp; 633 return new URI ("file", null, sp, null); 634 } catch (URISyntaxException x) { 635 throw new Error (x); } 637 } 638 639 640 641 642 655 public boolean canRead() { 656 SecurityManager security = System.getSecurityManager(); 657 if (security != null) { 658 security.checkRead(path); 659 } 660 return fs.checkAccess(this, false); 661 } 662 663 677 public boolean canWrite() { 678 SecurityManager security = System.getSecurityManager(); 679 if (security != null) { 680 security.checkWrite(path); 681 } 682 return fs.checkAccess(this, true); 683 } 684 685 697 public boolean exists() { 698 SecurityManager security = System.getSecurityManager(); 699 if (security != null) { 700 security.checkRead(path); 701 } 702 return ((fs.getBooleanAttributes(this) & FileSystem.BA_EXISTS) != 0); 703 } 704 705 718 public boolean isDirectory() { 719 SecurityManager security = System.getSecurityManager(); 720 if (security != null) { 721 security.checkRead(path); 722 } 723 return ((fs.getBooleanAttributes(this) & FileSystem.BA_DIRECTORY) 724 != 0); 725 } 726 727 742 public boolean isFile() { 743 SecurityManager security = System.getSecurityManager(); 744 if (security != null) { 745 security.checkRead(path); 746 } 747 return ((fs.getBooleanAttributes(this) & FileSystem.BA_REGULAR) != 0); 748 } 749 750 768 public boolean isHidden() { 769 SecurityManager security = System.getSecurityManager(); 770 if (security != null) { 771 security.checkRead(path); 772 } 773 return ((fs.getBooleanAttributes(this) & FileSystem.BA_HIDDEN) != 0); 774 } 775 776 790 public long lastModified() { 791 SecurityManager security = System.getSecurityManager(); 792 if (security != null) { 793 security.checkRead(path); 794 } 795 return fs.getLastModifiedTime(this); 796 } 797 798 810 public long length() { 811 SecurityManager security = System.getSecurityManager(); 812 if (security != null) { 813 security.checkRead(path); 814 } 815 return fs.getLength(this); 816 } 817 818 819 820 821 847 public boolean createNewFile() throws IOException { 848 SecurityManager security = System.getSecurityManager(); 849 if (security != null) security.checkWrite(path); 850 return fs.createFileExclusively(path); 851 } 852 853 866 public boolean delete() { 867 SecurityManager security = System.getSecurityManager(); 868 if (security != null) { 869 security.checkDelete(path); 870 } 871 return fs.delete(this); 872 } 873 874 898 public void deleteOnExit() { 899 SecurityManager security = System.getSecurityManager(); 900 if (security != null) { 901 security.checkDelete(path); 902 } 903 fs.deleteOnExit(this); 904 } 905 906 932 public String [] list() { 933 SecurityManager security = System.getSecurityManager(); 934 if (security != null) { 935 security.checkRead(path); 936 } 937 return fs.list(this); 938 } 939 940 967 public String  |