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