91 * <h2>Concurrency</h2> |
92 * <h2>Concurrency</h2> |
92 * <p> Implementations of this interface are immutable and safe for use by |
93 * <p> Implementations of this interface are immutable and safe for use by |
93 * multiple concurrent threads. |
94 * multiple concurrent threads. |
94 * |
95 * |
95 * @since 1.7 |
96 * @since 1.7 |
96 * @see Paths |
|
97 */ |
97 */ |
98 |
98 |
99 public interface Path |
99 public interface Path |
100 extends Comparable<Path>, Iterable<Path>, Watchable |
100 extends Comparable<Path>, Iterable<Path>, Watchable |
101 { |
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 |
102 /** |
215 /** |
103 * Returns the file system that created this object. |
216 * Returns the file system that created this object. |
104 * |
217 * |
105 * @return the file system that created this object |
218 * @return the file system that created this object |
106 */ |
219 */ |