Skip to content

Commit 724ffb2

Browse files
committed
[add] add module java-io and add examples about File
1 parent 1b9e2a2 commit 724ffb2

File tree

4 files changed

+133
-0
lines changed

4 files changed

+133
-0
lines changed

java-io/pom.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>java-learning</artifactId>
7+
<groupId>com.brianway.learning.java</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>java-io</artifactId>
13+
14+
</project>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.brianway.learning.java.io;
2+
3+
import java.io.File;
4+
import java.io.FilenameFilter;
5+
import java.util.Arrays;
6+
import java.util.regex.Pattern;
7+
8+
/**
9+
* Created by brian on 16/11/28.
10+
* 列举文件/文件夹
11+
*/
12+
public class DirList {
13+
public static void main(final String[] args) {
14+
File path = new File(".");
15+
String[] list;
16+
if (args.length == 0) {
17+
list = path.list();
18+
} else {
19+
list = path.list(new FilenameFilter() {
20+
private Pattern pattern = Pattern.compile(args[0]); // final String[] args
21+
22+
public boolean accept(File dir, String name) {
23+
return pattern.matcher(name).matches();
24+
}
25+
});
26+
}
27+
Arrays.sort(list, String.CASE_INSENSITIVE_ORDER);
28+
for (String dirItem : list) {
29+
System.out.println(dirItem);
30+
}
31+
}
32+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package com.brianway.learning.java.io;
2+
//: io/MakeDirectories.java
3+
// Demonstrates the use of the File class to
4+
// create directories and manipulate files.
5+
// {Args: MakeDirectoriesTest}
6+
7+
import java.io.File;
8+
9+
public class MakeDirectories {
10+
private static void usage() {
11+
System.err.println(
12+
"Usage:MakeDirectories path1 ...\n" +
13+
"Creates each path\n" +
14+
"Usage:MakeDirectories -d path1 ...\n" +
15+
"Deletes each path\n" +
16+
"Usage:MakeDirectories -r path1 path2\n" +
17+
"Renames from path1 to path2");
18+
System.exit(1);
19+
}
20+
21+
private static void fileData(File f) {
22+
System.out.println(
23+
"Absolute path: " + f.getAbsolutePath() +
24+
"\n Can read: " + f.canRead() +
25+
"\n Can write: " + f.canWrite() +
26+
"\n getName: " + f.getName() +
27+
"\n getParent: " + f.getParent() +
28+
"\n getPath: " + f.getPath() +
29+
"\n length: " + f.length() +
30+
"\n lastModified: " + f.lastModified());
31+
if (f.isFile()) {
32+
System.out.println("It's a file");
33+
} else if (f.isDirectory()) {
34+
System.out.println("It's a directory");
35+
}
36+
}
37+
38+
public static void main(String[] args) {
39+
if (args.length < 1) usage();
40+
if (args[0].equals("-r")) {
41+
if (args.length != 3) usage();
42+
File
43+
old = new File(args[1]),
44+
rname = new File(args[2]);
45+
old.renameTo(rname);
46+
fileData(old);
47+
fileData(rname);
48+
return; // Exit main
49+
}
50+
int count = 0;
51+
boolean del = false;
52+
if (args[0].equals("-d")) {
53+
count++;
54+
del = true;
55+
}
56+
count--;
57+
while (++count < args.length) {
58+
File f = new File(args[count]);
59+
if (f.exists()) {
60+
System.out.println(f + " exists");
61+
if (del) {
62+
System.out.println("deleting..." + f);
63+
f.delete();
64+
}
65+
} else { // Doesn't exist
66+
if (!del) {
67+
f.mkdirs();
68+
System.out.println("created " + f);
69+
}
70+
}
71+
fileData(f);
72+
}
73+
}
74+
}
75+
/* Output: (80% match)
76+
created MakeDirectoriesTest
77+
Absolute path: d:\aaa-TIJ4\code\io\MakeDirectoriesTest
78+
Can read: true
79+
Can write: true
80+
getName: MakeDirectoriesTest
81+
getParent: null
82+
getPath: MakeDirectoriesTest
83+
length: 0
84+
lastModified: 1101690308831
85+
It's a directory
86+
*///:~

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
<module>java-multithread</module>
4848
<module>java-base</module>
4949
<module>java-container</module>
50+
<module>java-io</module>
5051
</modules>
5152

5253
<dependencyManagement>

0 commit comments

Comments
 (0)