This repository has been archived by the owner on Dec 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
FileHandle.java
129 lines (105 loc) · 3.48 KB
/
FileHandle.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
import java.io.*;
public class FileHandle {
private File file;
public FileHandle(String file) {
this.file = new File(file);
}
public FileHandle(File file) {
this.file = file;
}
public FileHandle(String dir, String file) {
this.file = new File(dir, file);
}
public FileHandle(File dir, String file) {
this.file = new File(dir, file);
}
public File file() {
return file;
}
public FileHandle parent() {
return new FileHandle(file.getParentFile());
}
public boolean exists() {
return file.exists();
}
public void delete() {
file.delete();
{
public void mkdir() throws Exception {
if (file.exists()) {
if (file.isFile()) throw new Exception(file.getName() + " exists and is not a directory.");
} else if (!file.mkdirs()) {
throw new Exception("Unable to create directory " + file.getName());
}
}
public boolean deldir() {
return deldir(file);
}
private boolean deldir(File file) {
if(file.exists()) {
File[] files = file.listFiles();
if(files !=null) {
for(File fl: files) {
if(fl.isDirectory()) {
deldir(fl);
} else {
fl.delete();
}
}
}
}
return file.delete();
}
public void copy(InputStream in) throws Exception {
OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
try {
byte[] buffer = new byte[1024 * 4];
int n = 0;
while (-1 != (n = in.read(buffer))) out.write(buffer, 0, n);
} finally {
try { if (out != null) out.close(); }
catch (IOException ioe) { }
}
}
public String read() throws Exception {
InputStream in = new FileInputStream(file);
final StringBuffer sBuffer = new StringBuffer();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
final char[] buffer = new char[1024];
int cnt;
while ((cnt = br.read(buffer, 0, buffer.length)) > -1) {
sBuffer.append(buffer, 0, cnt);
}
br.close();
in.close();
return sBuffer.toString();
}
public void copydir(String targetLocation) throws IOException {
copydir(file, new File(targetLocation));
}
public void copydir(File targetLocation) throws IOException {
copydir(file, targetLocation);
}
private void copydir(File file, File targetLocation) throws IOException {
if (file.isDirectory()) {
if (!targetLocation.exists()) {
targetLocation.mkdir();
}
String[] children = file.list();
for (int i=0; i<children.length; i++) {
copydir(new File(file, children[i]),
new File(targetLocation, children[i]));
}
} else {
InputStream in = new FileInputStream(file);
OutputStream out = new FileOutputStream(targetLocation);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
}
}
}