1 /* 2 * Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package java.nio.file; 27 28 import java.io.File; 29 import java.io.IOException; 30 import java.net.URI; 31 import java.nio.file.spi.FileSystemProvider; 32 import java.util.Iterator; 33 import java.util.NoSuchElementException; 34 35 /** 36 * An object that may be used to locate a file in a file system. It will 37 * typically represent a system dependent file path. 38 * 39 * <p> A {@code Path} represents a path that is hierarchical and composed of a 40 * sequence of directory and file name elements separated by a special separator 41 * or delimiter. A <em>root component</em>, that identifies a file system 42 * hierarchy, may also be present. The name element that is <em>farthest</em> 43 * from the root of the directory hierarchy is the name of a file or directory. 44 * The other name elements are directory names. A {@code Path} can represent a 45 * root, a root and a sequence of names, or simply one or more name elements. 46 * A {@code Path} is considered to be an <i>empty path</i> if it consists 47 * solely of one name element that is empty. Accessing a file using an 48 * <i>empty path</i> is equivalent to accessing the default directory of the 49 * file system. {@code Path} defines the {@link #getFileName() getFileName}, 50 * {@link #getParent getParent}, {@link #getRoot getRoot}, and {@link #subpath 51 * subpath} methods to access the path components or a subsequence of its name 52 * elements. 53 * 54 * <p> In addition to accessing the components of a path, a {@code Path} also 55 * defines the {@link #resolve(Path) resolve} and {@link #resolveSibling(Path) 56 * resolveSibling} methods to combine paths. The {@link #relativize relativize} 57 * method that can be used to construct a relative path between two paths. 58 * Paths can be {@link #compareTo compared}, and tested against each other using 59 * the {@link #startsWith startsWith} and {@link #endsWith endsWith} methods. 60 * 61 * <p> This interface extends {@link Watchable} interface so that a directory 62 * located by a path can be {@link #register registered} with a {@link 63 * WatchService} and entries in the directory watched. </p> 64 * 65 * <p> <b>WARNING:</b> This interface is only intended to be implemented by 66 * those developing custom file system implementations. Methods may be added to 67 * this interface in future releases. </p> 68 * 69 * <h2>Accessing Files</h2> 70 * <p> Paths may be used with the {@link Files} class to operate on files, 71 * directories, and other types of files. For example, suppose we want a {@link 72 * java.io.BufferedReader} to read text from a file "{@code access.log}". The 73 * file is located in a directory "{@code logs}" relative to the current working 74 * directory and is UTF-8 encoded. 75 * <pre> 76 * Path path = FileSystems.getDefault().getPath("logs", "access.log"); 77 * BufferedReader reader = Files.newBufferedReader(path, StandardCharsets.UTF_8); 78 * </pre> 79 * 80 * <a id="interop"></a><h2>Interoperability</h2> 81 * <p> Paths associated with the default {@link 82 * java.nio.file.spi.FileSystemProvider provider} are generally interoperable 83 * with the {@link java.io.File java.io.File} class. Paths created by other 84 * providers are unlikely to be interoperable with the abstract path names 85 * represented by {@code java.io.File}. The {@link java.io.File#toPath toPath} 86 * method may be used to obtain a {@code Path} from the abstract path name 87 * represented by a {@code java.io.File} object. The resulting {@code Path} can 88 * be used to operate on the same file as the {@code java.io.File} object. In 89 * addition, the {@link #toFile toFile} method is useful to construct a {@code 90 * File} from the {@code String} representation of a {@code Path}. 91 * 92 * <h2>Concurrency</h2> 93 * <p> Implementations of this interface are immutable and safe for use by 94 * multiple concurrent threads. 95 * 96 * @since 1.7 97 */ 98 99 public interface Path 100 extends Comparable<Path>, Iterable<Path>, Watchable 101 { 102 /** 103 * Returns a {@code Path} by converting a path string, or a sequence of 104 * strings that when joined form a path string. If {@code more} does not 105 * specify any elements then the value of the {@code first} parameter is 106 * the path string to convert. If {@code more} specifies one or more 107 * elements then each non-empty string, including {@code first}, is 108 * considered to be a sequence of name elements and is joined to form a 109 * path string. The details as to how the Strings are joined is provider 110 * specific but typically they will be joined using the 111 * {@link FileSystem#getSeparator name-separator} as the separator. 112 * For example, if the name separator is "{@code /}" and 113 * {@code getPath("/foo","bar","gus")} is invoked, then the path string 114 * {@code "/foo/bar/gus"} is converted to a {@code Path}. A {@code Path} 115 * representing an empty path is returned if {@code first} is the empty 116 * string and {@code more} does not contain any non-empty strings. 117 * 118 * <p> The {@code Path} is obtained by invoking the {@link FileSystem#getPath 119 * getPath} method of the {@link FileSystems#getDefault default} {@link 120 * FileSystem}. 121 * 122 * <p> Note that while this method is very convenient, using it will imply 123 * an assumed reference to the default {@code FileSystem} and limit the 124 * utility of the calling code. Hence it should not be used in library code 125 * intended for flexible reuse. A more flexible alternative is to use an 126 * existing {@code Path} instance as an anchor, such as: 127 * <pre>{@code 128 * Path dir = ... 129 * Path path = dir.resolve("file"); 130 * }</pre> 131 * 132 * @param first 133 * the path string or initial part of the path string 134 * @param more 135 * additional strings to be joined to form the path string 136 * 137 * @return the resulting {@code Path} 138 * 139 * @throws InvalidPathException 140 * if the path string cannot be converted to a {@code Path} 141 * 142 * @see FileSystem#getPath 143 * 144 * @since 11 145 */ of(String first, String... more)146 public static Path of(String first, String... more) { 147 return FileSystems.getDefault().getPath(first, more); 148 } 149 150 /** 151 * Returns a {@code Path} by converting a URI. 152 * 153 * <p> This method iterates over the {@link FileSystemProvider#installedProviders() 154 * installed} providers to locate the provider that is identified by the 155 * URI {@link URI#getScheme scheme} of the given URI. URI schemes are 156 * compared without regard to case. If the provider is found then its {@link 157 * FileSystemProvider#getPath getPath} method is invoked to convert the 158 * URI. 159 * 160 * <p> In the case of the default provider, identified by the URI scheme 161 * "file", the given URI has a non-empty path component, and undefined query 162 * and fragment components. Whether the authority component may be present 163 * is platform specific. The returned {@code Path} is associated with the 164 * {@link FileSystems#getDefault default} file system. 165 * 166 * <p> The default provider provides a similar <em>round-trip</em> guarantee 167 * to the {@link java.io.File} class. For a given {@code Path} <i>p</i> it 168 * is guaranteed that 169 * <blockquote>{@code 170 * Path.of(}<i>p</i>{@code .}{@link Path#toUri() toUri}{@code ()).equals(} 171 * <i>p</i>{@code .}{@link Path#toAbsolutePath() toAbsolutePath}{@code ())} 172 * </blockquote> 173 * so long as the original {@code Path}, the {@code URI}, and the new {@code 174 * Path} are all created in (possibly different invocations of) the same 175 * Java virtual machine. Whether other providers make any guarantees is 176 * provider specific and therefore unspecified. 177 * 178 * @param uri 179 * the URI to convert 180 * 181 * @return the resulting {@code Path} 182 * 183 * @throws IllegalArgumentException 184 * if preconditions on the {@code uri} parameter do not hold. The 185 * format of the URI is provider specific. 186 * @throws FileSystemNotFoundException 187 * The file system, identified by the URI, does not exist and 188 * cannot be created automatically, or the provider identified by 189 * the URI's scheme component is not installed 190 * @throws SecurityException 191 * if a security manager is installed and it denies an unspecified 192 * permission to access the file system 193 * 194 * @since 11 195 */ of(URI uri)196 public static Path of(URI uri) { 197 String scheme = uri.getScheme(); 198 if (scheme == null) 199 throw new IllegalArgumentException("Missing scheme"); 200 201 // check for default provider to avoid loading of installed providers 202 if (scheme.equalsIgnoreCase("file")) 203 return FileSystems.getDefault().provider().getPath(uri); 204 205 // try to find provider 206 for (FileSystemProvider provider: FileSystemProvider.installedProviders()) { 207 if (provider.getScheme().equalsIgnoreCase(scheme)) { 208 return provider.getPath(uri); 209 } 210 } 211 212 throw new FileSystemNotFoundException("Provider \"" + scheme + "\" not installed"); 213 } 214 215 /** 216 * Returns the file system that created this object. 217 * 218 * @return the file system that created this object 219 */ getFileSystem()220 FileSystem getFileSystem(); 221 222 /** 223 * Tells whether or not this path is absolute. 224 * 225 * <p> An absolute path is complete in that it doesn't need to be combined 226 * with other path information in order to locate a file. 227 * 228 * @return {@code true} if, and only if, this path is absolute 229 */ isAbsolute()230 boolean isAbsolute(); 231 232 /** 233 * Returns the root component of this path as a {@code Path} object, 234 * or {@code null} if this path does not have a root component. 235 * 236 * @return a path representing the root component of this path, 237 * or {@code null} 238 */ getRoot()239 Path getRoot(); 240 241 /** 242 * Returns the name of the file or directory denoted by this path as a 243 * {@code Path} object. The file name is the <em>farthest</em> element from 244 * the root in the directory hierarchy. 245 * 246 * @return a path representing the name of the file or directory, or 247 * {@code null} if this path has zero elements 248 */ getFileName()249 Path getFileName(); 250 251 /** 252 * Returns the <em>parent path</em>, or {@code null} if this path does not 253 * have a parent. 254 * 255 * <p> The parent of this path object consists of this path's root 256 * component, if any, and each element in the path except for the 257 * <em>farthest</em> from the root in the directory hierarchy. This method 258 * does not access the file system; the path or its parent may not exist. 259 * Furthermore, this method does not eliminate special names such as "." 260 * and ".." that may be used in some implementations. On UNIX for example, 261 * the parent of "{@code /a/b/c}" is "{@code /a/b}", and the parent of 262 * {@code "x/y/.}" is "{@code x/y}". This method may be used with the {@link 263 * #normalize normalize} method, to eliminate redundant names, for cases where 264 * <em>shell-like</em> navigation is required. 265 * 266 * <p> If this path has one or more elements, and no root component, then 267 * this method is equivalent to evaluating the expression: 268 * <blockquote><pre> 269 * subpath(0, getNameCount()-1); 270 * </pre></blockquote> 271 * 272 * @return a path representing the path's parent 273 */ getParent()274 Path getParent(); 275 276 /** 277 * Returns the number of name elements in the path. 278 * 279 * @return the number of elements in the path, or {@code 0} if this path 280 * only represents a root component 281 */ getNameCount()282 int getNameCount(); 283 284 /** 285 * Returns a name element of this path as a {@code Path} object. 286 * 287 * <p> The {@code index} parameter is the index of the name element to return. 288 * The element that is <em>closest</em> to the root in the directory hierarchy 289 * has index {@code 0}. The element that is <em>farthest</em> from the root 290 * has index {@link #getNameCount count}{@code -1}. 291 * 292 * @param index 293 * the index of the element 294 * 295 * @return the name element 296 * 297 * @throws IllegalArgumentException 298 * if {@code index} is negative, {@code index} is greater than or 299 * equal to the number of elements, or this path has zero name 300 * elements 301 */ getName(int index)302 Path getName(int index); 303 304 /** 305 * Returns a relative {@code Path} that is a subsequence of the name 306 * elements of this path. 307 * 308 * <p> The {@code beginIndex} and {@code endIndex} parameters specify the 309 * subsequence of name elements. The name that is <em>closest</em> to the root 310 * in the directory hierarchy has index {@code 0}. The name that is 311 * <em>farthest</em> from the root has index {@link #getNameCount 312 * count}{@code -1}. The returned {@code Path} object has the name elements 313 * that begin at {@code beginIndex} and extend to the element at index {@code 314 * endIndex-1}. 315 * 316 * @param beginIndex 317 * the index of the first element, inclusive 318 * @param endIndex 319 * the index of the last element, exclusive 320 * 321 * @return a new {@code Path} object that is a subsequence of the name 322 * elements in this {@code Path} 323 * 324 * @throws IllegalArgumentException 325 * if {@code beginIndex} is negative, or greater than or equal to 326 * the number of elements. If {@code endIndex} is less than or 327 * equal to {@code beginIndex}, or larger than the number of elements. 328 */ subpath(int beginIndex, int endIndex)329 Path subpath(int beginIndex, int endIndex); 330 331 /** 332 * Tests if this path starts with the given path. 333 * 334 * <p> This path <em>starts</em> with the given path if this path's root 335 * component <em>starts</em> with the root component of the given path, 336 * and this path starts with the same name elements as the given path. 337 * If the given path has more name elements than this path then {@code false} 338 * is returned. 339 * 340 * <p> Whether or not the root component of this path starts with the root 341 * component of the given path is file system specific. If this path does 342 * not have a root component and the given path has a root component then 343 * this path does not start with the given path. 344 * 345 * <p> If the given path is associated with a different {@code FileSystem} 346 * to this path then {@code false} is returned. 347 * 348 * @param other 349 * the given path 350 * 351 * @return {@code true} if this path starts with the given path; otherwise 352 * {@code false} 353 */ startsWith(Path other)354 boolean startsWith(Path other); 355 356 /** 357 * Tests if this path starts with a {@code Path}, constructed by converting 358 * the given path string, in exactly the manner specified by the {@link 359 * #startsWith(Path) startsWith(Path)} method. On UNIX for example, the path 360 * "{@code foo/bar}" starts with "{@code foo}" and "{@code foo/bar}". It 361 * does not start with "{@code f}" or "{@code fo}". 362 * 363 * @param other 364 * the given path string 365 * 366 * @return {@code true} if this path starts with the given path; otherwise 367 * {@code false} 368 * 369 * @throws InvalidPathException 370 * If the path string cannot be converted to a Path. 371 */ startsWith(String other)372 boolean startsWith(String other); 373 374 /** 375 * Tests if this path ends with the given path. 376 * 377 * <p> If the given path has <em>N</em> elements, and no root component, 378 * and this path has <em>N</em> or more elements, then this path ends with 379 * the given path if the last <em>N</em> elements of each path, starting at 380 * the element farthest from the root, are equal. 381 * 382 * <p> If the given path has a root component then this path ends with the 383 * given path if the root component of this path <em>ends with</em> the root 384 * component of the given path, and the corresponding elements of both paths 385 * are equal. Whether or not the root component of this path ends with the 386 * root component of the given path is file system specific. If this path 387 * does not have a root component and the given path has a root component 388 * then this path does not end with the given path. 389 * 390 * <p> If the given path is associated with a different {@code FileSystem} 391 * to this path then {@code false} is returned. 392 * 393 * @param other 394 * the given path 395 * 396 * @return {@code true} if this path ends with the given path; otherwise 397 * {@code false} 398 */ endsWith(Path other)399 boolean endsWith(Path other); 400 401 /** 402 * Tests if this path ends with a {@code Path}, constructed by converting 403 * the given path string, in exactly the manner specified by the {@link 404 * #endsWith(Path) endsWith(Path)} method. On UNIX for example, the path 405 * "{@code foo/bar}" ends with "{@code foo/bar}" and "{@code bar}". It does 406 * not end with "{@code r}" or "{@code /bar}". Note that trailing separators 407 * are not taken into account, and so invoking this method on the {@code 408 * Path}"{@code foo/bar}" with the {@code String} "{@code bar/}" returns 409 * {@code true}. 410 * 411 * @param other 412 * the given path string 413 * 414 * @return {@code true} if this path ends with the given path; otherwise 415 * {@code false} 416 * 417 * @throws InvalidPathException 418 * If the path string cannot be converted to a Path. 419 */ endsWith(String other)420 boolean endsWith(String other); 421 422 /** 423 * Returns a path that is this path with redundant name elements eliminated. 424 * 425 * <p> The precise definition of this method is implementation dependent but 426 * in general it derives from this path, a path that does not contain 427 * <em>redundant</em> name elements. In many file systems, the "{@code .}" 428 * and "{@code ..}" are special names used to indicate the current directory 429 * and parent directory. In such file systems all occurrences of "{@code .}" 430 * are considered redundant. If a "{@code ..}" is preceded by a 431 * non-"{@code ..}" name then both names are considered redundant (the 432 * process to identify such names is repeated until it is no longer 433 * applicable). 434 * 435 * <p> This method does not access the file system; the path may not locate 436 * a file that exists. Eliminating "{@code ..}" and a preceding name from a 437 * path may result in the path that locates a different file than the original 438 * path. This can arise when the preceding name is a symbolic link. 439 * 440 * @return the resulting path or this path if it does not contain 441 * redundant name elements; an empty path is returned if this path 442 * does have a root component and all name elements are redundant 443 * 444 * @see #getParent 445 * @see #toRealPath 446 */ normalize()447 Path normalize(); 448 449 // -- resolution and relativization -- 450 451 /** 452 * Resolve the given path against this path. 453 * 454 * <p> If the {@code other} parameter is an {@link #isAbsolute() absolute} 455 * path then this method trivially returns {@code other}. If {@code other} 456 * is an <i>empty path</i> then this method trivially returns this path. 457 * Otherwise this method considers this path to be a directory and resolves 458 * the given path against this path. In the simplest case, the given path 459 * does not have a {@link #getRoot root} component, in which case this method 460 * <em>joins</em> the given path to this path and returns a resulting path 461 * that {@link #endsWith ends} with the given path. Where the given path has 462 * a root component then resolution is highly implementation dependent and 463 * therefore unspecified. 464 * 465 * @param other 466 * the path to resolve against this path 467 * 468 * @return the resulting path 469 * 470 * @see #relativize 471 */ resolve(Path other)472 Path resolve(Path other); 473 474 /** 475 * Converts a given path string to a {@code Path} and resolves it against 476 * this {@code Path} in exactly the manner specified by the {@link 477 * #resolve(Path) resolve} method. For example, suppose that the name 478 * separator is "{@code /}" and a path represents "{@code foo/bar}", then 479 * invoking this method with the path string "{@code gus}" will result in 480 * the {@code Path} "{@code foo/bar/gus}". 481 * 482 * @param other 483 * the path string to resolve against this path 484 * 485 * @return the resulting path 486 * 487 * @throws InvalidPathException 488 * if the path string cannot be converted to a Path. 489 * 490 * @see FileSystem#getPath 491 */ resolve(String other)492 Path resolve(String other); 493 494 /** 495 * Resolves the given path against this path's {@link #getParent parent} 496 * path. This is useful where a file name needs to be <i>replaced</i> with 497 * another file name. For example, suppose that the name separator is 498 * "{@code /}" and a path represents "{@code dir1/dir2/foo}", then invoking 499 * this method with the {@code Path} "{@code bar}" will result in the {@code 500 * Path} "{@code dir1/dir2/bar}". If this path does not have a parent path, 501 * or {@code other} is {@link #isAbsolute() absolute}, then this method 502 * returns {@code other}. If {@code other} is an empty path then this method 503 * returns this path's parent, or where this path doesn't have a parent, the 504 * empty path. 505 * 506 * @param other 507 * the path to resolve against this path's parent 508 * 509 * @return the resulting path 510 * 511 * @see #resolve(Path) 512 */ resolveSibling(Path other)513 Path resolveSibling(Path other); 514 515 /** 516 * Converts a given path string to a {@code Path} and resolves it against 517 * this path's {@link #getParent parent} path in exactly the manner 518 * specified by the {@link #resolveSibling(Path) resolveSibling} method. 519 * 520 * @param other 521 * the path string to resolve against this path's parent 522 * 523 * @return the resulting path 524 * 525 * @throws InvalidPathException 526 * if the path string cannot be converted to a Path. 527 * 528 * @see FileSystem#getPath 529 */ resolveSibling(String other)530 Path resolveSibling(String other); 531 532 /** 533 * Constructs a relative path between this path and a given path. 534 * 535 * <p> Relativization is the inverse of {@link #resolve(Path) resolution}. 536 * This method attempts to construct a {@link #isAbsolute relative} path 537 * that when {@link #resolve(Path) resolved} against this path, yields a 538 * path that locates the same file as the given path. For example, on UNIX, 539 * if this path is {@code "/a/b"} and the given path is {@code "/a/b/c/d"} 540 * then the resulting relative path would be {@code "c/d"}. Where this 541 * path and the given path do not have a {@link #getRoot root} component, 542 * then a relative path can be constructed. A relative path cannot be 543 * constructed if only one of the paths have a root component. Where both 544 * paths have a root component then it is implementation dependent if a 545 * relative path can be constructed. If this path and the given path are 546 * {@link #equals equal} then an <i>empty path</i> is returned. 547 * 548 * <p> For any two {@link #normalize normalized} paths <i>p</i> and 549 * <i>q</i>, where <i>q</i> does not have a root component, 550 * <blockquote> 551 * <i>p</i><tt>.relativize(</tt><i>p</i><tt>.resolve(</tt><i>q</i><tt>)).equals(</tt><i>q</i><tt>)</tt> 552 * </blockquote> 553 * 554 * <p> When symbolic links are supported, then whether the resulting path, 555 * when resolved against this path, yields a path that can be used to locate 556 * the {@link Files#isSameFile same} file as {@code other} is implementation 557 * dependent. For example, if this path is {@code "/a/b"} and the given 558 * path is {@code "/a/x"} then the resulting relative path may be {@code 559 * "../x"}. If {@code "b"} is a symbolic link then is implementation 560 * dependent if {@code "a/b/../x"} would locate the same file as {@code "/a/x"}. 561 * 562 * @param other 563 * the path to relativize against this path 564 * 565 * @return the resulting relative path, or an empty path if both paths are 566 * equal 567 * 568 * @throws IllegalArgumentException 569 * if {@code other} is not a {@code Path} that can be relativized 570 * against this path 571 */ relativize(Path other)572 Path relativize(Path other); 573 574 /** 575 * Returns a URI to represent this path. 576 * 577 * <p> This method constructs an absolute {@link URI} with a {@link 578 * URI#getScheme() scheme} equal to the URI scheme that identifies the 579 * provider. The exact form of the scheme specific part is highly provider 580 * dependent. 581 * 582 * <p> In the case of the default provider, the URI is hierarchical with 583 * a {@link URI#getPath() path} component that is absolute. The query and 584 * fragment components are undefined. Whether the authority component is 585 * defined or not is implementation dependent. There is no guarantee that 586 * the {@code URI} may be used to construct a {@link java.io.File java.io.File}. 587 * In particular, if this path represents a Universal Naming Convention (UNC) 588 * path, then the UNC server name may be encoded in the authority component 589 * of the resulting URI. In the case of the default provider, and the file 590 * exists, and it can be determined that the file is a directory, then the 591 * resulting {@code URI} will end with a slash. 592 * 593 * <p> The default provider provides a similar <em>round-trip</em> guarantee 594 * to the {@link java.io.File} class. For a given {@code Path} <i>p</i> it 595 * is guaranteed that 596 * <blockquote><tt> 597 * {@link Paths#get(URI) Paths.get}(</tt><i>p</i><tt>.toUri()).equals(</tt><i>p</i> 598 * <tt>.{@link #toAbsolutePath() toAbsolutePath}())</tt> 599 * </blockquote> 600 * so long as the original {@code Path}, the {@code URI}, and the new {@code 601 * Path} are all created in (possibly different invocations of) the same 602 * Java virtual machine. Whether other providers make any guarantees is 603 * provider specific and therefore unspecified. 604 * 605 * <p> When a file system is constructed to access the contents of a file 606 * as a file system then it is highly implementation specific if the returned 607 * URI represents the given path in the file system or it represents a 608 * <em>compound</em> URI that encodes the URI of the enclosing file system. 609 * A format for compound URIs is not defined in this release; such a scheme 610 * may be added in a future release. 611 * 612 * @return the URI representing this path 613 * 614 * @throws java.io.IOError 615 * if an I/O error occurs obtaining the absolute path, or where a 616 * file system is constructed to access the contents of a file as 617 * a file system, and the URI of the enclosing file system cannot be 618 * obtained 619 * 620 * @throws SecurityException 621 * In the case of the default provider, and a security manager 622 * is installed, the {@link #toAbsolutePath toAbsolutePath} method 623 * throws a security exception. 624 */ toUri()625 URI toUri(); 626 627 /** 628 * Returns a {@code Path} object representing the absolute path of this 629 * path. 630 * 631 * <p> If this path is already {@link Path#isAbsolute absolute} then this 632 * method simply returns this path. Otherwise, this method resolves the path 633 * in an implementation dependent manner, typically by resolving the path 634 * against a file system default directory. Depending on the implementation, 635 * this method may throw an I/O error if the file system is not accessible. 636 * 637 * @return a {@code Path} object representing the absolute path 638 * 639 * @throws java.io.IOError 640 * if an I/O error occurs 641 * @throws SecurityException 642 * In the case of the default provider, a security manager 643 * is installed, and this path is not absolute, then the security 644 * manager's {@link SecurityManager#checkPropertyAccess(String) 645 * checkPropertyAccess} method is invoked to check access to the 646 * system property {@code user.dir} 647 */ toAbsolutePath()648 Path toAbsolutePath(); 649 650 /** 651 * Returns the <em>real</em> path of an existing file. 652 * 653 * <p> The precise definition of this method is implementation dependent but 654 * in general it derives from this path, an {@link #isAbsolute absolute} 655 * path that locates the {@link Files#isSameFile same} file as this path, but 656 * with name elements that represent the actual name of the directories 657 * and the file. For example, where filename comparisons on a file system 658 * are case insensitive then the name elements represent the names in their 659 * actual case. Additionally, the resulting path has redundant name 660 * elements removed. 661 * 662 * <p> If this path is relative then its absolute path is first obtained, 663 * as if by invoking the {@link #toAbsolutePath toAbsolutePath} method. 664 * 665 * <p> The {@code options} array may be used to indicate how symbolic links 666 * are handled. By default, symbolic links are resolved to their final 667 * target. If the option {@link LinkOption#NOFOLLOW_LINKS NOFOLLOW_LINKS} is 668 * present then this method does not resolve symbolic links. 669 * 670 * Some implementations allow special names such as "{@code ..}" to refer to 671 * the parent directory. When deriving the <em>real path</em>, and a 672 * "{@code ..}" (or equivalent) is preceded by a non-"{@code ..}" name then 673 * an implementation will typically cause both names to be removed. When 674 * not resolving symbolic links and the preceding name is a symbolic link 675 * then the names are only removed if it guaranteed that the resulting path 676 * will locate the same file as this path. 677 * 678 * @param options 679 * options indicating how symbolic links are handled 680 * 681 * @return an absolute path represent the <em>real</em> path of the file 682 * located by this object 683 * 684 * @throws IOException 685 * if the file does not exist or an I/O error occurs 686 * @throws SecurityException 687 * In the case of the default provider, and a security manager 688 * is installed, its {@link SecurityManager#checkRead(String) checkRead} 689 * method is invoked to check read access to the file, and where 690 * this path is not absolute, its {@link SecurityManager#checkPropertyAccess(String) 691 * checkPropertyAccess} method is invoked to check access to the 692 * system property {@code user.dir} 693 */ toRealPath(LinkOption... options)694 Path toRealPath(LinkOption... options) throws IOException; 695 696 /** 697 * Returns a {@link File} object representing this path. Where this {@code 698 * Path} is associated with the default provider, then this method is 699 * equivalent to returning a {@code File} object constructed with the 700 * {@code String} representation of this path. 701 * 702 * <p> If this path was created by invoking the {@code File} {@link 703 * File#toPath toPath} method then there is no guarantee that the {@code 704 * File} object returned by this method is {@link #equals equal} to the 705 * original {@code File}. 706 * 707 * @return a {@code File} object representing this path 708 * 709 * @throws UnsupportedOperationException 710 * if this {@code Path} is not associated with the default provider 711 */ toFile()712 File toFile(); 713 714 // -- watchable -- 715 716 /** 717 * Registers the file located by this path with a watch service. 718 * 719 * <p> In this release, this path locates a directory that exists. The 720 * directory is registered with the watch service so that entries in the 721 * directory can be watched. The {@code events} parameter is the events to 722 * register and may contain the following events: 723 * <ul> 724 * <li>{@link StandardWatchEventKinds#ENTRY_CREATE ENTRY_CREATE} - 725 * entry created or moved into the directory</li> 726 * <li>{@link StandardWatchEventKinds#ENTRY_DELETE ENTRY_DELETE} - 727 * entry deleted or moved out of the directory</li> 728 * <li>{@link StandardWatchEventKinds#ENTRY_MODIFY ENTRY_MODIFY} - 729 * entry in directory was modified</li> 730 * </ul> 731 * 732 * <p> The {@link WatchEvent#context context} for these events is the 733 * relative path between the directory located by this path, and the path 734 * that locates the directory entry that is created, deleted, or modified. 735 * 736 * <p> The set of events may include additional implementation specific 737 * event that are not defined by the enum {@link StandardWatchEventKinds} 738 * 739 * <p> The {@code modifiers} parameter specifies <em>modifiers</em> that 740 * qualify how the directory is registered. This release does not define any 741 * <em>standard</em> modifiers. It may contain implementation specific 742 * modifiers. 743 * 744 * <p> Where a file is registered with a watch service by means of a symbolic 745 * link then it is implementation specific if the watch continues to depend 746 * on the existence of the symbolic link after it is registered. 747 * 748 * @param watcher 749 * the watch service to which this object is to be registered 750 * @param events 751 * the events for which this object should be registered 752 * @param modifiers 753 * the modifiers, if any, that modify how the object is registered 754 * 755 * @return a key representing the registration of this object with the 756 * given watch service 757 * 758 * @throws UnsupportedOperationException 759 * if unsupported events or modifiers are specified 760 * @throws IllegalArgumentException 761 * if an invalid combination of events or modifiers is specified 762 * @throws ClosedWatchServiceException 763 * if the watch service is closed 764 * @throws NotDirectoryException 765 * if the file is registered to watch the entries in a directory 766 * and the file is not a directory <i>(optional specific exception)</i> 767 * @throws IOException 768 * if an I/O error occurs 769 * @throws SecurityException 770 * In the case of the default provider, and a security manager is 771 * installed, the {@link SecurityManager#checkRead(String) checkRead} 772 * method is invoked to check read access to the file. 773 */ 774 @Override register(WatchService watcher, WatchEvent.Kind<?>[] events, WatchEvent.Modifier... modifiers)775 WatchKey register(WatchService watcher, 776 WatchEvent.Kind<?>[] events, 777 WatchEvent.Modifier... modifiers) 778 throws IOException; 779 780 /** 781 * Registers the file located by this path with a watch service. 782 * 783 * <p> An invocation of this method behaves in exactly the same way as the 784 * invocation 785 * <pre> 786 * watchable.{@link #register(WatchService,WatchEvent.Kind[],WatchEvent.Modifier[]) register}(watcher, events, new WatchEvent.Modifier[0]); 787 * </pre> 788 * 789 * <p> <b>Usage Example:</b> 790 * Suppose we wish to register a directory for entry create, delete, and modify 791 * events: 792 * <pre> 793 * Path dir = ... 794 * WatchService watcher = ... 795 * 796 * WatchKey key = dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY); 797 * </pre> 798 * @param watcher 799 * The watch service to which this object is to be registered 800 * @param events 801 * The events for which this object should be registered 802 * 803 * @return A key representing the registration of this object with the 804 * given watch service 805 * 806 * @throws UnsupportedOperationException 807 * If unsupported events are specified 808 * @throws IllegalArgumentException 809 * If an invalid combination of events is specified 810 * @throws ClosedWatchServiceException 811 * If the watch service is closed 812 * @throws NotDirectoryException 813 * If the file is registered to watch the entries in a directory 814 * and the file is not a directory <i>(optional specific exception)</i> 815 * @throws IOException 816 * If an I/O error occurs 817 * @throws SecurityException 818 * In the case of the default provider, and a security manager is 819 * installed, the {@link SecurityManager#checkRead(String) checkRead} 820 * method is invoked to check read access to the file. 821 */ 822 @Override register(WatchService watcher, WatchEvent.Kind<?>... events)823 WatchKey register(WatchService watcher, 824 WatchEvent.Kind<?>... events) 825 throws IOException; 826 827 // -- Iterable -- 828 829 /** 830 * Returns an iterator over the name elements of this path. 831 * 832 * <p> The first element returned by the iterator represents the name 833 * element that is closest to the root in the directory hierarchy, the 834 * second element is the next closest, and so on. The last element returned 835 * is the name of the file or directory denoted by this path. The {@link 836 * #getRoot root} component, if present, is not returned by the iterator. 837 * 838 * @return an iterator over the name elements of this path. 839 */ 840 @Override iterator()841 Iterator<Path> iterator(); 842 843 // -- compareTo/equals/hashCode -- 844 845 /** 846 * Compares two abstract paths lexicographically. The ordering defined by 847 * this method is provider specific, and in the case of the default 848 * provider, platform specific. This method does not access the file system 849 * and neither file is required to exist. 850 * 851 * <p> This method may not be used to compare paths that are associated 852 * with different file system providers. 853 * 854 * @param other the path compared to this path. 855 * 856 * @return zero if the argument is {@link #equals equal} to this path, a 857 * value less than zero if this path is lexicographically less than 858 * the argument, or a value greater than zero if this path is 859 * lexicographically greater than the argument 860 * 861 * @throws ClassCastException 862 * if the paths are associated with different providers 863 */ 864 @Override compareTo(Path other)865 int compareTo(Path other); 866 867 /** 868 * Tests this path for equality with the given object. 869 * 870 * <p> If the given object is not a Path, or is a Path associated with a 871 * different {@code FileSystem}, then this method returns {@code false}. 872 * 873 * <p> Whether or not two path are equal depends on the file system 874 * implementation. In some cases the paths are compared without regard 875 * to case, and others are case sensitive. This method does not access the 876 * file system and the file is not required to exist. Where required, the 877 * {@link Files#isSameFile isSameFile} method may be used to check if two 878 * paths locate the same file. 879 * 880 * <p> This method satisfies the general contract of the {@link 881 * java.lang.Object#equals(Object) Object.equals} method. </p> 882 * 883 * @param other 884 * the object to which this object is to be compared 885 * 886 * @return {@code true} if, and only if, the given object is a {@code Path} 887 * that is identical to this {@code Path} 888 */ equals(Object other)889 boolean equals(Object other); 890 891 /** 892 * Computes a hash code for this path. 893 * 894 * <p> The hash code is based upon the components of the path, and 895 * satisfies the general contract of the {@link Object#hashCode 896 * Object.hashCode} method. 897 * 898 * @return the hash-code value for this path 899 */ hashCode()900 int hashCode(); 901 902 /** 903 * Returns the string representation of this path. 904 * 905 * <p> If this path was created by converting a path string using the 906 * {@link FileSystem#getPath getPath} method then the path string returned 907 * by this method may differ from the original String used to create the path. 908 * 909 * <p> The returned path string uses the default name {@link 910 * FileSystem#getSeparator separator} to separate names in the path. 911 * 912 * @return the string representation of this path 913 */ toString()914 String toString(); 915 } 916