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