Skip to content

Commit f0c5991

Browse files
committed
Polish Stage 8 Lesson 3
1 parent cb00a74 commit f0c5991

File tree

8 files changed

+272
-0
lines changed

8 files changed

+272
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.segmentfault.deep.in.java.newfilesystem;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.nio.file.FileSystems;
6+
import java.nio.file.Files;
7+
import java.nio.file.Path;
8+
import java.nio.file.Paths;
9+
import java.util.stream.Stream;
10+
11+
public class DirectoryOperationsDemo {
12+
13+
public static void main(String[] args) {
14+
String classPath = System.getProperty("java.class.path");
15+
Stream.of(classPath.split(File.pathSeparator))
16+
.map(Paths::get) // String -> Path
17+
.filter(Files::isDirectory) // 过滤目录
18+
.filter(Files::isReadable)
19+
.filter(Files::isWritable)
20+
.map(Path::toString) // Path -> String
21+
.map(dirPath -> Paths.get(dirPath, "parent-dir", "sub-dir")) // Path -> new Path
22+
.forEach(newDir -> {
23+
try {
24+
Path newDirectory = Files.createDirectories(newDir);
25+
System.out.printf("新的目录[%s] 已被创建!\n", newDirectory);
26+
} catch (IOException e) {
27+
e.printStackTrace();
28+
}
29+
});
30+
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.segmentfault.deep.in.java.newfilesystem;
2+
3+
import java.io.IOException;
4+
import java.nio.file.Files;
5+
import java.nio.file.Path;
6+
import java.nio.file.Paths;
7+
import java.nio.file.attribute.UserPrincipal;
8+
9+
import static com.segmentfault.deep.in.java.newfilesystem.PathDemo.USER_DIR_LOCATION;
10+
11+
public class FileMetadataDemo {
12+
13+
public static void main(String[] args) throws IOException {
14+
Path path = Paths.get(USER_DIR_LOCATION);
15+
UserPrincipal userPrincipal = Files.getOwner(path);
16+
System.out.printf("Path[%s]'s owner is : %s\n", path, userPrincipal);
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.segmentfault.deep.in.java.newfilesystem;
2+
3+
import java.io.IOException;
4+
import java.nio.file.Files;
5+
import java.nio.file.Path;
6+
import java.nio.file.Paths;
7+
8+
import static com.segmentfault.deep.in.java.newfilesystem.PathDemo.USER_DIR_LOCATION;
9+
10+
public class FileOperationsDemo {
11+
12+
public static void main(String[] args) throws Exception {
13+
displayFileExists();
14+
displayFileAccessibility();
15+
displayFileEquals();
16+
}
17+
18+
private static void displayFileEquals() throws IOException {
19+
Path path = Paths.get(USER_DIR_LOCATION);
20+
Path path2 = Paths.get(USER_DIR_LOCATION);
21+
System.out.println(Files.isSameFile(path, path2));
22+
}
23+
24+
private static void displayFileAccessibility() {
25+
Path path = Paths.get(USER_DIR_LOCATION);
26+
System.out.printf("${user.dir} : %s , readable = %s , writable = %s , executable : %s \n",
27+
path,
28+
Files.isReadable(path),
29+
Files.isWritable(path),
30+
Files.isExecutable(path)
31+
);
32+
}
33+
34+
private static void displayFileExists() {
35+
Path path = Paths.get(USER_DIR_LOCATION);
36+
System.out.printf("${user.dir}: %s exists = %s\n", path, Files.exists(path));
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.segmentfault.deep.in.java.newfilesystem;
2+
3+
import java.io.IOException;
4+
import java.nio.Buffer;
5+
import java.nio.ByteBuffer;
6+
import java.nio.channels.SeekableByteChannel;
7+
import java.nio.charset.Charset;
8+
import java.nio.file.Files;
9+
import java.nio.file.Path;
10+
import java.nio.file.Paths;
11+
import java.nio.file.StandardOpenOption;
12+
13+
import static com.segmentfault.deep.in.java.newfilesystem.PathDemo.USER_DIR_LOCATION;
14+
15+
public class FileOperationsUsingByteChannelDemo {
16+
17+
public static void main(String[] args) {
18+
Charset charset = Charset.forName("UTF-8");
19+
Path pomXmlPath = Paths.get(USER_DIR_LOCATION, "pom.xml");
20+
Path pomCopyXmlPath = Paths.get(USER_DIR_LOCATION, "pom-copy.xml");
21+
try (SeekableByteChannel sourceByteChannel = Files.newByteChannel(pomXmlPath);
22+
SeekableByteChannel targetByteChannel = Files.newByteChannel(pomCopyXmlPath, StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE);
23+
) {
24+
ByteBuffer byteBuffer = ByteBuffer.allocate(16);
25+
while (sourceByteChannel.read(byteBuffer) > 0) {
26+
byteBuffer.rewind();
27+
// System.out.print(charset.decode(byteBuffer));
28+
targetByteChannel.write(byteBuffer);
29+
byteBuffer.flip();
30+
}
31+
} catch (IOException e) {
32+
33+
}
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.segmentfault.deep.in.java.newfilesystem;
2+
3+
import java.io.IOException;
4+
import java.nio.file.FileSystems;
5+
import java.nio.file.Files;
6+
import java.nio.file.Paths;
7+
8+
import static com.segmentfault.deep.in.java.newfilesystem.PathDemo.USER_DIR_LOCATION;
9+
10+
public class FindCommandDemo {
11+
12+
public static void main(String[] args) throws IOException {
13+
String pattern = "F[a-zA-Z]*.java";
14+
FindVisitor findVisitor = new FindVisitor(pattern);
15+
Files.walkFileTree(Paths.get(USER_DIR_LOCATION), findVisitor);
16+
System.out.printf("Found count : %s\n", findVisitor.getFoundCount());
17+
}
18+
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.segmentfault.deep.in.java.newfilesystem;
2+
3+
import java.nio.file.*;
4+
import java.nio.file.attribute.BasicFileAttributes;
5+
6+
public class FindVisitor extends SimpleFileVisitor<Path> {
7+
8+
private final PathMatcher pathMatcher;
9+
10+
private int foundCount;
11+
12+
public FindVisitor(String pattern) {
13+
this.pathMatcher = FileSystems.getDefault().getPathMatcher("glob:" + pattern);
14+
}
15+
16+
@Override
17+
public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) {
18+
matchFile(file);
19+
return FileVisitResult.CONTINUE;
20+
}
21+
22+
@Override
23+
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attributes) {
24+
matchFile(dir);
25+
return FileVisitResult.CONTINUE;
26+
}
27+
28+
private void matchFile(Path file) {
29+
Path name = file.getFileName();
30+
if (name != null && pathMatcher.matches(name)) {
31+
foundCount++;
32+
System.out.printf("Found file : %s\n", file);
33+
}
34+
}
35+
36+
public int getFoundCount() {
37+
return foundCount;
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.segmentfault.deep.in.java.newfilesystem;
2+
3+
import java.io.File;
4+
import java.nio.file.FileSystems;
5+
import java.nio.file.Path;
6+
import java.nio.file.Paths;
7+
8+
/**
9+
* Java 7 开始流行
10+
* 1. Fluent API(Builder API,Chain API)
11+
* 2. 工具 API(Objects
12+
*/
13+
public class PathDemo {
14+
15+
public static final String USER_DIR_LOCATION = System.getProperty("user.dir");
16+
17+
public static void main(String[] args) throws Exception {
18+
19+
// displayPathInfo();
20+
// displayPathNormalize();
21+
22+
displayPathConversation();
23+
}
24+
25+
private static void displayPathConversation() {
26+
Path pathFromLocation = Paths.get(USER_DIR_LOCATION);
27+
File file = new File(USER_DIR_LOCATION);
28+
Path pathFromFile = file.toPath();
29+
Path pathFromURI = Paths.get(pathFromFile.toUri());
30+
System.out.println("pathFromURL : " + pathFromURI);
31+
System.out.println("pathFromLocation : " + pathFromLocation);
32+
System.out.println("pathFromFile : " + pathFromFile);
33+
System.out.println("pathFromURL == pathFromLocation ? " + pathFromURI.equals(pathFromLocation));
34+
System.out.println("pathFromFile == pathFromLocation ? " + pathFromFile.equals(pathFromLocation));
35+
}
36+
37+
private static void displayPathNormalize() {
38+
Path path = Paths.get("D:\\workspace\\coures\\..\\");
39+
// D:\workspace\coures\java-new-filesystem
40+
// D:\workspace\coures\..\
41+
System.out.println(path.normalize());
42+
}
43+
44+
private static void displayPathInfo() {
45+
Path path = Paths.get(USER_DIR_LOCATION);
46+
System.out.printf("toString : %s\n", path);
47+
System.out.printf("getFileName : %s\n", path.getFileName());
48+
49+
int nameCount = path.getNameCount();
50+
for (int i = 0; i < nameCount; i++) {
51+
System.out.printf("getName(%d) : %s\n", i, path.getName(i));
52+
}
53+
for (Path p : path) {
54+
System.out.printf("For-Each path : %s\n", p);
55+
}
56+
System.out.printf("getParent : %s\n", path.getParent());
57+
System.out.printf("getRoot : %s\n", path.getRoot());
58+
59+
FileSystems.getDefault().getRootDirectories().forEach(System.out::println);
60+
}
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.segmentfault.deep.in.java.newfilesystem;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.nio.charset.Charset;
6+
import java.nio.file.Files;
7+
import java.nio.file.Path;
8+
import java.nio.file.Paths;
9+
10+
import static com.segmentfault.deep.in.java.newfilesystem.PathDemo.USER_DIR_LOCATION;
11+
12+
public class TryWithResourcesDemo {
13+
14+
public static void main(String[] args) {
15+
Path pomXmlPath = Paths.get(USER_DIR_LOCATION, "pom.xml");
16+
Charset charset = Charset.forName("UTF-8");
17+
try (BufferedReader reader = Files.newBufferedReader(pomXmlPath, charset)) {
18+
for (String s = reader.readLine(); s != null; s = reader.readLine()) {
19+
System.out.println(s);
20+
}
21+
} catch (IOException e) {
22+
System.err.format("IOException : %s\n", e.getMessage());
23+
}
24+
// finally {
25+
// if (reader != null) {
26+
// reader.close();
27+
// }
28+
// }
29+
}
30+
}

0 commit comments

Comments
 (0)