1 /* 2 * Copyright (c) 2007, 2010, 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.nio.file.attribute.*; 29 import java.nio.channels.SeekableByteChannel; 30 import java.io.IOException; 31 import java.io.OutputStream; 32 import java.net.URI; 33 import java.util.Iterator; 34 import java.util.Set; 35 36 /** 37 * A file reference that locates a file using a system dependent path. The file 38 * is not required to exist. 39 * 40 * <p> On many platforms a <em>path</em> is the means to locate and access files 41 * in a file system. A path is hierarchical and composed of a sequence of 42 * directory and file name elements separated by a special separator or 43 * delimiter. 44 * 45 * <h4>Path operations</h4> 46 * 47 * <p> A system dependent path represented by this class is conceptually a 48 * sequence of name elements and optionally a <em>root component</em>. The name 49 * that is <em>farthest</em> from the root of the directory hierarchy is the 50 * name of a file or directory. The other elements are directory names. The root 51 * component typically identifies a file system hierarchy. A {@code Path} can 52 * represent a root, a root and a sequence of names, or simply one or more name 53 * elements. It defines the {@link #getName() getName}, {@link #getParent 54 * getParent}, {@link #getRoot getRoot}, and {@link #subpath subpath} methods 55 * to access the components or a subsequence of its name elements. 56 * 57 * <p> In addition to accessing the components of a path, a {@code Path} also 58 * defines {@link #resolve(Path) resolve} and {@link #relativize relativize} 59 * operations. Paths can also be {@link #compareTo compared}, and tested 60 * against each other using using the {@link #startsWith startsWith} and {@link 61 * #endsWith endWith} methods. 62 * 63 * <h4>File operations</h4> 64 * 65 * <p> A {@code Path} is either <em>absolute</em> or <em>relative</em>. An 66 * absolute path is complete in that does not need to be combined with another 67 * path in order to locate a file. All operations on relative paths are first 68 * resolved against a file system's default directory as if by invoking the 69 * {@link #toAbsolutePath toAbsolutePath} method. 70 * 71 * <p> In addition to the operations defined by the {@link FileRef} interface, 72 * this class defines the following operations: 73 * 74 * <ul> 75 * <li><p> The {@link #newByteChannel newByteChannel} method 76 * may be used to open a file and obtain a byte channel for reading or 77 * writing. </p></li> 78 * <li><p> Files may be {@link #createFile(FileAttribute[]) created}, or 79 * directories may be {@link #createDirectory(FileAttribute[]) created}. 80 * </p></li> 81 * <li><p> The {@link #delete delete} method may be used to delete a file. 82 * </p></li> 83 * <li><p> The {@link #checkAccess checkAccess} method may be used to check 84 * the existence or accessibility of a file. </p></li> 85 * <li><p> The {@link #isSameFile isSameFile} method may be used to test if 86 * two file references locate the same file. </p></li> 87 * <li><p> The {@link #getFileStore getFileStore} method may be used to 88 * obtain the {@link FileStore} representing the storage where a file is 89 * located. </p></li> 90 * <li><p> Directories can be {@link #newDirectoryStream opened} so as to 91 * iterate over the entries in the directory. </p></li> 92 * <li><p> Files can be {@link #copyTo(Path,CopyOption[]) copied} or 93 * {@link #moveTo(Path,CopyOption[]) moved}. </p></li> 94 * <li><p> Symbolic links may be {@link #createSymbolicLink created}, or the 95 * target of a symbolic link may be {@link #readSymbolicLink read}. </p></li> 96 * <li><p> The {@link #toRealPath real} path of an existing file may be 97 * obtained. </li></p> 98 * </ul> 99 * 100 * <p> This class implements {@link Watchable} interface so that a directory 101 * located by a path can be {@link #register registered} with a {@link WatchService}. 102 * and entries in the directory watched. 103 * 104 * <h4>File attributes</h4> 105 * 106 * In addition to the {@link #setAttribute setAttribute} and {@link #getAttribute 107 * getAttribute} methods, the <a href="attribute/package-summary.html">{@code 108 * java.nio.file.attribute}</a> package provides type-safe and efficient access 109 * to file attributes or <em>meta-data</em> associated with files. The {@link 110 * Attributes Attributes} class defines methods that operate on or return file 111 * attributes. For example, the file type, size, timestamps, and other 112 * <em>basic</em> meta-data are obtained, in bulk, by invoking the {@link 113 * Attributes#readBasicFileAttributes Attributes.readBasicFileAttributes} method: 114 * <pre> 115 * Path file = ... 116 * BasicFileAttributes attrs = Attributes.readBasicFileAttributes(file); 117 * </pre> 118 * 119 * <a name="interop"><h4>Interoperability</h4></a> 120 * 121 * <p> Paths created by file systems associated with the default {@link 122 * java.nio.file.spi.FileSystemProvider provider} are generally interoperable 123 * with the {@link java.io.File java.io.File} class. Paths created by other 124 * providers are unlikely to be interoperable with the abstract path names 125 * represented by {@code java.io.File}. The {@link java.io.File#toPath 126 * File.toPath} method may be used to obtain a {@code Path} from the abstract 127 * path name represented by a {@code java.io.File java.io.File} object. The 128 * resulting {@code Path} can be used to operate on the same file as the {@code 129 * java.io.File} object. 130 * 131 * <p> Path objects created by file systems associated with the default 132 * provider are interoperable with objects created by other file systems created 133 * by the same provider. Path objects created by file systems associated with 134 * other providers may not be interoperable with other file systems created by 135 * the same provider. The reasons for this are provider specific. 136 * 137 * <h4>Concurrency</h4></a> 138 * 139 * <p> Instances of this class are immutable and safe for use by multiple concurrent 140 * threads. 141 * 142 * @since 1.7 143 */ 144 145 public abstract class Path 146 implements FileRef, Comparable<Path>, Iterable<Path>, Watchable 147 { 148 /** 149 * Initializes a new instance of this class. 150 */ 151 protected Path() { } 152 153 /** 154 * Returns the file system that created this object. 155 * 156 * @return the file system that created this object 157 */ 158 public abstract FileSystem getFileSystem(); 159 160 /** 161 * Tells whether or not this path is absolute. 162 * 163 * <p> An absolute path is complete in that it doesn't need to be 164 * combined with other path information in order to locate a file. 165 * 166 * @return {@code true} if, and only if, this path is absolute 167 */ 168 public abstract boolean isAbsolute(); 169 170 /** 171 * Returns the root component of this path as a {@code Path} object, 172 * or {@code null} if this path does not have a root component. 173 * 174 * @return a path representing the root component of this path, 175 * or {@code null} 176 */ 177 public abstract Path getRoot(); 178 179 /** 180 * Returns the name of the file or directory denoted by this path. The 181 * file name is the <em>farthest</em> element from the root in the directory 182 * hierarchy. 183 * 184 * @return a path representing the name of the file or directory, or 185 * {@code null} if this path has zero elements 186 */ 187 public abstract Path getName(); 188 189 /** 190 * Returns the <em>parent path</em>, or {@code null} if this path does not 191 * have a parent. 192 * 193 * <p> The parent of this path object consists of this path's root 194 * component, if any, and each element in the path except for the 195 * <em>farthest</em> from the root in the directory hierarchy. This method 196 * does not access the file system; the path or its parent may not exist. 197 * Furthermore, this method does not eliminate special names such as "." 198 * and ".." that may be used in some implementations. On UNIX for example, 199 * the parent of "{@code /a/b/c}" is "{@code /a/b}", and the parent of 200 * {@code "x/y/.}" is "{@code x/y}". This method may be used with the {@link 201 * #normalize normalize} method, to eliminate redundant names, for cases where 202 * <em>shell-like</em> navigation is required. 203 * 204 * <p> If this path has one or more elements, and no root component, then 205 * this method is equivalent to evaluating the expression: 206 * <blockquote><pre> 207 * subpath(0, getNameCount()-1); 208 * </pre></blockquote> 209 * 210 * @return a path representing the path's parent 211 */ 212 public abstract Path getParent(); 213 214 /** 215 * Returns the number of name elements in the path. 216 * 217 * @return the number of elements in the path, or {@code 0} if this path 218 * only represents a root component 219 */ 220 public abstract int getNameCount(); 221 222 /** 223 * Returns a name element of this path. 224 * 225 * <p> The {@code index} parameter is the index of the name element to return. 226 * The element that is <em>closest</em> to the root in the directory hierarchy 227 * has index {@code 0}. The element that is <em>farthest</em> from the root 228 * has index {@link #getNameCount count}{@code -1}. 229 * 230 * @param index 231 * the index of the element 232 * 233 * @return the name element 234 * 235 * @throws IllegalArgumentException 236 * if {@code index} is negative, {@code index} is greater than or 237 * equal to the number of elements, or this path has zero name 238 * elements 239 */ 240 public abstract Path getName(int index); 241 242 /** 243 * Returns a relative {@code Path} that is a subsequence of the name 244 * elements of this path. 245 * 246 * <p> The {@code beginIndex} and {@code endIndex} parameters specify the 247 * subsequence of name elements. The name that is <em>closest</em> to the root 248 * in the directory hierarchy has index {@code 0}. The name that is 249 * <em>farthest</em> from the root has index {@link #getNameCount 250 * count}{@code -1}. The returned {@code Path} object has the name elements 251 * that begin at {@code beginIndex} and extend to the element at index {@code 252 * endIndex-1}. 253 * 254 * @param beginIndex 255 * the index of the first element, inclusive 256 * @param endIndex 257 * the index of the last element, exclusive 258 * 259 * @return a new {@code Path} object that is a subsequence of the name 260 * elements in this {@code Path} 261 * 262 * @throws IllegalArgumentException 263 * if {@code beginIndex} is negative, or greater than or equal to 264 * the number of elements. If {@code endIndex} is less than or 265 * equal to {@code beginIndex}, or larger than the number of elements. 266 */ 267 public abstract Path subpath(int beginIndex, int endIndex); 268 269 /** 270 * Tests if this path starts with the given path. 271 * 272 * <p> This path <em>starts</em> with the given path if this path's root 273 * component <em>starts</em> with the root component of the given path, 274 * and this path starts with the same name elements as the given path. 275 * If the given path has more name elements than this path then {@code false} 276 * is returned. 277 * 278 * <p> Whether or not the root component of this path starts with the root 279 * component of the given path is file system specific. If this path does 280 * not have a root component and the given path has a root component then 281 * this path does not start with the given path. 282 * 283 * @param other 284 * the given path 285 * 286 * @return {@code true} if this path starts with the given path; otherwise 287 * {@code false} 288 */ 289 public abstract boolean startsWith(Path other); 290 291 /** 292 * Tests if this path ends with the given path. 293 * 294 * <p> If the given path has <em>N</em> elements, and no root component, 295 * and this path has <em>N</em> or more elements, then this path ends with 296 * the given path if the last <em>N</em> elements of each path, starting at 297 * the element farthest from the root, are equal. 298 * 299 * <p> If the given path has a root component then this path ends with the 300 * given path if the root component of this path <em>ends with</em> the root 301 * component of the given path, and the corresponding elements of both paths 302 * are equal. Whether or not the root component of this path ends with the 303 * root component of the given path is file system specific. If this path 304 * does not have a root component and the given path has a root component 305 * then this path does not end with the given path. 306 * 307 * @param other 308 * the given path 309 * 310 * @return {@code true} if this path ends with the given path; otherwise 311 * {@code false} 312 */ 313 public abstract boolean endsWith(Path other); 314 315 /** 316 * Returns a path that is this path with redundant name elements eliminated. 317 * 318 * <p> The precise definition of this method is implementation dependent but 319 * in general it derives from this path, a path that does not contain 320 * <em>redundant</em> name elements. In many file systems, the "{@code .}" 321 * and "{@code ..}" are special names used to indicate the current directory 322 * and parent directory. In such file systems all occurrences of "{@code .}" 323 * are considered redundant. If a "{@code ..}" is preceded by a 324 * non-"{@code ..}" name then both names are considered redundant (the 325 * process to identify such names is repeated until is it no longer 326 * applicable). 327 * 328 * <p> This method does not access the file system; the path may not locate 329 * a file that exists. Eliminating "{@code ..}" and a preceding name from a 330 * path may result in the path that locates a different file than the original 331 * path. This can arise when the preceding name is a symbolic link. 332 * 333 * @return the resulting path, or this path if it does not contain 334 * redundant name elements, or {@code null} if this path does not 335 * have a root component and all name elements are redundant 336 * 337 * @see #getParent 338 * @see #toRealPath 339 */ 340 public abstract Path normalize(); 341 342 // -- resolution and relativization -- 343 344 /** 345 * Resolve the given path against this path. 346 * 347 * <p> If the {@code other} parameter is an {@link #isAbsolute() absolute} 348 * path then this method trivially returns {@code other}. If {@code other} 349 * is {@code null} then this path is returned. Otherwise this method 350 * considers this path to be a directory and resolves the given path 351 * against this path. In the simplest case, the given path does not have 352 * a {@link #getRoot root} component, in which case this method <em>joins</em> 353 * the given path to this path and returns a resulting path that {@link 354 * #endsWith ends} with the given path. Where the given path has a root 355 * component then resolution is highly implementation dependent and therefore 356 * unspecified. 357 * 358 * @param other 359 * the path to resolve against this path; can be {@code null} 360 * 361 * @return the resulting path 362 * 363 * @see #relativize 364 */ 365 public abstract Path resolve(Path other); 366 367 /** 368 * Converts a given path string to a {@code Path} and resolves it against 369 * this {@code Path} in exactly the manner specified by the {@link 370 * #resolve(Path) resolve} method. 371 * 372 * @param other 373 * the path string to resolve against this path 374 * 375 * @return the resulting path 376 * 377 * @throws InvalidPathException 378 * If the path string cannot be converted to a Path. 379 * 380 * @see FileSystem#getPath 381 */ 382 public abstract Path resolve(String other); 383 384 /** 385 * Constructs a relative path between this path and a given path. 386 * 387 * <p> Relativization is the inverse of {@link #resolve(Path) resolution}. 388 * This method attempts to construct a {@link #isAbsolute relative} path 389 * that when {@link #resolve(Path) resolved} against this path, yields a 390 * path that locates the same file as the given path. For example, on UNIX, 391 * if this path is {@code "/a/b"} and the given path is {@code "/a/b/c/d"} 392 * then the resulting relative path would be {@code "c/d"}. Where this 393 * path and the given path do not have a {@link #getRoot root} component, 394 * then a relative path can be constructed. A relative path cannot be 395 * constructed if only one of the paths have a root component. Where both 396 * paths have a root component then it is implementation dependent if a 397 * relative path can be constructed. If this path and the given path are 398 * {@link #equals equal} then {@code null} is returned. 399 * 400 * <p> For any two paths <i>p</i> and <i>q</i>, where <i>q</i> does not have 401 * a root component, 402 * <blockquote> 403 * <i>p</i><tt>.relativize(</tt><i>p</i><tt>.resolve(</tt><i>q</i><tt>)).equals(</tt><i>q</i><tt>)</tt> 404 * </blockquote> 405 * 406 * <p> When symbolic links are supported, then whether the resulting path, 407 * when resolved against this path, yields a path that can be used to locate 408 * the {@link #isSameFile same} file as {@code other} is implementation 409 * dependent. For example, if this path is {@code "/a/b"} and the given 410 * path is {@code "/a/x"} then the resulting relative path may be {@code 411 * "../x"}. If {@code "b"} is a symbolic link then is implementation 412 * dependent if {@code "a/b/../x"} would locate the same file as {@code "/a/x"}. 413 * 414 * @param other 415 * the path to relativize against this path 416 * 417 * @return the resulting relative path, or {@code null} if both paths are 418 * equal 419 * 420 * @throws IllegalArgumentException 421 * if {@code other} is not a {@code Path} that can be relativized 422 * against this path 423 */ 424 public abstract Path relativize(Path other); 425 426 // -- file operations -- 427 428 /** 429 * Deletes the file located by this path. 430 * 431 * <p> An implementation may require to examine the file to determine if the 432 * file is a directory. Consequently this method may not be atomic with respect 433 * to other file system operations. If the file is a symbolic link then the 434 * symbolic link itself, not the final target of the link, is deleted. 435 * 436 * <p> If the file is a directory then the directory must be empty. In some 437 * implementations a directory has entries for special files or links that 438 * are created when the directory is created. In such implementations a 439 * directory is considered empty when only the special entries exist. 440 * 441 * <p> On some operating systems it may not be possible to remove a file when 442 * it is open and in use by this Java virtual machine or other programs. 443 * 444 * @throws NoSuchFileException 445 * if the file does not exist <i>(optional specific exception)</i> 446 * @throws DirectoryNotEmptyException 447 * if the file is a directory and could not otherwise be deleted 448 * because the directory is not empty <i>(optional specific 449 * exception)</i> 450 * @throws IOException 451 * if an I/O error occurs 452 * @throws SecurityException 453 * In the case of the default provider, and a security manager is 454 * installed, the {@link SecurityManager#checkDelete(String)} method 455 * is invoked to check delete access to the file 456 */ 457 public abstract void delete() throws IOException; 458 459 /** 460 * Deletes the file located by this path, if it exists. 461 * 462 * <p> As with the {@link #delete delete()} method, an implementation may 463 * need to examine the file to determine if the file is a directory. 464 * Consequently this method may not be atomic with respect to other file 465 * system operations. If the file is a symbolic link, then the symbolic 466 * link itself, not the final target of the link, is deleted. 467 * 468 * <p> If the file is a directory then the directory must be empty. In some 469 * implementations a directory has entries for special files or links that 470 * are created when the directory is created. In such implementations a 471 * directory is considered empty when only the special entries exist. 472 * 473 * <p> On some operating systems it may not be possible to remove a file when 474 * it is open and in use by this Java virtual machine or other programs. 475 * 476 * @throws DirectoryNotEmptyException 477 * if the file is a directory and could not otherwise be deleted 478 * because the directory is not empty <i>(optional specific 479 * exception)</i> 480 * @throws IOException 481 * if an I/O error occurs 482 * @throws SecurityException 483 * In the case of the default provider, and a security manager is 484 * installed, the {@link SecurityManager#checkDelete(String)} method 485 * is invoked to check delete access to the file. 486 */ 487 public abstract void deleteIfExists() throws IOException; 488 489 /** 490 * Creates a symbolic link to a target <i>(optional operation)</i>. 491 * 492 * <p> The {@code target} parameter is the target of the link. It may be an 493 * {@link Path#isAbsolute absolute} or relative path and may not exist. When 494 * the target is a relative path then file system operations on the resulting 495 * link are relative to the path of the link. 496 * 497 * <p> The {@code attrs} parameter is an optional array of {@link FileAttribute 498 * attributes} to set atomically when creating the link. Each attribute is 499 * identified by its {@link FileAttribute#name name}. If more than one attribute 500 * of the same name is included in the array then all but the last occurrence 501 * is ignored. 502 * 503 * <p> Where symbolic links are supported, but the underlying {@link FileStore} 504 * does not support symbolic links, then this may fail with an {@link 505 * IOException}. Additionally, some operating systems may require that the 506 * Java virtual machine be started with implementation specific privileges to 507 * create symbolic links, in which case this method may throw {@code IOException}. 508 * 509 * @param target 510 * the target of the symbolic link 511 * @param attrs 512 * the array of attributes to set atomically when creating the 513 * symbolic link 514 * 515 * @return this path 516 * 517 * @throws UnsupportedOperationException 518 * if the implementation does not support symbolic links or the 519 * array contains an attribute that cannot be set atomically when 520 * creating the symbolic link 521 * @throws FileAlreadyExistsException 522 * if a file with the name already exists <i>(optional specific 523 * exception)</i> 524 * @throws IOException 525 * if an I/O error occurs 526 * @throws SecurityException 527 * In the case of the default provider, and a security manager 528 * is installed, it denies {@link LinkPermission}<tt>("symbolic")</tt> 529 * or its {@link SecurityManager#checkWrite(String) checkWrite} 530 * method denies write access to the path of the symbolic link. 531 */ 532 public abstract Path createSymbolicLink(Path target, FileAttribute<?>... attrs) 533 throws IOException; 534 535 /** 536 * Creates a new link (directory entry) for an existing file <i>(optional 537 * operation)</i>. 538 * 539 * <p> This path locates the directory entry to create. The {@code existing} 540 * parameter is the path to an existing file. This method creates a new 541 * directory entry for the file so that it can be accessed using this path. 542 * On some file systems this is known as creating a "hard link". Whether the 543 * file attributes are maintained for the file or for each directory entry 544 * is file system specific and therefore not specified. Typically, a file 545 * system requires that all links (directory entries) for a file be on the 546 * same file system. Furthermore, on some platforms, the Java virtual machine 547 * may require to be started with implementation specific privileges to 548 * create hard links or to create links to directories. 549 * 550 * @param existing 551 * a reference to an existing file 552 * 553 * @return this path 554 * 555 * @throws UnsupportedOperationException 556 * if the implementation does not support adding an existing file 557 * to a directory 558 * @throws FileAlreadyExistsException 559 * if the entry could not otherwise be created because a file of 560 * that name already exists <i>(optional specific exception)</i> 561 * @throws IOException 562 * if an I/O error occurs 563 * @throws SecurityException 564 * In the case of the default provider, and a security manager 565 * is installed, it denies {@link LinkPermission}<tt>("hard")</tt> 566 * or its {@link SecurityManager#checkWrite(String) checkWrite} 567 * method denies write access to both this path and the path of the 568 * existing file. 569 */ 570 public abstract Path createLink(Path existing) throws IOException; 571 572 /** 573 * Reads the target of a symbolic link <i>(optional operation)</i>. 574 * 575 * <p> If the file system supports <a href="package-summary.html#links">symbolic 576 * links</a> then this method is used to read the target of the link, failing 577 * if the file is not a symbolic link. The target of the link need not exist. 578 * The returned {@code Path} object will be associated with the same file 579 * system as this {@code Path}. 580 * 581 * @return a {@code Path} object representing the target of the link 582 * 583 * @throws UnsupportedOperationException 584 * if the implementation does not support symbolic links 585 * @throws NotLinkException 586 * if the target could otherwise not be read because the file 587 * is not a symbolic link <i>(optional specific exception)</i> 588 * @throws IOException 589 * if an I/O error occurs 590 * @throws SecurityException 591 * In the case of the default provider, and a security manager 592 * is installed, it checks that {@code FilePermission} has been 593 * granted with the "{@code readlink}" action to read the link. 594 */ 595 public abstract Path readSymbolicLink() throws IOException; 596 597 /** 598 * Returns a URI to represent this path. 599 * 600 * <p> This method constructs a hierarchical {@link URI} that is absolute 601 * with a non-empty path component. Its {@link URI#getScheme() scheme} is 602 * equal to the URI scheme that identifies the provider. The exact form of 603 * the other URI components is highly provider dependent. In particular, it 604 * is implementation dependent if its query, fragment, and authority 605 * components are defined or undefined. 606 * 607 * <p> For the default provider the {@link URI#getPath() path} component 608 * will represent the {@link #toAbsolutePath absolute} path; the query, 609 * fragment components are undefined. Whether the authority component is 610 * defined or not is implementation dependent. There is no guarantee that 611 * the {@code URI} may be used to construct a {@link java.io.File java.io.File}. 612 * In particular, if this path represents a Universal Naming Convention (UNC) 613 * path, then the UNC server name may be encoded in the authority component 614 * of the resulting URI. In the case of the default provider, and the file 615 * exists, and it can be determined that the file is a directory, then the 616 * resulting {@code URI} will end with a slash. 617 * 618 * <p> The default provider provides a similar <em>round-trip</em> guarantee 619 * to the {@link java.io.File} class. For a given {@code Path} <i>p</i> it 620 * is guaranteed that 621 * <blockquote><tt> 622 * {@link Paths#get(URI) Paths.get}(</tt><i>p</i><tt>.toUri()).equals(</tt><i>p</i> 623 * <tt>.{@link #toAbsolutePath() toAbsolutePath}())</tt> 624 * </blockquote> 625 * so long as the original {@code Path}, the {@code URI}, and the new {@code 626 * Path} are all created in (possibly different invocations of) the same 627 * Java virtual machine. Whether other providers make any guarantees is 628 * provider specific and therefore unspecified. 629 * 630 * <p> When a file system is constructed to access the contents of a file 631 * as a file system then it is highly implementation specific if the returned 632 * URI represents the given path in the file system or it represents a 633 * <em>compound</em> URI that encodes the URI of the enclosing file system. 634 * A format for compound URIs is not defined in this release; such a scheme 635 * may be added in a future release. 636 * 637 * @return an absolute, hierarchical URI with a non-empty path component 638 * 639 * @throws java.io.IOError 640 * if an I/O error occurs obtaining the absolute path, or where a 641 * file system is constructed to access the contents of a file as 642 * a file system, and the URI of the enclosing file system cannot be 643 * obtained 644 * 645 * @throws SecurityException 646 * In the case of the default provider, and a security manager 647 * is installed, the {@link #toAbsolutePath toAbsolutePath} method 648 * throws a security exception. 649 */ 650 public abstract URI toUri(); 651 652 /** 653 * Returns a {@code Path} object representing the absolute path of this 654 * path. 655 * 656 * <p> If this path is already {@link Path#isAbsolute absolute} then this 657 * method simply returns this path. Otherwise, this method resolves the path 658 * in an implementation dependent manner, typically by resolving the path 659 * against a file system default directory. Depending on the implementation, 660 * this method may throw an I/O error if the file system is not accessible. 661 * 662 * @return a {@code Path} object representing the absolute path 663 * 664 * @throws IOError 665 * if an I/O error occurs 666 * @throws SecurityException 667 * In the case of the default provider, a security manager 668 * is installed, and this path is not absolute, then the security 669 * manager's {@link SecurityManager#checkPropertyAccess(String) 670 * checkPropertyAccess} method is invoked to check access to the 671 * system property {@code user.dir} 672 */ 673 public abstract Path toAbsolutePath(); 674 675 /** 676 * Returns the <em>real</em> path of an existing file. 677 * 678 * <p> The precise definition of this method is implementation dependent but 679 * in general it derives from this path, an {@link #isAbsolute absolute} 680 * path that locates the {@link #isSameFile same} file as this path, but 681 * with name elements that represent the actual name of the directories 682 * and the file. For example, where filename comparisons on a file system 683 * are case insensitive then the name elements represent the names in their 684 * actual case. Additionally, the resulting path has redundant name 685 * elements removed. 686 * 687 * <p> If this path is relative then its absolute path is first obtained, 688 * as if by invoking the {@link #toAbsolutePath toAbsolutePath} method. 689 * 690 * <p> The {@code resolveLinks} parameter specifies if symbolic links 691 * should be resolved. This parameter is ignored when symbolic links are 692 * not supported. Where supported, and the parameter has the value {@code 693 * true} then symbolic links are resolved to their final target. Where the 694 * parameter has the value {@code false} then this method does not resolve 695 * symbolic links. Some implementations allow special names such as 696 * "{@code ..}" to refer to the parent directory. When deriving the <em>real 697 * path</em>, and a "{@code ..}" (or equivalent) is preceded by a 698 * non-"{@code ..}" name then an implementation will typically causes both 699 * names to be removed. When not resolving symbolic links and the preceding 700 * name is a symbolic link then the names are only removed if it guaranteed 701 * that the resulting path will locate the same file as this path. 702 * 703 * @return an absolute path represent the <em>real</em> path of the file 704 * located by this object 705 * 706 * @throws IOException 707 * if the file does not exist or an I/O error occurs 708 * @throws SecurityException 709 * In the case of the default provider, and a security manager 710 * is installed, its {@link SecurityManager#checkRead(String) checkRead} 711 * method is invoked to check read access to the file, and where 712 * this path is not absolute, its {@link SecurityManager#checkPropertyAccess(String) 713 * checkPropertyAccess} method is invoked to check access to the 714 * system property {@code user.dir} 715 */ 716 public abstract Path toRealPath(boolean resolveLinks) throws IOException; 717 718 /** 719 * Copy the file located by this path to a target location. 720 * 721 * <p> This method copies the file located by this {@code Path} to the 722 * target location with the {@code options} parameter specifying how the 723 * copy is performed. By default, the copy fails if the target file already 724 * exists, except if the source and target are the {@link #isSameFile same} 725 * file, in which case this method has no effect. File attributes are not 726 * required to be copied to the target file. If symbolic links are supported, 727 * and the file is a symbolic link, then the final target of the link is copied. 728 * If the file is a directory then it creates an empty directory in the target 729 * location (entries in the directory are not copied). This method can be 730 * used with the {@link Files#walkFileTree Files.walkFileTree} utility 731 * method to copy a directory and all entries in the directory, or an entire 732 * <i>file-tree</i> where required. 733 * 734 * <p> The {@code options} parameter is an array of options and may contain 735 * any of the following: 736 * 737 * <table border=1 cellpadding=5 summary=""> 738 * <tr> <th>Option</th> <th>Description</th> </tr> 739 * <tr> 740 * <td> {@link StandardCopyOption#REPLACE_EXISTING REPLACE_EXISTING} </td> 741 * <td> If the target file exists, then the target file is replaced if it 742 * is not a non-empty directory. If the target file exists and is a 743 * symbolic link, then the symbolic link itself, not the target of 744 * the link, is replaced. </td> 745 * </tr> 746 * <tr> 747 * <td> {@link StandardCopyOption#COPY_ATTRIBUTES COPY_ATTRIBUTES} </td> 748 * <td> Attempts to copy the file attributes associated with this file to 749 * the target file. The exact file attributes that are copied is platform 750 * and file system dependent and therefore unspecified. Minimally, the 751 * {@link BasicFileAttributes#lastModifiedTime last-modified-time} is 752 * copied to the target file if supported by both the source and target 753 * file store. Copying of file timestamps may result in precision 754 * loss. </td> 755 * </tr> 756 * <tr> 757 * <td> {@link LinkOption#NOFOLLOW_LINKS NOFOLLOW_LINKS} </td> 758 * <td> Symbolic links are not followed. If the file, located by this path, 759 * is a symbolic link, then the symbolic link itself, not the target of 760 * the link, is copied. It is implementation specific if file attributes 761 * can be copied to the new link. In other words, the {@code 762 * COPY_ATTRIBUTES} option may be ignored when copying a symbolic link. </td> 763 * </tr> 764 * </table> 765 * 766 * <p> An implementation of this interface may support additional 767 * implementation specific options. 768 * 769 * <p> Copying a file is not an atomic operation. If an {@link IOException} 770 * is thrown then it possible that the target file is incomplete or some of 771 * its file attributes have not been copied from the source file. When the 772 * {@code REPLACE_EXISTING} option is specified and the target file exists, 773 * then the target file is replaced. The check for the existence of the file 774 * and the creation of the new file may not be atomic with respect to other 775 * file system activities. 776 * 777 * @param target 778 * the target location 779 * @param options 780 * options specifying how the copy should be done 781 * 782 * @return the target 783 * 784 * @throws UnsupportedOperationException 785 * if the array contains a copy option that is not supported 786 * @throws FileAlreadyExistsException 787 * if the target file exists and cannot be replaced because the 788 * {@code REPLACE_EXISTING} option is not specified, or the target 789 * file is a non-empty directory <i>(optional specific exception)</i> 790 * @throws IOException 791 * if an I/O error occurs 792 * @throws SecurityException 793 * In the case of the default provider, and a security manager is 794 * installed, the {@link SecurityManager#checkRead(String) checkRead} 795 * method is invoked to check read access to the source file, the 796 * {@link SecurityManager#checkWrite(String) checkWrite} is invoked 797 * to check write access to the target file. If a symbolic link is 798 * copied the security manager is invoked to check {@link 799 * LinkPermission}{@code ("symbolic")}. 800 */ 801 public abstract Path copyTo(Path target, CopyOption... options) 802 throws IOException; 803 804 /** 805 * Move or rename the file located by this path to a target location. 806 * 807 * <p> By default, this method attempts to move the file to the target 808 * location, failing if the target file exists except if the source and 809 * target are the {@link #isSameFile same} file, in which case this method 810 * has no effect. If the file is a symbolic link then the symbolic link 811 * itself, not the target of the link, is moved. This method may be 812 * invoked to move an empty directory. In some implementations a directory 813 * has entries for special files or links that are created when the 814 * directory is created. In such implementations a directory is considered 815 * empty when only the special entries exist. When invoked to move a 816 * directory that is not empty then the directory is moved if it does not 817 * require moving the entries in the directory. For example, renaming a 818 * directory on the same {@link FileStore} will usually not require moving 819 * the entries in the directory. When moving a directory requires that its 820 * entries be moved then this method fails (by throwing an {@code 821 * IOException}). To move a <i>file tree</i> may involve copying rather 822 * than moving directories and this can be done using the {@link 823 * #copyTo copyTo} method in conjunction with the {@link 824 * Files#walkFileTree Files.walkFileTree} utility method. 825 * 826 * <p> The {@code options} parameter is an array of options and may contain 827 * any of the following: 828 * 829 * <table border=1 cellpadding=5 summary=""> 830 * <tr> <th>Option</th> <th>Description</th> </tr> 831 * <tr> 832 * <td> {@link StandardCopyOption#REPLACE_EXISTING REPLACE_EXISTING} </td> 833 * <td> If the target file exists, then the target file is replaced if it 834 * is not a non-empty directory. If the target file exists and is a 835 * symbolic link, then the symbolic link itself, not the target of 836 * the link, is replaced. </td> 837 * </tr> 838 * <tr> 839 * <td> {@link StandardCopyOption#ATOMIC_MOVE ATOMIC_MOVE} </td> 840 * <td> The move is performed as an atomic file system operation and all 841 * other options are ignored. If the target file exists then it is 842 * implementation specific if the existing file is replaced or this method 843 * fails by throwing an {@link IOException}. If the move cannot be 844 * performed as an atomic file system operation then {@link 845 * AtomicMoveNotSupportedException} is thrown. This can arise, for 846 * example, when the target location is on a different {@code FileStore} 847 * and would require that the file be copied, or target location is 848 * associated with a different provider to this object. </td> 849 * </table> 850 * 851 * <p> An implementation of this interface may support additional 852 * implementation specific options. 853 * 854 * <p> Where the move requires that the file be copied then the {@link 855 * BasicFileAttributes#lastModifiedTime last-modified-time} is copied to the 856 * new file. An implementation may also attempt to copy other file 857 * attributes but is not required to fail if the file attributes cannot be 858 * copied. When the move is performed as a non-atomic operation, and a {@code 859 * IOException} is thrown, then the state of the files is not defined. The 860 * original file and the target file may both exist, the target file may be 861 * incomplete or some of its file attributes may not been copied from the 862 * original file. 863 * 864 * @param target 865 * the target location 866 * @param options 867 * options specifying how the move should be done 868 * 869 * @return the target 870 * 871 * @throws UnsupportedOperationException 872 * if the array contains a copy option that is not supported 873 * @throws FileAlreadyExistsException 874 * if the target file exists and cannot be replaced because the 875 * {@code REPLACE_EXISTING} option is not specified, or the target 876 * file is a non-empty directory 877 * @throws AtomicMoveNotSupportedException 878 * if the options array contains the {@code ATOMIC_MOVE} option but 879 * the file cannot be moved as an atomic file system operation. 880 * @throws IOException 881 * if an I/O error occurs 882 * @throws SecurityException 883 * In the case of the default provider, and a security manager is 884 * installed, the {@link SecurityManager#checkWrite(String) checkWrite} 885 * method is invoked to check write access to both the source and 886 * target file. 887 */ 888 public abstract Path moveTo(Path target, CopyOption... options) 889 throws IOException; 890 891 /** 892 * Opens the directory referenced by this object, returning a {@code 893 * DirectoryStream} to iterate over all entries in the directory. The 894 * elements returned by the directory stream's {@link DirectoryStream#iterator 895 * iterator} are of type {@code Path}, each one representing an entry in the 896 * directory. The {@code Path} objects are obtained as if by {@link 897 * #resolve(Path) resolving} the name of the directory entry against this 898 * path. 899 * 900 * <p> The directory stream's {@code close} method should be invoked after 901 * iteration is completed so as to free any resources held for the open 902 * directory. 903 * 904 * <p> When an implementation supports operations on entries in the 905 * directory that execute in a race-free manner then the returned directory 906 * stream is a {@link SecureDirectoryStream}. 907 * 908 * @return a new and open {@code DirectoryStream} object 909 * 910 * @throws NotDirectoryException 911 * if the file could not otherwise be opened because it is not 912 * a directory <i>(optional specific exception)</i> 913 * @throws IOException 914 * if an I/O error occurs 915 * @throws SecurityException 916 * In the case of the default provider, and a security manager is 917 * installed, the {@link SecurityManager#checkRead(String) checkRead} 918 * method is invoked to check read access to the directory. 919 */ 920 public abstract DirectoryStream<Path> newDirectoryStream() 921 throws IOException; 922 923 /** 924 * Opens the directory referenced by this object, returning a {@code 925 * DirectoryStream} to iterate over the entries in the directory. The 926 * elements returned by the directory stream's {@link DirectoryStream#iterator 927 * iterator} are of type {@code Path}, each one representing an entry in the 928 * directory. The {@code Path} objects are obtained as if by {@link 929 * #resolve(Path) resolving} the name of the directory entry against this 930 * path. The entries returned by the iterator are filtered by matching the 931 * {@code String} representation of their file names against the given 932 * <em>globbing</em> pattern. 933 * 934 * <p> For example, suppose we want to iterate over the files ending with 935 * ".java" in a directory: 936 * <pre> 937 * Path dir = ... 938 * DirectoryStream<Path> stream = dir.newDirectoryStream("*.java"); 939 * </pre> 940 * 941 * <p> The globbing pattern is specified by the {@link 942 * FileSystem#getPathMatcher getPathMatcher} method. 943 * 944 * <p> The directory stream's {@code close} method should be invoked after 945 * iteration is completed so as to free any resources held for the open 946 * directory. 947 * 948 * <p> When an implementation supports operations on entries in the 949 * directory that execute in a race-free manner then the returned directory 950 * stream is a {@link SecureDirectoryStream}. 951 * 952 * @param glob 953 * the glob pattern 954 * 955 * @return a new and open {@code DirectoryStream} object 956 * 957 * @throws java.util.regex.PatternSyntaxException 958 * if the pattern is invalid 959 * @throws NotDirectoryException 960 * if the file could not otherwise be opened because it is not 961 * a directory <i>(optional specific exception)</i> 962 * @throws IOException 963 * if an I/O error occurs 964 * @throws SecurityException 965 * In the case of the default provider, and a security manager is 966 * installed, the {@link SecurityManager#checkRead(String) checkRead} 967 * method is invoked to check read access to the directory. 968 */ 969 public abstract DirectoryStream<Path> newDirectoryStream(String glob) 970 throws IOException; 971 972 /** 973 * Opens the directory referenced by this object, returning a {@code 974 * DirectoryStream} to iterate over the entries in the directory. The 975 * elements returned by the directory stream's {@link DirectoryStream#iterator 976 * iterator} are of type {@code Path}, each one representing an entry in the 977 * directory. The {@code Path} objects are obtained as if by {@link 978 * #resolve(Path) resolving} the name of the directory entry against this 979 * path. The entries returned by the iterator are filtered by the given 980 * {@link DirectoryStream.Filter filter}. 981 * 982 * <p> The directory stream's {@code close} method should be invoked after 983 * iteration is completed so as to free any resources held for the open 984 * directory. 985 * 986 * <p> Where the filter terminates due to an uncaught error or runtime 987 * exception then it is propagated to the {@link Iterator#hasNext() 988 * hasNext} or {@link Iterator#next() next} method. Where an {@code 989 * IOException} is thrown, it results in the {@code hasNext} or {@code 990 * next} method throwing a {@link DirectoryIteratorException} with the 991 * {@code IOException} as the cause. 992 * 993 * <p> When an implementation supports operations on entries in the 994 * directory that execute in a race-free manner then the returned directory 995 * stream is a {@link SecureDirectoryStream}. 996 * 997 * <p> <b>Usage Example:</b> 998 * Suppose we want to iterate over the files in a directory that are 999 * larger than 8K. 1000 * <pre> 1001 * DirectoryStream.Filter<Path> filter = new DirectoryStream.Filter<Path>() { 1002 * public boolean accept(Path file) throws IOException { 1003 * long size = Attributes.readBasicFileAttributes(file).size(); 1004 * return (size > 8192L); 1005 * } 1006 * }; 1007 * Path dir = ... 1008 * DirectoryStream<Path> stream = dir.newDirectoryStream(filter); 1009 * </pre> 1010 * @param filter 1011 * the directory stream filter 1012 * 1013 * @return a new and open {@code DirectoryStream} object 1014 * 1015 * @throws NotDirectoryException 1016 * if the file could not otherwise be opened because it is not 1017 * a directory <i>(optional specific exception)</i> 1018 * @throws IOException 1019 * if an I/O error occurs 1020 * @throws SecurityException 1021 * In the case of the default provider, and a security manager is 1022 * installed, the {@link SecurityManager#checkRead(String) checkRead} 1023 * method is invoked to check read access to the directory. 1024 */ 1025 public abstract DirectoryStream<Path> newDirectoryStream(DirectoryStream.Filter<? super Path> filter) 1026 throws IOException; 1027 1028 /** 1029 * Creates a new and empty file, failing if the file already exists. 1030 * 1031 * <p> This {@code Path} locates the file to create. The check for the 1032 * existence of the file and the creation of the new file if it does not 1033 * exist are a single operation that is atomic with respect to all other 1034 * filesystem activities that might affect the directory. 1035 * 1036 * <p> The {@code attrs} parameter is an optional array of {@link FileAttribute 1037 * file-attributes} to set atomically when creating the file. Each attribute 1038 * is identified by its {@link FileAttribute#name name}. If more than one 1039 * attribute of the same name is included in the array then all but the last 1040 * occurrence is ignored. 1041 * 1042 * @param attrs 1043 * an optional list of file attributes to set atomically when 1044 * creating the file 1045 * 1046 * @return this path 1047 * 1048 * @throws UnsupportedOperationException 1049 * if the array contains an attribute that cannot be set atomically 1050 * when creating the file 1051 * @throws FileAlreadyExistsException 1052 * if a file of that name already exists 1053 * <i>(optional specific exception)</i> 1054 * @throws IOException 1055 * if an I/O error occurs 1056 * @throws SecurityException 1057 * In the case of the default provider, and a security manager is 1058 * installed, the {@link SecurityManager#checkWrite(String) checkWrite} 1059 * method is invoked to check write access to the new file. 1060 */ 1061 public abstract Path createFile(FileAttribute<?>... attrs) throws IOException; 1062 1063 /** 1064 * Creates a new directory. 1065 * 1066 * <p> This {@code Path} locates the directory to create. The check for the 1067 * existence of the file and the creation of the directory if it does not 1068 * exist are a single operation that is atomic with respect to all other 1069 * filesystem activities that might affect the directory. 1070 * 1071 * <p> The {@code attrs} parameter is an optional array of {@link FileAttribute 1072 * file-attributes} to set atomically when creating the directory. Each 1073 * file attribute is identified by its {@link FileAttribute#name name}. If 1074 * more than one attribute of the same name is included in the array then all 1075 * but the last occurrence is ignored. 1076 * 1077 * @param attrs 1078 * an optional list of file attributes to set atomically when 1079 * creating the directory 1080 * 1081 * @return this path 1082 * 1083 * @throws UnsupportedOperationException 1084 * if the array contains an attribute that cannot be set atomically 1085 * when creating the directory 1086 * @throws FileAlreadyExistsException 1087 * if a directory could not otherwise be created because a file of 1088 * that name already exists <i>(optional specific exception)</i> 1089 * @throws IOException 1090 * if an I/O error occurs 1091 * @throws SecurityException 1092 * In the case of the default provider, and a security manager is 1093 * installed, the {@link SecurityManager#checkWrite(String) checkWrite} 1094 * method is invoked to check write access to the new directory. 1095 * 1096 * @see Files#createDirectories 1097 */ 1098 public abstract Path createDirectory(FileAttribute<?>... attrs) 1099 throws IOException; 1100 1101 /** 1102 * Opens or creates a file, returning a seekable byte channel to access the 1103 * file. 1104 * 1105 * <p> The {@code options} parameter determines how the file is opened. 1106 * The {@link StandardOpenOption#READ READ} and {@link StandardOpenOption#WRITE WRITE} 1107 * options determine if the file should be opened for reading and/or writing. 1108 * If neither option (or the {@link StandardOpenOption#APPEND APPEND} 1109 * option) is contained in the array then the file is opened for reading. 1110 * By default reading or writing commences at the beginning of the file. 1111 * 1112 * <p> In the addition to {@code READ} and {@code WRITE}, the following 1113 * options may be present: 1114 * 1115 * <table border=1 cellpadding=5 summary=""> 1116 * <tr> <th>Option</th> <th>Description</th> </tr> 1117 * <tr> 1118 * <td> {@link StandardOpenOption#APPEND APPEND} </td> 1119 * <td> If this option is present then the file is opened for writing and 1120 * each invocation of the channel's {@code write} method first advances 1121 * the position to the end of the file and then writes the requested 1122 * data. Whether the advancement of the position and the writing of the 1123 * data are done in a single atomic operation is system-dependent and 1124 * therefore unspecified. This option may not be used in conjunction 1125 * with the {@code READ} or {@code TRUNCATE_EXISTING} options. </td> 1126 * </tr> 1127 * <tr> 1128 * <td> {@link StandardOpenOption#TRUNCATE_EXISTING TRUNCATE_EXISTING} </td> 1129 * <td> If this option is present then the existing file is truncated to 1130 * a size of 0 bytes. This option is ignored when the file is opened only 1131 * for reading. </td> 1132 * </tr> 1133 * <tr> 1134 * <td> {@link StandardOpenOption#CREATE_NEW CREATE_NEW} </td> 1135 * <td> If this option is present then a new file is created, failing if 1136 * the file already exists or is a symbolic link. When creating a file the 1137 * check for the existence of the file and the creation of the file if it 1138 * does not exist is atomic with respect to other file system operations. 1139 * This option is ignored when the file is opened only for reading. </td> 1140 * </tr> 1141 * <tr> 1142 * <td > {@link StandardOpenOption#CREATE CREATE} </td> 1143 * <td> If this option is present then an existing file is opened if it 1144 * exists, otherwise a new file is created. This option is ignored if the 1145 * {@code CREATE_NEW} option is also present or the file is opened only 1146 * for reading. </td> 1147 * </tr> 1148 * <tr> 1149 * <td > {@link StandardOpenOption#DELETE_ON_CLOSE DELETE_ON_CLOSE} </td> 1150 * <td> When this option is present then the implementation makes a 1151 * <em>best effort</em> attempt to delete the file when closed by the 1152 * {@link SeekableByteChannel#close close} method. If the {@code close} 1153 * method is not invoked then a <em>best effort</em> attempt is made to 1154 * delete the file when the Java virtual machine terminates. </td> 1155 * </tr> 1156 * <tr> 1157 * <td>{@link StandardOpenOption#SPARSE SPARSE} </td> 1158 * <td> When creating a new file this option is a <em>hint</em> that the 1159 * new file will be sparse. This option is ignored when not creating 1160 * a new file. </td> 1161 * </tr> 1162 * <tr> 1163 * <td> {@link StandardOpenOption#SYNC SYNC} </td> 1164 * <td> Requires that every update to the file's content or metadata be 1165 * written synchronously to the underlying storage device. (see <a 1166 * href="package-summary.html#integrity"> Synchronized I/O file 1167 * integrity</a>). </td> 1168 * <tr> 1169 * <tr> 1170 * <td> {@link StandardOpenOption#DSYNC DSYNC} </td> 1171 * <td> Requires that every update to the file's content be written 1172 * synchronously to the underlying storage device. (see <a 1173 * href="package-summary.html#integrity"> Synchronized I/O file 1174 * integrity</a>). </td> 1175 * </tr> 1176 * </table> 1177 * 1178 * <p> An implementation may also support additional implementation specific 1179 * options. 1180 * 1181 * <p> The {@code attrs} parameter is an optional array of file {@link 1182 * FileAttribute file-attributes} to set atomically when a new file is created. 1183 * 1184 * <p> In the case of the default provider, the returned seekable byte channel 1185 * is a {@link java.nio.channels.FileChannel}. 1186 * 1187 * <p> <b>Usage Examples:</b> 1188 * <pre> 1189 * Path file = ... 1190 * 1191 * // open file for reading 1192 * ReadableByteChannel rbc = file.newByteChannel(EnumSet.of(READ))); 1193 * 1194 * // open file for writing to the end of an existing file, creating 1195 * // the file if it doesn't already exist 1196 * WritableByteChannel wbc = file.newByteChannel(EnumSet.of(CREATE,APPEND)); 1197 * 1198 * // create file with initial permissions, opening it for both reading and writing 1199 * FileAttribute<Set<PosixFilePermission>> perms = ... 1200 * SeekableByteChannel sbc = file.newByteChannel(EnumSet.of(CREATE_NEW,READ,WRITE), perms); 1201 * </pre> 1202 * 1203 * @param options 1204 * Options specifying how the file is opened 1205 * @param attrs 1206 * An optional list of file attributes to set atomically when 1207 * creating the file 1208 * 1209 * @return a new seekable byte channel 1210 * 1211 * @throws IllegalArgumentException 1212 * if the set contains an invalid combination of options 1213 * @throws UnsupportedOperationException 1214 * if an unsupported open option is specified or the array contains 1215 * attributes that cannot be set atomically when creating the file 1216 * @throws FileAlreadyExistsException 1217 * if a file of that name already exists and the {@link 1218 * StandardOpenOption#CREATE_NEW CREATE_NEW} option is specified 1219 * <i>(optional specific exception)</i> 1220 * @throws IOException 1221 * if an I/O error occurs 1222 * @throws SecurityException 1223 * In the case of the default provider, and a security manager is 1224 * installed, the {@link SecurityManager#checkRead(String) checkRead} 1225 * method is invoked to check read access to the path if the file is 1226 * opened for reading. The {@link SecurityManager#checkWrite(String) 1227 * checkWrite} method is invoked to check write access to the path 1228 * if the file is opened for writing. 1229 */ 1230 public abstract SeekableByteChannel newByteChannel(Set<? extends OpenOption> options, 1231 FileAttribute<?>... attrs) 1232 throws IOException; 1233 1234 /** 1235 * Opens or creates a file, returning a seekable byte channel to access the 1236 * file. 1237 * 1238 * <p> This method opens or creates a file in exactly the manner specified 1239 * by the {@link Path#newByteChannel(Set,FileAttribute[]) newByteChannel} 1240 * method. 1241 * 1242 * @param options 1243 * options specifying how the file is opened 1244 * 1245 * @return a new seekable byte channel 1246 * 1247 * @throws IllegalArgumentException 1248 * if the set contains an invalid combination of options 1249 * @throws UnsupportedOperationException 1250 * if an unsupported open option is specified 1251 * @throws FileAlreadyExistsException 1252 * if a file of that name already exists and the {@link 1253 * StandardOpenOption#CREATE_NEW CREATE_NEW} option is specified 1254 * <i>(optional specific exception)</i> 1255 * @throws IOException 1256 * if an I/O error occurs 1257 * @throws SecurityException 1258 * In the case of the default provider, and a security manager is 1259 * installed, the {@link SecurityManager#checkRead(String) checkRead} 1260 * method is invoked to check read access to the path if the file is 1261 * opened for reading. The {@link SecurityManager#checkWrite(String) 1262 * checkWrite} method is invoked to check write access to the path 1263 * if the file is opened for writing. 1264 */ 1265 public abstract SeekableByteChannel newByteChannel(OpenOption... options) 1266 throws IOException; 1267 1268 /** 1269 * Opens or creates the file located by this object for writing, returning 1270 * an output stream to write bytes to the file. 1271 * 1272 * <p> This method opens or creates a file in exactly the manner specified 1273 * by the {@link Path#newByteChannel(Set,FileAttribute[]) newByteChannel} 1274 * method except that the {@link StandardOpenOption#READ READ} option may not 1275 * be present in the array of open options. 1276 * 1277 * @param options 1278 * options specifying how the file is opened 1279 * 1280 * @return a new output stream 1281 * 1282 * @throws IllegalArgumentException {@inheritDoc} 1283 * @throws UnsupportedOperationException {@inheritDoc} 1284 * @throws IOException {@inheritDoc} 1285 * @throws SecurityException {@inheritDoc} 1286 */ 1287 @Override 1288 public abstract OutputStream newOutputStream(OpenOption... options) 1289 throws IOException; 1290 1291 /** 1292 * Tells whether or not the file located by this object is considered 1293 * <em>hidden</em>. The exact definition of hidden is platform or provider 1294 * dependent. On UNIX for example a file is considered to be hidden if its 1295 * name begins with a period character ('.'). On Windows a file is 1296 * considered hidden if it isn't a directory and the DOS {@link 1297 * DosFileAttributes#isHidden hidden} attribute is set. 1298 * 1299 * <p> Depending on the implementation this method may require to access 1300 * the file system to determine if the file is considered hidden. 1301 * 1302 * @return {@code true} if the file is considered hidden 1303 * 1304 * @throws IOException 1305 * if an I/O error occurs 1306 * @throws SecurityException 1307 * In the case of the default provider, and a security manager is 1308 * installed, the {@link SecurityManager#checkRead(String) checkRead} 1309 * method is invoked to check read access to the file. 1310 */ 1311 public abstract boolean isHidden() throws IOException; 1312 1313 /** 1314 * Checks the existence and optionally the accessibility of the file 1315 * located by this path. 1316 * 1317 * <p> This method checks the existence of a file and that this Java virtual 1318 * machine has appropriate privileges that would allow it access the file 1319 * according to all of access modes specified in the {@code modes} parameter 1320 * as follows: 1321 * 1322 * <table border=1 cellpadding=5 summary=""> 1323 * <tr> <th>Value</th> <th>Description</th> </tr> 1324 * <tr> 1325 * <td> {@link AccessMode#READ READ} </td> 1326 * <td> Checks that the file exists and that the Java virtual machine has 1327 * permission to read the file. </td> 1328 * </tr> 1329 * <tr> 1330 * <td> {@link AccessMode#WRITE WRITE} </td> 1331 * <td> Checks that the file exists and that the Java virtual machine has 1332 * permission to write to the file, </td> 1333 * </tr> 1334 * <tr> 1335 * <td> {@link AccessMode#EXECUTE EXECUTE} </td> 1336 * <td> Checks that the file exists and that the Java virtual machine has 1337 * permission to {@link Runtime#exec execute} the file. The semantics 1338 * may differ when checking access to a directory. For example, on UNIX 1339 * systems, checking for {@code EXECUTE} access checks that the Java 1340 * virtual machine has permission to search the directory in order to 1341 * access file or subdirectories. </td> 1342 * </tr> 1343 * </table> 1344 * 1345 * <p> If the {@code modes} parameter is of length zero, then the existence 1346 * of the file is checked. 1347 * 1348 * <p> This method follows symbolic links if the file referenced by this 1349 * object is a symbolic link. Depending on the implementation, this method 1350 * may require to read file permissions, access control lists, or other 1351 * file attributes in order to check the effective access to the file. To 1352 * determine the effective access to a file may require access to several 1353 * attributes and so in some implementations this method may not be atomic 1354 * with respect to other file system operations. Furthermore, as the result 1355 * of this method is immediately outdated, there is no guarantee that a 1356 * subsequence access will succeed (or even that it will access the same 1357 * file). Care should be taken when using this method in security sensitive 1358 * applications. 1359 * 1360 * @param modes 1361 * The access modes to check; may have zero elements 1362 * 1363 * @throws UnsupportedOperationException 1364 * an implementation is required to support checking for 1365 * {@code READ}, {@code WRITE}, and {@code EXECUTE} access. This 1366 * exception is specified to allow for the {@code Access} enum to 1367 * be extended in future releases. 1368 * @throws NoSuchFileException 1369 * if a file does not exist <i>(optional specific exception)</i> 1370 * @throws AccessDeniedException 1371 * the requested access would be denied or the access cannot be 1372 * determined because the Java virtual machine has insufficient 1373 * privileges or other reasons. <i>(optional specific exception)</i> 1374 * @throws IOException 1375 * if an I/O error occurs 1376 * @throws SecurityException 1377 * In the case of the default provider, and a security manager is 1378 * installed, the {@link SecurityManager#checkRead(String) checkRead} 1379 * is invoked when checking read access to the file or only the 1380 * existence of the file, the {@link SecurityManager#checkWrite(String) 1381 * checkWrite} is invoked when checking write access to the file, 1382 * and {@link SecurityManager#checkExec(String) checkExec} is invoked 1383 * when checking execute access. 1384 */ 1385 public abstract void checkAccess(AccessMode... modes) throws IOException; 1386 1387 /** 1388 * Tests whether the file located by this path exists. 1389 * 1390 * <p> This convenience method is intended for cases where it is required to 1391 * take action when it can be confirmed that a file exists. This method simply 1392 * invokes the {@link #checkAccess checkAccess} method to check if the file 1393 * exists. If the {@code checkAccess} method succeeds then this method returns 1394 * {@code true}, otherwise if an {@code IOException} is thrown (because the 1395 * file doesn't exist or cannot be accessed by this Java virtual machine) 1396 * then {@code false} is returned. 1397 * 1398 * <p> Note that the result of this method is immediately outdated. If this 1399 * method indicates the file exists then there is no guarantee that a 1400 * subsequence access will succeed. Care should be taken when using this 1401 * method in security sensitive applications. 1402 * 1403 * @return {@code true} if the file exists; {@code false} if the file does 1404 * not exist or its existence cannot be determined. 1405 * 1406 * @throws SecurityException 1407 * In the case of the default provider, the {@link 1408 * SecurityManager#checkRead(String)} is invoked to check 1409 * read access to the file. 1410 * 1411 * @see #notExists 1412 */ 1413 public abstract boolean exists(); 1414 1415 /** 1416 * Tests whether the file located by this path does not exist. 1417 * 1418 * <p> This convenience method is intended for cases where it is required to 1419 * take action when it can be confirmed that a file does not exist. This 1420 * method invokes the {@link #checkAccess checkAccess} method to check if the 1421 * file exists. If the file does not exist then {@code true} is returned, 1422 * otherwise the file exists or cannot be accessed by this Java virtual 1423 * machine and {@code false} is returned. 1424 * 1425 * <p> Note that this method is not the complement of the {@link #exists 1426 * exists} method. Where it is not possible to determine if a file exists 1427 * or not then both methods return {@code false}. As with the {@code exists} 1428 * method, the result of this method is immediately outdated. If this 1429 * method indicates the file does exist then there is no guarantee that a 1430 * subsequence attempt to create the file will succeed. Care should be taken 1431 * when using this method in security sensitive applications. 1432 * 1433 * @return {@code true} if the file does not exist; {@code false} if the 1434 * file exists or its existence cannot be determined. 1435 * 1436 * @throws SecurityException 1437 * In the case of the default provider, the {@link 1438 * SecurityManager#checkRead(String)} is invoked to check 1439 * read access to the file. 1440 */ 1441 public abstract boolean notExists(); 1442 1443 /** 1444 * Returns the {@link FileStore} representing the file store where an 1445 * existing file, located by this path, is stored. 1446 * 1447 * <p> Once a reference to the {@code FileStore} is obtained it is 1448 * implementation specific if operations on the returned {@code FileStore}, 1449 * or {@link FileStoreAttributeView} objects obtained from it, continue 1450 * to depend on the existence of the file. In particular the behavior is not 1451 * defined for the case that the file is deleted or moved to a different 1452 * file store. 1453 * 1454 * @return the file store where the file is stored 1455 * 1456 * @throws IOException 1457 * if an I/O error occurs 1458 * @throws SecurityException 1459 * In the case of the default provider, and a security manager is 1460 * installed, the {@link SecurityManager#checkRead(String) checkRead} 1461 * method is invoked to check read access to the file, and in 1462 * addition it checks {@link RuntimePermission}<tt> 1463 * ("getFileStoreAttributes")</tt> 1464 */ 1465 public abstract FileStore getFileStore() throws IOException; 1466 1467 // -- watchable -- 1468 1469 /** 1470 * Registers the file located by this path with a watch service. 1471 * 1472 * <p> In this release, this path locates a directory that exists. The 1473 * directory is registered with the watch service so that entries in the 1474 * directory can be watched. The {@code events} parameter is an array of 1475 * events to register and may contain the following events: 1476 * <ul> 1477 * <li>{@link StandardWatchEventKind#ENTRY_CREATE ENTRY_CREATE} - 1478 * entry created or moved into the directory</li> 1479 * <li>{@link StandardWatchEventKind#ENTRY_DELETE ENTRY_DELETE} - 1480 * entry deleted or moved out of the directory</li> 1481 * <li>{@link StandardWatchEventKind#ENTRY_MODIFY ENTRY_MODIFY} - 1482 * entry in directory was modified</li> 1483 * </ul> 1484 * 1485 * <p> The {@link WatchEvent#context context} for these events is the 1486 * relative path between the directory located by this path, and the path 1487 * that locates the directory entry that is created, deleted, or modified. 1488 * 1489 * <p> The set of events may include additional implementation specific 1490 * event that are not defined by the enum {@link StandardWatchEventKind} 1491 * 1492 * <p> The {@code modifiers} parameter is an array of <em>modifiers</em> 1493 * that qualify how the directory is registered. This release does not 1494 * define any <em>standard</em> modifiers. The array may contain 1495 * implementation specific modifiers. 1496 * 1497 * <p> Where a file is registered with a watch service by means of a symbolic 1498 * link then it is implementation specific if the watch continues to depend 1499 * on the existence of the symbolic link after it is registered. 1500 * 1501 * @param watcher 1502 * the watch service to which this object is to be registered 1503 * @param events 1504 * the events for which this object should be registered 1505 * @param modifiers 1506 * the modifiers, if any, that modify how the object is registered 1507 * 1508 * @return a key representing the registration of this object with the 1509 * given watch service 1510 * 1511 * @throws UnsupportedOperationException 1512 * if unsupported events or modifiers are specified 1513 * @throws IllegalArgumentException 1514 * if an invalid combination of events or modifiers is specified 1515 * @throws ClosedWatchServiceException 1516 * if the watch service is closed 1517 * @throws NotDirectoryException 1518 * if the file is registered to watch the entries in a directory 1519 * and the file is not a directory <i>(optional specific exception)</i> 1520 * @throws IOException 1521 * if an I/O error occurs 1522 * @throws SecurityException 1523 * In the case of the default provider, and a security manager is 1524 * installed, the {@link SecurityManager#checkRead(String) checkRead} 1525 * method is invoked to check read access to the file. 1526 */ 1527 @Override 1528 public abstract WatchKey register(WatchService watcher, 1529 WatchEvent.Kind<?>[] events, 1530 WatchEvent.Modifier... modifiers) 1531 throws IOException; 1532 1533 /** 1534 * Registers the file located by this path with a watch service. 1535 * 1536 * <p> An invocation of this method behaves in exactly the same way as the 1537 * invocation 1538 * <pre> 1539 * watchable.{@link #register(WatchService,WatchEvent.Kind[],WatchEvent.Modifier[]) register}(watcher, events, new WatchEvent.Modifier[0]); 1540 * </pre> 1541 * 1542 * <p> <b>Usage Example:</b> 1543 * Suppose we wish to register a directory for entry create, delete, and modify 1544 * events: 1545 * <pre> 1546 * Path dir = ... 1547 * WatchService watcher = ... 1548 * 1549 * WatchKey key = dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY); 1550 * </pre> 1551 * @param watcher 1552 * The watch service to which this object is to be registered 1553 * @param events 1554 * The events for which this object should be registered 1555 * 1556 * @return A key representing the registration of this object with the 1557 * given watch service 1558 * 1559 * @throws UnsupportedOperationException 1560 * If unsupported events are specified 1561 * @throws IllegalArgumentException 1562 * If an invalid combination of events is specified 1563 * @throws ClosedWatchServiceException 1564 * If the watch service is closed 1565 * @throws NotDirectoryException 1566 * If the file is registered to watch the entries in a directory 1567 * and the file is not a directory <i>(optional specific exception)</i> 1568 * @throws IOException 1569 * If an I/O error occurs 1570 * @throws SecurityException 1571 * In the case of the default provider, and a security manager is 1572 * installed, the {@link SecurityManager#checkRead(String) checkRead} 1573 * method is invoked to check read access to the file. 1574 */ 1575 @Override 1576 public abstract WatchKey register(WatchService watcher, 1577 WatchEvent.Kind<?>... events) 1578 throws IOException; 1579 1580 // -- Iterable -- 1581 1582 /** 1583 * Returns an iterator over the name elements of this path. 1584 * 1585 * <p> The first element returned by the iterator represents the name 1586 * element that is closest to the root in the directory hierarchy, the 1587 * second element is the next closest, and so on. The last element returned 1588 * is the name of the file or directory denoted by this path. The {@link 1589 * #getRoot root} component, if present, is not returned by the iterator. 1590 * 1591 * @return an iterator over the name elements of this path. 1592 */ 1593 @Override 1594 public abstract Iterator<Path> iterator(); 1595 1596 // -- compareTo/equals/hashCode -- 1597 1598 /** 1599 * Compares two abstract paths lexicographically. The ordering defined by 1600 * this method is provider specific, and in the case of the default 1601 * provider, platform specific. This method does not access the file system 1602 * and neither file is required to exist. 1603 * 1604 * @param other the path compared to this path. 1605 * 1606 * @return zero if the argument is {@link #equals equal} to this path, a 1607 * value less than zero if this path is lexicographically less than 1608 * the argument, or a value greater than zero if this path is 1609 * lexicographically greater than the argument 1610 */ 1611 @Override 1612 public abstract int compareTo(Path other); 1613 1614 /** 1615 * Tests if the file referenced by this object is the same file referenced 1616 * by another object. 1617 * 1618 * <p> If this {@code Path} and the given {@code Path} are {@link 1619 * #equals(Object) equal} then this method returns {@code true} without checking 1620 * if the file exists. If the {@code Path} and the given {@code Path} 1621 * are associated with different providers, or the given {@code Path} is 1622 * {@code null} then this method returns {@code false}. Otherwise, this method 1623 * checks if both {@code Paths} locate the same file, and depending on the 1624 * implementation, may require to open or access both files. 1625 * 1626 * <p> If the file system and files remain static, then this method implements 1627 * an equivalence relation for non-null {@code Paths}. 1628 * <ul> 1629 * <li>It is <i>reflexive</i>: for a non-null {@code Path} {@code f}, 1630 * {@code f.isSameFile(f)} should return {@code true}. 1631 * <li>It is <i>symmetric</i>: for two non-null {@code Path} 1632 * {@code f} and {@code g}, {@code f.isSameFile(g)} will equal 1633 * {@code g.isSameFile(f)}. 1634 * <li>It is <i>transitive</i>: for three {@code Paths} 1635 * {@code f}, {@code g}, and {@code h}, if {@code f.isSameFile(g)} returns 1636 * {@code true} and {@code g.isSameFile(h)} returns {@code true}, then 1637 * {@code f.isSameFile(h)} will return return {@code true}. 1638 * </ul> 1639 * 1640 * @param other 1641 * the other file reference 1642 * 1643 * @return {@code true} if, and only if, this object and the given object 1644 * locate the same file 1645 * 1646 * @throws IOException 1647 * if an I/O error occurs 1648 * @throws SecurityException 1649 * In the case of the default provider, and a security manager is 1650 * installed, the {@link SecurityManager#checkRead(String) checkRead} 1651 * method is invoked to check read access to both files. 1652 * 1653 * @see java.nio.file.attribute.BasicFileAttributes#fileKey 1654 */ 1655 public abstract boolean isSameFile(Path other) throws IOException; 1656 1657 /** 1658 * Tests this path for equality with the given object. 1659 * 1660 * <p> If the given object is not a Path, or is a Path associated with a 1661 * different provider, then this method immediately returns {@code false}. 1662 * 1663 * <p> Whether or not two path are equal depends on the file system 1664 * implementation. In some cases the paths are compared without regard 1665 * to case, and others are case sensitive. This method does not access the 1666 * file system and the file is not required to exist. 1667 * 1668 * <p> This method satisfies the general contract of the {@link 1669 * java.lang.Object#equals(Object) Object.equals} method. </p> 1670 * 1671 * @param other 1672 * the object to which this object is to be compared 1673 * 1674 * @return {@code true} if, and only if, the given object is a {@code Path} 1675 * that is identical to this {@code Path} 1676 */ 1677 @Override 1678 public abstract boolean equals(Object other); 1679 1680 /** 1681 * Computes a hash code for this path. 1682 * 1683 * <p> The hash code is based upon the components of the path, and 1684 * satisfies the general contract of the {@link Object#hashCode 1685 * Object.hashCode} method. 1686 * 1687 * @return the hash-code value for this path 1688 */ 1689 @Override 1690 public abstract int hashCode(); 1691 1692 /** 1693 * Returns the string representation of this path. 1694 * 1695 * <p> If this path was created by converting a path string using the 1696 * {@link FileSystem#getPath getPath} method then the path string returned 1697 * by this method may differ from the original String used to create the path. 1698 * 1699 * <p> The returned path string uses the default name {@link 1700 * FileSystem#getSeparator separator} to separate names in the path. 1701 * 1702 * @return the string representation of this path 1703 */ 1704 @Override 1705 public abstract String toString(); 1706 }