??2?/**?
??3??*?//FileOperate.java
??4??*?文件的各種操作
??5??*?楊彩?http://blog.sina.com.cn/m/yangcai
??6??*?文件操作?1.0
??7??*/?
??8??
??9?//package?common;?
?10??
?11?import?java.io.*;?
?12??
?13?public?class?FileOperate
?14?{
?15??static?boolean?exitnow=false;
?16??static?String?aa,bb;?
?17???public?FileOperate()?{?
?18???}?
?19??
?20???/**?
?21????*?新建目錄?
?22????*/?
?23???public?void?newFolder(String?folderPath)?{?
?24?????try?
?25?????{?
?26???????String?filePath?=?folderPath;?
?27???????filePath?=?filePath.toString();?
?28???????File?myFilePath?=?new?File(filePath);?
?29???????if(!myFilePath.exists())?
?30???????{?
?31?????????myFilePath.mkdir();
?32???????}
?33???????System.out.println("新建目錄操作?成功執行");?
?34?????}?
?35?????catch(Exception?e)
?36?????{?
?37???????System.out.println("新建目錄操作出錯");?
?38???????e.printStackTrace();?
?39?????}?
?40???}?
?41??
?42???/**?
?43????*?新建文件?
?44????*/?
?45???public?void?newFile(String?filePathAndName,?String?fileContent)
?46???{?
?47??
?48?????try
?49?????{?
?50???????String?filePath?=?filePathAndName;?
?51???????filePath?=?filePath.toString();?
?52???????File?myFilePath?=?new?File(filePath);?
?53???????if?(!myFilePath.exists())
?54???????{?
?55?????????myFilePath.createNewFile();?
?56???????}?
?57???????FileWriter?resultFile?=?new?FileWriter(myFilePath);?
?58???????PrintWriter?myFile?=?new?PrintWriter(resultFile);?
?59???????String?strContent?=?fileContent;?
?60???????myFile.println(strContent);?
?61???????resultFile.close();?
?62???????System.out.println("新建文件操作?成功執行");?
?63?????}?
?64?????catch?(Exception?e)?{?
?65???????System.out.println("新建目錄操作出錯");?
?66???????e.printStackTrace();?
?67??
?68?????}?
?69??
?70???}?
?71??
?72???/**?
?73????*?刪除文件?
?74????*/?
?75???public?void?delFile(String?filePathAndName)?{?
?76?????try?{?
?77???????String?filePath?=?filePathAndName;?
?78???????filePath?=?filePath.toString();?
?79???????File?myDelFile?=?new?File(filePath);?
?80???????myDelFile.delete();?
?81???????System.out.println("刪除文件操作?成功執行");?
?82?????}?
?83?????catch?(Exception?e)?{?
?84???????System.out.println("刪除文件操作出錯");?
?85???????e.printStackTrace();?
?86??
?87?????}?
?88??
?89???}?
?90??
?91???/**?
?92????*?刪除文件夾?
?93????*/?
?94???public?void?delFolder(String?folderPath)
?95???{?
?96?????try
?97?????{?
?98???????delAllFile(folderPath);?//刪除完里面所有內容?
?99???????String?filePath?=?folderPath;?
100???????filePath?=?filePath.toString();?
101???????File?myFilePath?=?new?File(filePath);?
102???????myFilePath.delete();?//刪除空文件夾?
103???????System.out.println("刪除文件夾操作?成功執行");?
104?????}?
105?????catch?(Exception?e)
106?????{?
107???????System.out.println("刪除文件夾操作出錯");?
108???????e.printStackTrace();?
109??
110?????}?
111??
112???}?
113??
114???/**?
115????*?刪除文件夾里面的所有文件?
116????*?@param?path?String?文件夾路徑?如?c:/fqf?
117????*/?
118???public?void?delAllFile(String?path)
119???{?
120?????File?file?=?new?File(path);?
121?????if(!file.exists())
122?????{?
123???????return;?
124?????}?
125?????if(!file.isDirectory())
126?????{?
127???????return;?
128?????}?
129?????String[]?tempList?=?file.list();?
130?????File?temp?=?null;?
131?????for?(int?i?=?0;?i?<?tempList.length;?i++)
132?????{?
133???????if(path.endsWith(File.separator))
134???????{?
135?????????temp?=?new?File(path?+?tempList[i]);?
136???????}?
137???????else
138???????{?
139?????????temp?=?new?File(path?+?File.separator?+?tempList[i]);?
140???????}?
141???????if?(temp.isFile())
142???????{?
143?????????temp.delete();?
144???????}?
145???????if?(temp.isDirectory())
146???????{?
147?????????delAllFile(path+"/"+?tempList[i]);//先刪除文件夾里面的文件?
148?????????delFolder(path+"/"+?tempList[i]);//再刪除空文件夾?
149???????}?
150?????}
151???????????System.out.println("刪除文件操作?成功執行");??
152???}?
153??
154???/**?
155????*?復制單個文件?
156????*?@param?oldPath?String?原文件路徑?如:c:/fqf.txt?
157????*?@param?newPath?String?復制后路徑?如:f:/fqf.txt?
158????*/?
159???public?void?copyFile(String?oldPath,?String?newPath)?{?
160?????try?{?
161???????int?bytesum?=?0;?
162???????int?byteread?=?0;?
163???????File?oldfile?=?new?File(oldPath);?
164???????if?(oldfile.exists())
165???????{?//文件存在時?
166?????????InputStream?inStream?=?new?FileInputStream(oldPath);?//讀入原文件?
167?????????FileOutputStream?fs?=?new?FileOutputStream(newPath);?
168?????????byte[]?buffer?=?new?byte[1444];?
169?????????int?length;?
170?????????while?(?(byteread?=?inStream.read(buffer))?!=?-1)?{?
171???????????bytesum?+=?byteread;?//字節數?文件大小?
172???????????System.out.println(bytesum);?
173???????????fs.write(buffer,?0,?byteread);?
174?????????}?
175?????????inStream.close();?
176???????}
177?????????????System.out.println("刪除文件夾操作?成功執行");??
178?????}?
179?????catch?(Exception?e)?{?
180???????System.out.println("復制單個文件操作出錯");?
181???????e.printStackTrace();?
182??
183?????}?
184??
185???}?
186??
187???/**?
188????*?復制整個文件夾內容?
189????*?@param?oldPath?String?原文件路徑?如:c:/fqf?
190????*?@param?newPath?String?復制后路徑?如:f:/fqf/ff?
191????*/?
192???public?void?copyFolder(String?oldPath,?String?newPath)?{?
193??
194?????try
195?????{?
196???????(new?File(newPath)).mkdirs();?//如果文件夾不存在?則建立新文件夾?
197???????File?a=new?File(oldPath);?
198???????String[]?file=a.list();?
199???????File?temp=null;?
200???????for?(int?i?=?0;?i?<?file.length;?i++)
201???????{?
202?????????if(oldPath.endsWith(File.separator))
203?????????{?
204???????????temp=new?File(oldPath+file[i]);?
205?????????}?
206?????????else{?
207???????????temp=new?File(oldPath+File.separator+file[i]);?
208?????????}?
209??
210?????????if(temp.isFile())
211?????????{?
212???????????FileInputStream?input?=?new?FileInputStream(temp);?
213???????????FileOutputStream?output?=?new?FileOutputStream(newPath?+?"/"?+?
214???????????????(temp.getName()).toString());?
215???????????byte[]?b?=?new?byte[1024?*?5];?
216???????????int?len;?
217???????????while?(?(len?=?input.read(b))?!=?-1)
218???????????{?
219?????????????output.write(b,?0,?len);?
220???????????}?
221???????????output.flush();?
222???????????output.close();?
223???????????input.close();?
224?????????}?
225?????????if(temp.isDirectory())
226?????????{//如果是子文件夾?
227???????????copyFolder(oldPath+"/"+file[i],newPath+"/"+file[i]);?
228?????????}?
229???????}
230?????????????System.out.println("復制文件夾操作?成功執行");??
231?????}?
232?????catch?(Exception?e)?{?
233???????System.out.println("復制整個文件夾內容操作出錯");?
234???????e.printStackTrace();?
235??
236?????}?
237??
238???}?
239??
240???/**?
241????*?移動文件到指定目錄?
242????*?@param?oldPath?String?如:c:/fqf.txt?
243????*?@param?newPath?String?如:d:/fqf.txt?
244????*/?
245???public?void?moveFile(String?oldPath,?String?newPath)?{?
246?????copyFile(oldPath,?newPath);?
247?????delFile(oldPath);?
248??
249???}?
250??
251???/**?
252????*?移動文件到指定目錄?
253????*?@param?oldPath?String?如:c:/fqf.txt?
254????*?@param?newPath?String?如:d:/fqf.txt?
255????*/?
256???public?void?moveFolder(String?oldPath,?String?newPath)?{?
257?????copyFolder(oldPath,?newPath);?
258?????delFolder(oldPath);?
259??
260???}
261???
262???public?static?void?main(String?args[])
263???{
264????System.out.println("使用此功能請按[1]??功能一:新建目錄");
265????System.out.println("使用此功能請按[2]??功能二:新建文件");
266????System.out.println("使用此功能請按[3]??功能三:刪除文件");
267????System.out.println("使用此功能請按[4]??功能四:刪除文件夾");
268????System.out.println("使用此功能請按[5]??功能五:刪除文件夾里面的所有文件");
269????System.out.println("使用此功能請按[6]??功能六:復制文件");
270????System.out.println("使用此功能請按[7]??功能七:復制文件夾的所有內容");
271????System.out.println("使用此功能請按[8]??功能八:移動文件到指定目錄");
272????System.out.println("使用此功能請按[9]??功能九:移動文件夾到指定目錄");
273????System.out.println("使用此功能請按[10]?退出程序");
274????
275??while(!exitnow)
276??{
277?????FileOperate?fo=new?FileOperate();
278?????try
279?????{
280?????BufferedReader?Bin=new?BufferedReader(new?InputStreamReader(System.in));
281?????String?a=Bin.readLine();
282?????int?b=Integer.parseInt(a);
283?????
284?????switch(b)?
285?????{
286??????case?1:System.out.println("你選擇了功能一??請輸入目錄名");??
287?????????aa=Bin.readLine();
288?????????fo.newFolder(aa);
289?????????break;
290??????case?2:System.out.println("你選擇了功能二??請輸入文件名");??
291?????????aa=Bin.readLine();
292?????????System.out.println("請輸入在"+aa+"中的內容");
293?????????bb=Bin.readLine();
294?????????fo.newFile(aa,bb);
295?????????break;
296??????case?3:System.out.println("你選擇了功能三??請輸入文件名");??
297?????????aa=Bin.readLine();
298?????????fo.delFile(aa);
299?????????break;
300??????case?4:System.out.println("你選擇了功能四??請輸入文件名");??
301?????????aa=Bin.readLine();
302?????????fo.delFolder(aa);
303?????????break;
304??????case?5:System.out.println("你選擇了功能五??請輸入文件名");??
305?????????aa=Bin.readLine();
306?????????fo.delAllFile(aa);
307?????????break;???
308??????case?6:System.out.println("你選擇了功能六??請輸入文件名");??
309?????????aa=Bin.readLine();
310?????????System.out.println("請輸入目標文件名");?
311?????????bb=Bin.readLine();
312?????????fo.copyFile(aa,bb);
313?????????break;
314??????case?7:System.out.println("你選擇了功能七??請輸入源文件名");??
315?????????aa=Bin.readLine();
316?????????System.out.println("請輸入目標文件名");?
317?????????bb=Bin.readLine();
318?????????fo.copyFolder(aa,bb);
319?????????break;????????
320??????case?8:System.out.println("你選擇了功能八??請輸入源文件名");??
321?????????aa=Bin.readLine();
322?????????System.out.println("請輸入目標文件名");?
323?????????bb=Bin.readLine();
324?????????fo.moveFile(aa,bb);
325?????????break;
326????????case?9:System.out.println("你選擇了功能九??請輸入源文件名");??
327?????????aa=Bin.readLine();
328?????????System.out.println("請輸入目標文件名");?
329?????????bb=Bin.readLine();
330?????????fo.moveFolder(aa,bb);
331?????????break;????????
332??????case?10:exitnow=true;
333??????????System.out.println("程序結束,請退出");
334?????????break;
335??????default:System.out.println("輸入錯誤.請輸入1-10之間的數");???????????????
336??????}
337?????
338?????
339?????System.out.println("請重新選擇功能");
340?????
341?????
342?????}
343?????catch(Exception?e)
344?????{
345?????System.out.println("輸入錯誤字符或程序出錯");
346?????}
347?????
348??}???
349??}?
350?}