-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMakeDirectories.java
More file actions
102 lines (91 loc) · 2.78 KB
/
MakeDirectories.java
File metadata and controls
102 lines (91 loc) · 2.78 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package io;
import java.io.*;
import java.util.*;
import java.util.regex.*;
/**
* RUN:
* javac io/MakeDirectories.java && java io.MakeDirectories MakeDirectoriesTest
*
* OUTPUT:
* created MakeDirectoriesTest
* Full name: D:\\univer\java\\thinking_in_java\\MakeDirectoriesTest
* awailable for reading: true
* awailable for writing: true
* file name getName(): MakeDirectoriesTest
* parent directory getParent(): null
* path getPath(): MakeDirectoriesTest
* size: 0
* last modified date: 1425444969289
* Is directory
*/
public class MakeDirectories {
private static void usage() {
System.err.println(
"Using: MakeDirectories path1 ...\n" +
"Create all paths\n" +
"Using: MakeDirectories -d paht1 ...\n" +
"Remove all paths\n" +
"Using: MakeDirectories -r path1 path2\n" +
"Rename path1 to path2\n"
);
System.exit(1);
}
private static void fileData(File f) {
System.out.println(
"Full name: " + f.getAbsolutePath() +
"\n awailable for reading: " + f.canRead() +
"\n awailable for writing: " + f.canWrite() +
"\n file name getName(): " + f.getName() +
"\n parent directory getParent(): " + f.getParent() +
"\n path getPath(): " + f.getPath() +
"\n size: " + f.length() +
"\n last modified date: " + f.lastModified()
);
if (f.isFile()) {
System.out.println("Is file");
}
else if (f.isDirectory()) {
System.out.println("Is directory");
}
}
public static void main(String[] args) {
if (args.length < 1) {
usage();
}
if (args[0].equals("-r")) {
if (args.length != 3) {
usage();
}
File old = new File(args[1]);
File rname = new File(args[2]);
old.renameTo(rname);
fileData(old);
fileData(rname);
return;
}
int count = 0;
boolean del = false;
if (args[0].equals("-d")) {
count++;
del = true;
}
count--;
while (++count < args.length) {
File f = new File(args[count]);
if (f.exists()) {
System.out.println(f + " exists");
if (del) {
System.out.println("deleting "+f);
f.delete();
}
}
else {
if (! del) {
f.mkdirs();
System.out.println("created " + f);
}
}
fileData(f);
}
}
}