Skip to content

Commit 6804e62

Browse files
author
jim.deng
committed
2020年12月11日14:57:40
1 parent 4682ea8 commit 6804e62

13 files changed

Lines changed: 437 additions & 37 deletions

AddAndSubtractPaths.java

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// files/AddAndSubtractPaths.java
2+
// (c)2020 MindView LLC: see Copyright.txt
3+
// We make no guarantees that this code is fit for any purpose.
4+
// Visit http://OnJava8.com for more book information.
5+
import java.nio.file.*;
6+
import java.io.IOException;
7+
8+
public class AddAndSubtractPaths {
9+
static Path base = Paths.get("..", "..", "..")
10+
.toAbsolutePath()
11+
.normalize();
12+
static void show(int id, Path result) {
13+
if(result.isAbsolute())
14+
System.err.println("(" + id + ")[relativize] " +
15+
base.relativize(result));
16+
else
17+
System.err.println("(" + id + ") " + result);
18+
try {
19+
System.err.println("RealPath: "
20+
+ result.toRealPath());
21+
} catch(IOException e) {
22+
System.err.println(e);
23+
}
24+
}
25+
public static void main(String[] args) {
26+
System.err.println(System.getProperty("os.name"));
27+
System.err.println(base);
28+
Path p = Paths.get("AddAndSubtractPaths.java")
29+
.toAbsolutePath();
30+
show(1, p);
31+
Path convoluted = p.getParent().getParent()
32+
.resolve("strings")
33+
.resolve("..")
34+
.resolve(p.getParent().getFileName());
35+
show(2, convoluted);
36+
show(3, convoluted.normalize());
37+
38+
Path p2 = Paths.get("..", "..");
39+
show(4, p2);
40+
show(5, p2.normalize());
41+
show(6, p2.toAbsolutePath().normalize());
42+
43+
Path p3 = Paths.get(".").toAbsolutePath();
44+
Path p4 = p3.resolve(p2);
45+
show(7, p4);
46+
show(8, p4.normalize());
47+
48+
Path p5 = Paths.get("").toAbsolutePath();
49+
show(9, p5);
50+
show(10, p5.resolveSibling("strings"));
51+
show(11, Paths.get("nonexistent"));
52+
}
53+
}
54+
/* Output:
55+
Windows 10
56+
C:\Users\Bruce\Documents\GitHub
57+
(1)r on-
58+
java\ExtractedExamples\files\AddAndSubtractPaths.java
59+
RealPath: C:\Users\Bruce\Documents\GitHub\on-
60+
java\ExtractedExamples\files\AddAndSubtractPaths.java
61+
(2)r on-java\ExtractedExamples\strings\..\files
62+
RealPath: C:\Users\Bruce\Documents\GitHub\on-
63+
java\ExtractedExamples\files
64+
(3)r on-java\ExtractedExamples\files
65+
RealPath: C:\Users\Bruce\Documents\GitHub\on-
66+
java\ExtractedExamples\files
67+
(4) ..\..
68+
RealPath: C:\Users\Bruce\Documents\GitHub\on-java
69+
(5) ..\..
70+
RealPath: C:\Users\Bruce\Documents\GitHub\on-java
71+
(6)r on-java
72+
RealPath: C:\Users\Bruce\Documents\GitHub\on-java
73+
(7)r on-java\ExtractedExamples\files\.\..\..
74+
RealPath: C:\Users\Bruce\Documents\GitHub\on-java
75+
(8)r on-java
76+
RealPath: C:\Users\Bruce\Documents\GitHub\on-java
77+
(9)r on-java\ExtractedExamples\files
78+
RealPath: C:\Users\Bruce\Documents\GitHub\on-
79+
java\ExtractedExamples\files
80+
(10)r on-java\ExtractedExamples\strings
81+
RealPath: C:\Users\Bruce\Documents\GitHub\on-
82+
java\ExtractedExamples\strings
83+
(11) nonexistent
84+
java.nio.file.NoSuchFileException:
85+
C:\Users\Bruce\Documents\GitHub\on-
86+
java\ExtractedExamples\files\nonexistent
87+
*/

