-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathCreatingPaths.java
41 lines (38 loc) · 1.21 KB
/
CreatingPaths.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package nio2;
import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;
/**
*
* @author chengfeili
* Jun 20, 2017 9:31:31 AM
*
* Path is a direct replacement for the legacy java.io.File class, and
* conceptually it contains many of the same properties. For example,
* both File and Path objects may refer to a file or a directory. Both
* also may refer to an absolute path or relative path within the file
* system.
*/
public class CreatingPaths {
public void path() {
Path path1 = Paths.get("/home/zoo");
Path path2 = Paths.get("/", "home", "zoo");
// Use URI values for both local and remote paths
// URIs must reference absolute paths at runtime;
Path path3 = Paths.get("file:///home/zoo");
URI uri = path3.toUri();
try {
FileSystem fileSystem = FileSystems.getFileSystem(new URI("http://www.selikoff.net"));
Path path4 = fileSystem.getPath("duck.txt");
} catch (URISyntaxException e) {
}
// Legacy File Instances
File file = new File("/home/zoo");
Path path5 = file.toPath();
File file2 = path5.toFile();
}
}