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 [] list(FilenameFilter filter) { 968 String names[] = list(); 969 if ((names == null) || (filter == null)) { 970 return names; 971 } 972 ArrayList v = new ArrayList (); 973 for (int i = 0 ; i < names.length ; i++) { 974 if (filter.accept(this, names[i])) { 975 v.add(names[i]); 976 } 977 } 978 return (String [])(v.toArray(new String [0])); 979 } 980 981 1014 public File [] listFiles() { 1015 String [] ss = list(); 1016 if (ss == null) return null; 1017 int n = ss.length; 1018 File [] fs = new File [n]; 1019 for (int i = 0; i < n; i++) { 1020 fs[i] = new File (ss[i], this); 1021 } 1022 return fs; 1023 } 1024 1025 1053 public File [] listFiles(FilenameFilter filter) { 1054 String ss[] = list(); 1055 if (ss == null) return null; 1056 ArrayList v = new ArrayList (); 1057 for (int i = 0 ; i < ss.length ; i++) { 1058 if ((filter == null) || filter.accept(this, ss[i])) { 1059 v.add(new File (ss[i], this)); 1060 } 1061 } 1062 return (File [])(v.toArray(new File [0])); 1063 } 1064 1065 1092 public File [] listFiles(FileFilter filter) { 1093 String ss[] = list(); 1094 if (ss == null) return null; 1095 ArrayList v = new ArrayList (); 1096 for (int i = 0 ; i < ss.length ; i++) { 1097 File f = new File (ss[i], this); 1098 if ((filter == null) || filter.accept(f)) { 1099 v.add(f); 1100 } 1101 } 1102 return (File [])(v.toArray(new File [0])); 1103 } 1104 1105 1116 public boolean mkdir() { 1117 SecurityManager security = System.getSecurityManager(); 1118 if (security != null) { 1119 security.checkWrite(path); 1120 } 1121 return fs.createDirectory(this); 1122 } 1123 1124 1144 public boolean mkdirs() { 1145 if (exists()) { 1146 return false; 1147 } 1148 if (mkdir()) { 1149 return true; 1150 } 1151 File canonFile = null; 1152 try { 1153 canonFile = getCanonicalFile(); 1154 } catch (IOException e) { 1155 return false; 1156 } 1157 String parent = canonFile.getParent(); 1158 return (parent != null) && 1159 (new File (parent, fs.prefixLength(parent)).mkdirs() && 1160 canonFile.mkdir()); 1161 } 1162 1163 1186 public boolean renameTo(File dest) { 1187 SecurityManager security = System.getSecurityManager(); 1188 if (security != null) { 1189 security.checkWrite(path); 1190 security.checkWrite(dest.path); 1191 } 1192 return fs.rename(this, dest); 1193 } 1194 1195 1221 public boolean setLastModified(long time) { 1222 if (time < 0) throw new IllegalArgumentException ("Negative time"); 1223 SecurityManager security = System.getSecurityManager(); 1224 if (security != null) { 1225 security.checkWrite(path); 1226 } 1227 return fs.setLastModifiedTime(this, time); 1228 } 1229 1230 1247 public boolean setReadOnly() { 1248 SecurityManager security = System.getSecurityManager(); 1249 if (security != null) { 1250 security.checkWrite(path); 1251 } 1252 return fs.setReadOnly(this); 1253 } 1254 1255 1256 1257 1258 1300 public static File [] listRoots() { 1301 return fs.listRoots(); 1302 } 1303 1304 1305 1306 1307 private static final Object tmpFileLock = new Object (); 1308 1309 private static int counter = -1; 1310 1311 private static File generateFile(String prefix, String suffix, File dir) 1312 throws IOException 1313 { 1314 if (counter == -1) { 1315 counter = new Random ().nextInt() & 0xffff; 1316 } 1317 counter++; 1318 return new File (dir, prefix + Integer.toString(counter) + suffix); 1319 } 1320 1321 private static String tmpdir; 1322 1323 private static String getTempDir() { 1324 if (tmpdir == null) { 1325 GetPropertyAction a = new GetPropertyAction("java.io.tmpdir"); 1326 tmpdir = ((String ) AccessController.doPrivileged(a)); 1327 tmpdir = fs.normalize(tmpdir); 1328 } 1329 return tmpdir; 1330 } 1331 1332 private static boolean checkAndCreate(String filename, SecurityManager sm) 1333 throws IOException 1334 { 1335 if (sm != null) { 1336 try { 1337 sm.checkWrite(filename); 1338 } catch (AccessControlException x) { 1339 1342 throw new SecurityException ("Unable to create temporary file"); 1343 } 1344 } 1345 return fs.createFileExclusively(filename); 1346 } 1347 1348 1417 public static File createTempFile(String prefix, String suffix, 1418 File directory) 1419 throws IOException 1420 { 1421 if (prefix == null) throw new NullPointerException (); 1422 if (prefix.length() < 3) 1423 throw new IllegalArgumentException ("Prefix string too short"); 1424 String s = (suffix == null) ? ".tmp" : suffix; 1425 synchronized (tmpFileLock) { 1426 if (directory == null) { 1427 String tmpDir = getTempDir(); 1428 directory = new File (tmpDir, fs.prefixLength(tmpDir)); 1429 } 1430 SecurityManager sm = System.getSecurityManager(); 1431 File f; 1432 do { 1433 f = generateFile(prefix, s, directory); 1434 } while (!checkAndCreate(f.getPath(), sm)); 1435 return f; 1436 } 1437 } 1438 1439 1468 public static File createTempFile(String prefix, String suffix) 1469 throws IOException 1470 { 1471 return createTempFile(prefix, suffix, null); 1472 } 1473 1474 1475 1476 1477 1494 public int compareTo(File pathname) { 1495 return fs.compare(this, pathname); 1496 } 1497 1498 1512 public boolean equals(Object obj) { 1513 if ((obj != null) && (obj instanceof File )) { 1514 return compareTo((File )obj) == 0; 1515 } 1516 return false; 1517 } 1518 1519 1532 public int hashCode() { 1533 return fs.hashCode(this); 1534 } 1535 1536 1542 public String toString() { 1543 return getPath(); 1544 } 1545 1546 1551 private synchronized void writeObject(java.io.ObjectOutputStream s) 1552 throws IOException 1553 { 1554 s.defaultWriteObject(); 1555 s.writeChar(this.separatorChar); } 1557 1558 1564 private synchronized void readObject(java.io.ObjectInputStream s) 1565 throws IOException , ClassNotFoundException 1566 { 1567 s.defaultReadObject(); 1568 char sep = s.readChar(); if (sep != separatorChar) 1570 this.path = this.path.replace(sep, separatorChar); 1571 this.path = fs.normalize(this.path); 1572 this.prefixLength = fs.prefixLength(this.path); 1573 } 1574 1575 1576 private static final long serialVersionUID = 301077366599181567L; 1577 1578} 1579 | Popular Tags |