forked from SafeGroceryStore/MDUT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileUtil.java
140 lines (131 loc) · 4.65 KB
/
FileUtil.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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import java.io.*;
import java.text.SimpleDateFormat;
public class FileUtil extends Object {
public static String run(String methodName, String params, String encoding) {
String r = "";
if (methodName.equalsIgnoreCase("listfile")) {
r = FileUtil.listfile(params, encoding);
} else if (methodName.equalsIgnoreCase("getpath")) {
r = FileUtil.getPath();
} else if (methodName.equalsIgnoreCase("readfile")) {
r = FileUtil.readFile(params);
}else if (methodName.equalsIgnoreCase("writefile")) {
String fp = params.substring(0, params.indexOf("^"));
String fc = params.substring(params.indexOf("^") + 1);
r = FileUtil.writeFile(fp,fc);
} else if (methodName.equalsIgnoreCase("listdiver")) {
r = WwwRootPathCode();
} else if (methodName.equalsIgnoreCase("deletefile")) {
r = deleteFile(params);
} else {
r = "unkown methodName";
}
return r;
}
public static String WwwRootPathCode() {
String d = System.getProperty("user.dir");
StringBuilder s = new StringBuilder();
if (!d.startsWith("/")) {
try {
File[] roots = File.listRoots();
for (File root : roots) {
s.append(root.toString(), 0, 2);
}
} catch (Exception e) {
s.append("/");
}
} else {
s.append("/");
}
return s.toString();
}
public static String listfile(String dirPath, String encoding){
if (encoding == null || encoding.equals("")) {
encoding = "utf-8";
}
String r = "";
File oF = new File(dirPath), l[] = oF.listFiles();
String s = "", sT, sQ, sF = "";
java.util.Date dt;
String fileCode=(String)System.getProperties().get("file.encoding");
SimpleDateFormat fm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
for (int i = 0; i < l.length; i++) {
dt = new java.util.Date(l[i].lastModified());
sT = fm.format(dt);
sQ = l[i].canRead() ? "R" : "";
sQ += l[i].canWrite() ? " W" : "";
if("".equals(sQ)){
sQ = "Unknown";
}
String nm = new String(l[i].getName().getBytes(fileCode), encoding);
if (l[i].isDirectory()) {
s += nm + "/\t" + sT + "\t" + l[i].length() + "\t" + sQ + "\n";
} else {
sF += nm + "\t" + sT + "\t" + l[i].length() + "\t" + sQ + "\n";
}
}
s += sF;
r = new String(s.getBytes(fileCode), encoding);
}catch (Exception e){
}
return r;
}
public static String getPath() {
String result = "";
File directory = new File("");
try {
result = directory.getAbsolutePath();
} catch (Exception e) {
}
return result;
}
public static String readFile(String filePath){
StringBuffer sb = new StringBuffer();
DataInputStream input = null;
try {
input = new DataInputStream(new FileInputStream(filePath));
while (input.available() > 0) {
String hex = String.format("%02x", input.readByte() & 0xFF);
sb.append(hex);
}
} catch (Exception e) {
sb.append(e.toString());
}
//System.out.println(sb.toString());
return sb.toString();
}
public static String writeFile(String filePath, String fileContext){
String r = "ok";
try {
String h = "0123456789ABCDEF";
String fileHexContext = fileContext;
File f = new File(filePath);
FileOutputStream os = null;
os = new FileOutputStream(f);
for (int i = 0; i < fileHexContext.length(); i += 2) {
os.write((h.indexOf(fileHexContext.charAt(i)) << 4 | h.indexOf(fileHexContext.charAt(i + 1))));
}
os.close();
} catch (Exception e) {
}
return r;
}
public static String deleteFile(String path) {
StringBuffer sb = new StringBuffer();
File f = new File(path);
if (f.exists()) {
if (f.delete()) {
sb.append("success");
} else {
sb.append("fail");
}
} else {
sb.append("error");
}
return sb.toString();
}
public static void main(String[] args) {
System.out.println(listfile("/",""));
}
}