Directories.java

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// files/Directories.java
2+
// (c)2020 MindView LLC: see Copyright.txt
3+
// We make no guarantees that this code is fit for any purpose.
4+
// Visit http://OnJava8.com for more book information.
5+
import java.util.*;
6+
import java.nio.file.*;
7+
//import onjava.RmDir;
8+
9+
public class Directories {
10+
static Path test = Paths.get("test");
11+
static String sep =
12+
FileSystems.getDefault().getSeparator();
13+
static List<String> parts =
14+
Arrays.asList("foo", "bar", "baz", "bag");
15+
static Path makeVariant() {
16+
Collections.rotate(parts, 1);
17+
return Paths.get("test", String.join(sep, parts));
18+
}
19+
static void refreshTestDir() throws Exception {
20+
if(Files.exists(test))
21+
RmDir.rmdir(test);
22+
if(!Files.exists(test))
23+
Files.createDirectory(test);
24+
}
25+
public static void
26+
main(String[] args) throws Exception {
27+
refreshTestDir();
28+
Files.createFile(test.resolve("Hello.txt"));
29+
Path variant = makeVariant();
30+
// Throws exception (too many levels):
31+
try {
32+
Files.createDirectory(variant);
33+
} catch(Exception e) {
34+
System.err.println("Nope, that doesn't work.");
35+
}
36+
populateTestDir();
37+
Path tempdir =
38+
Files.createTempDirectory(test, "DIR_");
39+
Files.createTempFile(tempdir, "pre", ".non");
40+
Files.newDirectoryStream(test)
41+
.forEach(System.out::println);
42+
System.err.println("*********");
43+
Files.walk(test).forEach(System.out::println);
44+
}
45+
static void populateTestDir() throws Exception {
46+
for(int i = 0; i < parts.size(); i++) {
47+
Path variant = makeVariant();
48+
if(!Files.exists(variant)) {
49+
Files.createDirectories(variant);
50+
Files.copy(Paths.get("Directories.java"),
51+
variant.resolve("File.txt"));
52+
Files.createTempFile(variant, null, null);
53+
}
54+
}
55+
}
56+
}
57+
/* Output:
58+
Nope, that doesn't work.
59+
test\bag
60+
test\bar
61+
test\baz
62+
test\DIR_5142667942049986036
63+
test\foo
64+
test\Hello.txt
65+
*********
66+
test
67+
test\bag
68+
test\bag\foo
69+
test\bag\foo\bar
70+
test\bag\foo\bar\baz
71+
test\bag\foo\bar\baz\8279660869874696036.tmp
72+
test\bag\foo\bar\baz\File.txt
73+
test\bar
74+
test\bar\baz
75+
test\bar\baz\bag
76+
test\bar\baz\bag\foo
77+
test\bar\baz\bag\foo\1274043134240426261.tmp
78+
test\bar\baz\bag\foo\File.txt
79+
test\baz
80+
test\baz\bag
81+
test\baz\bag\foo
82+
test\baz\bag\foo\bar
83+
test\baz\bag\foo\bar\6130572530014544105.tmp
84+
test\baz\bag\foo\bar\File.txt
85+
test\DIR_5142667942049986036
86+
test\DIR_5142667942049986036\pre7704286843227113253.non
87+
test\foo
88+
test\foo\bar
89+
test\foo\bar\baz
90+
test\foo\bar\baz\bag
91+
test\foo\bar\baz\bag\5412864507741775436.tmp
92+
test\foo\bar\baz\bag\File.txt
93+
test\Hello.txt
94+
*/

PartsOfPaths.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// files/PartsOfPaths.java
2+
// (c)2020 MindView LLC: see Copyright.txt
3+
// We make no guarantees that this code is fit for any purpose.
4+
// Visit http://OnJava8.com for more book information.
5+
import java.nio.file.*;
6+
7+
public class PartsOfPaths {
8+
public static void main(String[] args) {
9+
System.err.println(System.getProperty("os.name"));
10+
Path p = Paths.get("PartsOfPaths.java").toAbsolutePath();
11+
System.err.println(p);
12+
System.err.println(Files.exists(p));
13+
14+
for(int i = 0; i < p.getNameCount(); i++)
15+
System.err.println("第" + i + "部分--"+p.getName(i));
16+
System.err.println("ends with '.java': " +
17+
p.endsWith(".java"));
18+
for(Path pp : p) {
19+
System.err.print(pp + ": ");
20+
System.err.print(p.startsWith(pp) + " : ");
21+
System.err.println(p.endsWith(pp));
22+
}
23+
System.err.println("Starts with " + p.getRoot() +
24+
" " + p.startsWith(p.getRoot()));
25+
}
26+
}
27+
/* Output:
28+
Windows 10
29+
Users
30+
Bruce
31+
Documents
32+
GitHub
33+
on-java
34+
ExtractedExamples
35+
files
36+
PartsOfPaths.java
37+
ends with '.java': false
38+
Users: false : false
39+
Bruce: false : false
40+
Documents: false : false
41+
GitHub: false : false
42+
on-java: false : false
43+
ExtractedExamples: false : false
44+
files: false : false
45+
PartsOfPaths.java: false : true
46+
Starts with C:\ true
47+
*/

