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