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