PathInfo.java

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
// files/PathInfo.java
2+
// (c)2020 MindView LLC: see Copyright.txt
3+
// We make no guarantees that this code is fit for any purpose.
4+
// Visit http://OnJava8.com for more book information.
5+
import java.nio.file.*;
6+
import java.net.URI;
7+
import java.io.File;
8+
import java.io.IOException;
9+
10+
public class PathInfo {
11+
static void show(String id, Object p) {
12+
System.err.println(id + ": " + p);
13+
}
14+
static void info(Path p) {
15+
show("toString", p);
16+
show("Exists", Files.exists(p));
17+
show("RegularFile", Files.isRegularFile(p));
18+
show("Directory", Files.isDirectory(p));
19+
show("Absolute", p.isAbsolute());
20+
show("FileName", p.getFileName());
21+
show("Parent", p.getParent());
22+
show("Root", p.getRoot());
23+
System.err.println("******************");
24+
}
25+
public static void main(String[] args) {
26+
System.err.println(System.getProperty("os.name"));
27+
28+
info(Paths.get(
29+
"C:", "path", "to", "nowhere", "NoFile.txt"));
30+
31+
Path p = Paths.get("PathInfo.java");
32+
info(p);
33+
34+
Path ap = p.toAbsolutePath();
35+
info(ap);
36+
37+
info(ap.getParent());
38+
39+
try {
40+
info(p.toRealPath());
41+
} catch(IOException e) {
42+
System.err.println(e);
43+
}
44+
45+
URI u = p.toUri();
46+
System.err.println("URI: " + u);
47+
48+
Path puri = Paths.get(u);
49+
System.err.println(Files.exists(puri));
50+
51+
File f = ap.toFile(); // Don't be fooled
52+
}
53+
}
54+
/* Output:
55+
Windows 10
56+
toString: C:\path\to\nowhere\NoFile.txt
57+
Exists: false
58+
RegularFile: false
59+
Directory: false
60+
Absolute: true
61+
FileName: NoFile.txt
62+
Parent: C:\path\to\nowhere
63+
Root: C:\
64+
******************
65+
toString: PathInfo.java
66+
Exists: true
67+
RegularFile: true
68+
Directory: false
69+
Absolute: false
70+
FileName: PathInfo.java
71+
Parent: null
72+
Root: null
73+
******************
74+
toString: C:\Users\Bruce\Documents\GitHub\on-
75+
java\ExtractedExamples\files\PathInfo.java
76+
Exists: true
77+
RegularFile: true
78+
Directory: false
79+
Absolute: true
80+
FileName: PathInfo.java
81+
Parent: C:\Users\Bruce\Documents\GitHub\on-
82+
java\ExtractedExamples\files
83+
Root: C:\
84+
******************
85+
toString: C:\Users\Bruce\Documents\GitHub\on-
86+
java\ExtractedExamples\files
87+
Exists: true
88+
RegularFile: false
89+
Directory: true
90+
Absolute: true
91+
FileName: files
92+
Parent: C:\Users\Bruce\Documents\GitHub\on-
93+
java\ExtractedExamples
94+
Root: C:\
95+
******************
96+
toString: C:\Users\Bruce\Documents\GitHub\on-
97+
java\ExtractedExamples\files\PathInfo.java
98+
Exists: true
99+
RegularFile: true
100+
Directory: false
101+
Absolute: true
102+
FileName: PathInfo.java
103+
Parent: C:\Users\Bruce\Documents\GitHub\on-
104+
java\ExtractedExamples\files
105+
Root: C:\
106+
******************
107+
URI: file:///C:/Users/Bruce/Documents/GitHub/on-
108+
java/ExtractedExamples/files/PathInfo.java
109+
true
110+
*/

files/AddAndSubtractPaths.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,25 @@ public class AddAndSubtractPaths {
1010
.toAbsolutePath()
1111
.normalize();
1212
static void show(int id, Path result) {
13+
// System.err.println("AAA*---"+result);
1314
if(result.isAbsolute())
14-
System.err.println("(" + id + ")r " +
15+
System.err.println("(" + id + ")[relativize] " +
1516
base.relativize(result));
1617
else
1718
System.err.println("(" + id + ") " + result);
1819
try {
1920
System.err.println("RealPath: "
2021
+ result.toRealPath());
2122
} catch(IOException e) {
22-
System.err.println(e);
23+
System.err.println("--------------------------"+e);
2324
}
25+
System.err.println();
26+
System.err.println();
2427
}
2528
public static void main(String[] args) {
2629
System.err.println(System.getProperty("os.name"));
2730
System.err.println(base);
31+
2832
Path p = Paths.get("AddAndSubtractPaths.java")
2933
.toAbsolutePath();
3034
show(1, p);

0 commit comments

Comments
 (0)