-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileManager.java
228 lines (224 loc) · 7.02 KB
/
FileManager.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileManager
{
// (1)实现文件夹创建、删除;
// (2)实现当前文件夹下的内容罗列;
// (3)实现文件拷贝和文件夹拷贝(文件夹拷贝指深度拷贝,包括所有子目录和文件);
// (4)实现指定文件的加密和解密;
// (5)使用Eclipse、NetBeans或Intellj作为开发环境,使用Maven管理项目,使用Git进行源代码控制;
// (6)在实验报告中,对软件需求进行规范、详细的描述。
private String currPath;
public FileManager()
{
currPath = System.getProperty("user.dir");
}
public Boolean goTo(String path)
{
File dir = new File(path);
if(dir.exists() == false)
{
System.out.println("指定的文件夹"+path+"不存在。");
return false;
}
else
{
this.currPath = dir.getAbsolutePath();
System.out.println("已经前往"+this.currPath);
return true;
}
}
public boolean createDir(String name)
{
File dir = new File(this.currPath+File.separator+name);
if(dir.exists() == true)
{
System.out.println("指定的文件夹"+dir.getAbsolutePath()+"已存在。");
return false;
}
else
{
boolean res = dir.mkdirs();
if(res == false)
{
System.out.println("文件夹创建失败。");
}
return res;
}
}
public boolean deleteDir(String name)
{
File dir = new File(this.currPath+File.separator+name);
if(dir.exists() == false)
{
System.out.println("指定的文件夹不存在。");
return false;
}
else
{
boolean res = dir.delete();
if(res == false)
{
System.out.println("文件夹删除失败。");
}
return res;
}
}
public int listDir()
{
File dir = new File(this.currPath);
System.out.println("当前工作路径为:"+this.currPath);
File[] all = dir.listFiles();
for(File f : all)
{
System.out.println(f.getName());
}
return all.length;
}
public Boolean fileCopy(String ogName, String destName) throws IOException
{
File f = new File(this.currPath+File.separator+ogName);
FileInputStream fin = new FileInputStream(f);
FileOutputStream fout = new FileOutputStream(this.currPath+File.separator+destName);
if(f.exists() == false)
{
System.out.println("指定的源文件:"+ogName+"不存在。");
fin.close();
fout.close();
return false;
}
int n = fin.available() / 10;
int count = 0;
byte b[] = new byte[n];
while ((count = fin.read(b,0,n)) != -1)
{
fout.write(b, 0, count);
}
fin.close();
fout.flush();
fout.close();
return true;
}
private Boolean fileCopy(File og, File dest) throws IOException
{
FileInputStream fin = new FileInputStream(og);
FileOutputStream fout = new FileOutputStream(dest);
if(og.exists() == false)
{
System.out.println("指定的源文件:"+og.getAbsolutePath()+"不存在。");
fin.close();
fout.close();
return false;
}
int n = fin.available() / 10;
int count = 0;
byte b[] = new byte[n];
while ((count = fin.read(b,0,n)) != -1)
{
fout.write(b, 0, count);
}
fin.close();
fout.flush();
fout.close();
return true;
}
public boolean folderCopy(String ogName, String destPath, String newName) throws IOException
{
File f = new File(this.currPath+File.separator+ogName);
if(f.exists() == false || f.isDirectory() == false)
{
System.out.println("指定的源文件夹:"+ogName+"不存在。");
return false;
}
File dest = new File(destPath+File.separator+newName);
dest.mkdir();
galaxyFold(f, dest);
return true;
}
private void galaxyFold(File og, File dest) throws IOException
{
File[] all = og.listFiles();
if(all == null)
return;
else
{
for(File temp : all)
{
File dest2 = new File(dest, temp.getName());
if(temp.isDirectory() == true)
{
dest2.mkdir();
galaxyFold(temp, dest2);
}
else
{
fileCopy(temp, dest2);
}
}
}
}
public void encrypt(String ogName) throws IOException
{
byte pw = 6;
File x = new File(ogName);
if(x.exists() == false)
{
System.out.println("请求加密的文件不存在。");
return;
}
FileInputStream fin = new FileInputStream(this.currPath+File.separator+ogName);
FileOutputStream fout = new FileOutputStream(this.currPath+File.separator+ogName+"_encrypted");
int n = fin.available() / 10;
byte buf[] = new byte[n];
int count = 0;
while((count = fin.read(buf, 0, n))!=-1)
{
for(int i=0; i<count;i++)
{
buf[i]=(byte)(buf[i]^pw);
}
fout.write(buf, 0, count);
}
System.out.println("文件"+ogName+"加密成功");
fin.close();
fout.flush();
fout.close();
}
public void decrypt(String ogName, byte pw) throws IOException
{
File x = new File(ogName);
if(x.exists() == false)
{
System.out.println("请求解密的文件不存在。");
return;
}
FileInputStream fin = new FileInputStream(this.currPath+File.separator+ogName);
FileOutputStream fout = new FileOutputStream(this.currPath+File.separator+ogName+"_decrypted");
int n = fin.available() / 10;
byte buf[] = new byte[n];
int count = 0;
while((count = fin.read(buf, 0, n))!=-1)
{
for(int i=0; i<count;i++)
{
buf[i]=(byte)(buf[i]^pw);
}
fout.write(buf, 0, count);
}
System.out.println("文件"+ogName+"解密成功");
fin.close();
fout.flush();
fout.close();
}
public static void main(String[] args) throws IOException {
System.out.println("Hello World!");
FileManager fm = new FileManager();
//fm.goTo("C:\\Users\\surface\\Desktop");
//fm.createDir("koko");
//fm.fileCopy("og.txt", "dest.txt");
//fm.folderCopy("kk", "C:\\Users\\surface\\Desktop", "kjk");
fm.decrypt("dest.txt_encryped", (byte)6);
}
}