finalFile writeFile = newFile(Filepath);
try {
if (!writeFile.exists()) {
writeFile.getParentFile().mkdirs();
writeFile.createNewFile();
}else{
if(writeFile.length()>0){
writeFile.delete();
writeFile.createNewFile();
}
}
1. //1.创建文件夹
2. //import java.io.*;
3. File myFolderPath = new File(str1);
4. try {
5. if (!myFolderPath.exists()) {
6. //按照myFolderPath来创建该目录下的文件夹
7. myFolderPath.mkdir();
8. }
9. }
10.catch (Exception e) {
11. System.out.println("新建目录操作出错");
12. e.printStackTrace();
13.}
14.
15.//2.创建文件
16.//import java.io.*;
17.File myFilePath = new File(str1);
18.try {
19. if (!myFilePath.exists()) {
20. myFilePath.createNewFile();
21. }
22. FileWriter resultFile = new FileWriter(myFilePath);
23. PrintWriter myFile = new PrintWriter(resultFile);
24. myFile.println(str2);
25. resultFile.close();
26.}
27.catch (Exception e) {
28. System.out.println("新建文件操作出错");
29. e.printStackTrace();
30.}
31.
32.//3.删除文件
33.//import java.io.*;
34.File myDelFile = new File(str1);
35.try {
36. myDelFile.delete();
37.}
38.catch (Exception e) {
39. System.out.println("删除文件操作出错");
40. e.printStackTrace();
41.}
42.
43.//4.删除文件夹
44.//import java.io.*;
45.File delFolderPath = new File(str1);
46.try {
47. delFolderPath.delete(); //删除空文件夹
48.}
49.catch (Exception e) {
50. System.out.println("删除文件夹操作出错");
51. e.printStackTrace();
52.}
53.
54.//5.删除一个文件下夹所有的文件夹
55.//import java.io.*;
56.File delfile=new File(str1);
57.File[] files=delfile.listFiles();
58.for(int i=0;i<files.length;i++){
59. if(files[i].isDirectory()){
60. files[i].delete();
61. }
62.}
63.Plsnote:
64. String path="C:/windows/Help"; //我随便给的一个目录 File f=new File(path); //new的一个File对象 if(f.isDirectory()){ //如果path表示的是一个目录则返回true .... }
65.
66.
67.//6.清空文件夹
68.//import java.io.*;
69.File delfilefolder=new File(str1);
70.try {
71. if (!delfilefolder.exists()) {
72. delfilefolder.delete();
73. }
74. delfilefolder.mkdir();
75.}
76.catch (Exception e) {
77. System.out.println("清空目录操作出错");
78. e.printStackTrace();
79.}
80.
81.//7.读取文件
82.//import java.io.*;
83.// 逐行读取数据
84.FileReader fr = new FileReader(str1);
85.BufferedReader br = new BufferedReader(fr);
86.String str2 = br.readLine();
87.while (str2 != null) {
88. str3
89. str2 = br.readLine();
90.}
91.br.close();
92.fr.close();
93.
94.//8.写入文件
95.//import java.io.*;
96.// 将数据写入文件
97.try {
98. FileWriter fw = new FileWriter(str1);
99. fw.write(str2);
100. fw.flush();
101. fw.close();
102.} catch (IOException e) {
103. e.printStackTrace();
104.}
105.
106.//9.写入随机文件
107.//import java.io.*;
108.try {
109. RandomAcessFile logFile=new RandomAcessFile(str1,"rw");
110. long lg=logFile.length();
111. logFile.seek(str2);
112. logFile.writeByte(str3);
113.}catch(IOException ioe){
114. System.out.println("无法写入文件:"+ioe.getMessage());
115.}
116.
117.//10.读取文件属性
118.//import java.io.*;
119.// 文件属性的取得
120.File f = new File(str1);
121.if (af.exists()) {
122. System.out.println(f.getName() + "的属性如下: 文件长度为:" + f.length());
123. System.out.println(f.isFile() ? "是文件" : "不是文件");
124. System.out.println(f.isDirectory() ? "是目录" : "不是目录");
125. System.out.println(f.canRead() ? "可读取" : "不");
126. System.out.println(f.canWrite() ? "是隐藏文件" : "");
127. System.out.println("文件夹的最后修改日期为:" + new Date(f.lastModified()));
128. } else {
129. System.out.println(f.getName() + "的属性如下:");
130. System.out.println(f.isFile() ? "是文件" : "不是文件");
131. System.out.println(f.isDirectory() ? "是目录" : "不是目录");
132. System.out.println(f.canRead() ? "可读取" : "不");
133. System.out.println(f.canWrite() ? "是隐藏文件" : "");
134. System.out.println("文件的最后修改日期为:" + new Date(f.lastModified()));
135.}
136.if(f.canRead()){
137. str2
138.}
139.if(f.canWrite()){
140. str3
141.}
142.
143.//11.写入属性
144.//import java.io.*;
145.File filereadonly=new File(str1);
146.try {
147. boolean b=filereadonly.setReadOnly();
148.}
149.catch (Exception e) {
150. System.out.println("拒绝写访问:"+e.printStackTrace());
151.}
152.
153.//12.枚举一个文件夹中的所有文件
154.//import java.io.*;
155.//import java.util.*;
156.LinkedList<String> folderList = new LinkedList<String>();
157.folderList.add(str1);
158.while (folderList.size() > 0) {
159. File file = new File(folderList.peek());
160. folderList.removeLast();
161. File[] files = file.listFiles();
162. ArrayList<File> fileList = new ArrayList<File>();
163. for (int i = 0; i < files.length; i++) {
164. if (files[i].isDirectory()) {
165. folderList.add(files[i].getPath());
166. } else {
167. fileList.add(files[i]);
168. }
169. }
170. for (File f : fileList) {
171. str2=f.getAbsoluteFile();
172. str3
173. }
174.}
175.
176.//13.复制文件夹
177.//import java.io.*;
178.//import java.util.*;
179.LinkedList<String> folderList = new LinkedList<String>();
180.folderList.add(str1);
181.LinkedList<String> folderList2 = new LinkedList<String>();
182.folderList2.add(str2+ str1.substring(str1.lastIndexOf("\\")));
183.while (folderList.size() > 0) {
184. (new File(folderList2.peek())).mkdirs(); // 如果文件夹不存在 则建立新文件夹
185. File folders = new File(folderList.peek());
186. String[] file = folders.list();
187. File temp = null;
188. try {
189. for (int i = 0; i < file.length; i++) {
190. if (folderList.peek().endsWith(File.separator)) {
191. temp = new File(folderList.peek() + File.separator
192. + file[i]);
193. } else {
194. temp = new File(folderList.peek() + File.separator + file[i]);
195. }
196. if (temp.isFile()) {
197. FileInputStream input = new FileInputStream(temp);
198. FileOutputStream output = new FileOutputStream(
199. folderList2.peek() + File.separator + (temp.getName()).toString());
200. byte[] b = new byte[5120];
201. int len;
202. while ((len = input.read(b)) != -1) {
203. output.write(b, 0, len);
204. }
205. output.flush();
206. output.close();
207. input.close();
208. }
209. if (temp.isDirectory()) {// 如果是子文件夹
210. for (File f : temp.listFiles()) {
211. if (f.isDirectory()) {
212. folderList.add(f.getPath());
213. folderList2.add(folderList2.peek()
214. + File.separator + f.getName());
215. }
216. }
217. }
218. }
219. } catch (Exception e) {
220. //System.out.println("复制整个文件夹内容操作出错");
221. e.printStackTrace();
222. }
223. folderList.removeFirst();
224. folderList2.removeFirst();
225.}
226.
227.//14.复制一个文件夹下所有的文件夹到另一个文件夹下
228.//import java.io.*;
229.//import java.util.*;
230.File copyfolders=new File(str1);
231.File[] copyfoldersList=copyfolders.listFiles();
232.for(int k=0;k<copyfoldersList.length;k++){
233. if(copyfoldersList[k].isDirectory()){
234. ArrayList<String>folderList=new ArrayList<String>();
235. folderList.add(copyfoldersList[k].getPath());
236. ArrayList<String>folderList2=new ArrayList<String>();
237. folderList2.add(str2+"/"+copyfoldersList[k].getName());
238. for(int j=0;j<folderList.length;j++){
239. (new File(folderList2.get(j))).mkdirs(); //如果文件夹不存在 则建立新文件夹
240. File folders=new File(folderList.get(j));
241. String[] file=folders.list();
242. File temp=null;
243. try {
244. for (int i = 0; i < file.length; i++) {
245. if(folderList.get(j).endsWith(File.separator)){
246. temp=new File(folderList.get(j)+"/"+file[i]);
247. } else {
248. temp=new File(folderList.get(j)+"/"+File.separator+file[i]);
249. }
250. FileInputStream input = new FileInputStream(temp);
251. if(temp.isFile()){
252. FileInputStream input = new FileInputStream(temp);
253. FileOutputStream output = new FileOutputStream(folderList2.get(j) + "/" + (temp.getName()).toString());
254. byte[] b = new byte[5120];
255. int len;
256. while ( (len = input.read(b)) != -1) {
257. output.write(b, 0, len);
258. }
259. output.flush();
260. output.close();
261. input.close();
262. }
263. if(temp.isDirectory()){//如果是子文件夹
264. folderList.add(folderList.get(j)+"/"+file[i]);
265. folderList2.add(folderList2.get(j)+"/"+file[i]);
266. }
267. }
268. }
269. catch (Exception e) {
270. System.out.println("复制整个文件夹内容操作出错");
271. e.printStackTrace();
272. }
273. }
274. }
275.}
276.
277.//15.移动文件夹
278.//import java.io.*;
279.//import java.util.*;
280.LinkedList<String> folderList = new LinkedList<String>();
281.folderList.add(str1);
282.LinkedList<String> folderList2 = new LinkedList<String>();
283.folderList2.add(str2 + str1.substring(str1.lastIndexOf("\\")));
284.while (folderList.size() > 0) {
285. (new File(folderList2.peek())).mkdirs(); // 如果文件夹不存在 则建立新文件夹
286. File folders = new File(folderList.peek());
287. String[] file = folders.list();
288. File temp = null;
289. try {
290. for (int i = 0; i < file.length; i++) {
291. if (folderList.peek().endsWith(File.separator)) {
292. temp = new File(folderList.peek() + File.separator + file[i]);
293. } else {
294. temp = new File(folderList.peek() + File.separator + file[i]);
295. }
296. if (temp.isFile()) {
297. FileInputStream input = new FileInputStream(temp);
298. FileOutputStream output = new FileOutputStream(
299. folderList2.peek() + File.separator + (temp.getName()).toString());
300. byte[] b = new byte[5120];
301. int len;
302. while ((len = input.read(b)) != -1) {
303. output.write(b, 0, len);
304. }
305. output.flush();
306. output.close();
307. input.close();
308. if (!temp.delete())
309. System.out.println("删除单个文件操作出错!");
310. }
311. if (temp.isDirectory()) {// 如果是子文件夹
312. for (File f : temp.listFiles()) {
313. if (f.isDirectory()) {
314. folderList.add(f.getPath());
315. folderList2.add(folderList2.peek() + File.separator + f.getName());
316. }
317. }
318. }
319. }
320. } catch (Exception e) {
321. // System.out.println("复制整个文件夹内容操作出错");
322. e.printStackTrace();
323. }
324. folderList.removeFirst();
325. folderList2.removeFirst();
326.}
327.File f = new File(str1);
328.if (!f.delete()) {
329. for (File file : f.listFiles()) {
330. if (file.list().length == 0) {
331. System.out.println(file.getPath());
332. file.delete();
333. }
334. }
335.}
336.//16.移动一个文件夹下所有的文件夹到另一个目录下
337.//import java.io.*;
338.//import java.util.*;
339.File movefolders=new File(str1);
340.File[] movefoldersList=movefolders.listFiles();
341.for(int k=0;k<movefoldersList.length;k++){
342. if(movefoldersList[k].isDirectory()){
343. ArrayList<String>folderList=new ArrayList<String>();
344. folderList.add(movefoldersList[k].getPath());
345. ArrayList<String>folderList2=new ArrayList<String>();
346. folderList2.add(str2+"/"+movefoldersList[k].getName());
347. for(int j=0;j<folderList.length;j++){
348. (new File(folderList2.get(j))).mkdirs(); //如果文件夹不存在 则建立新文件夹
349. File folders=new File(folderList.get(j));
350. String[] file=folders.list();
351. File temp=null;
352. try {
353. for (int i = 0; i < file.length; i++) {
354. if(folderList.get(j).endsWith(File.separator)){
355. temp=new File(folderList.get(j)+"/"+file[i]);
356. }
357. else{
358. temp=new File(folderList.get(j)+"/"+File.separator+file[i]);
359. }
360. FileInputStream input = new FileInputStream(temp);
361. if(temp.isFile()){
362. FileInputStream input = new FileInputStream(temp);
363. FileOutputStream output = new FileOutputStream(folderList2.get(j) + "/" + (temp.getName()).toString());
364. byte[] b = new byte[5120];
365. int len;
366. while ( (len = input.read(b)) != -1) {
367. output.write(b, 0, len);
368. }
369. output.flush();
370. output.close();
371. input.close();
372. temp.delete();
373. }
374. if(temp.isDirectory()){//如果是子文件夹
375. folderList.add(folderList.get(j)+"/"+file[i]);
376. folderList2.add(folderList2.get(j)+"/"+file[i]);
377. }
378. }
379. }
380. catch (Exception e) {
381. System.out.println("复制整个文件夹内容操作出错");
382. e.printStackTrace();
383. }
384. }
385. movefoldersList[k].delete();
386. }
387.}
388.
389.//17.以一个文件夹的框架在另一个目录创建文件夹和空文件
390.//import java.io.*;
391.//import java.util.*;
392.boolean b=false;//不创建空文件
393.ArrayList<String>folderList=new ArrayList<String>();
394.folderList.add(str1);
395.ArrayList<String>folderList2=new ArrayList<String>();
396.folderList2.add(str2);
397.for(int j=0;j<folderList.length;j++){
398. (new File(folderList2.get(j))).mkdirs(); //如果文件夹不存在 则建立新文件夹
399. File folders=new File(folderList.get(j));
400. String[] file=folders.list();
401. File temp=null;
402. try {
403. for (int i = 0; i < file.length; i++) {
404. if(folderList.get(j).endsWith(File.separator)){
405. temp=new File(folderList.get(j)+"/"+file[i]);
406. }
407. else{
408. temp=new File(folderList.get(j)+"/"+File.separator+file[i]);
409. }
410. FileInputStream input = new FileInputStream(temp);
411. if(temp.isFile()){
412. if (b) temp.createNewFile();
413. }
414. if(temp.isDirectory()){//如果是子文件夹
415. folderList.add(folderList.get(j)+"/"+file[i]);
416. folderList2.add(folderList2.get(j)+"/"+file[i]);
417. }
418. }
419. }
420. catch (Exception e) {
421. System.out.println("复制整个文件夹内容操作出错");
422. e.printStackTrace();
423. }
424.}
425.
426.//18.复制文件
427.//import java.io.*;
428. int bytesum = 0;
429. int byteread = 0;
430. File oldfile = new File(str1);
431. try {
432. if (oldfile.exists()) { //文件存在时
433. FileInputStream inStream = new FileInputStream(oldfile); //读入原文件
434. FileOutputStream fs = new FileOutputStream(new File(str2,oldfile.getName()));
435. byte[] buffer = new byte[5120];
436. int length;
437. while ( (byteread = inStream.read(buffer)) != -1) {
438. bytesum += byteread; //字节数 文件大小
439. System.out.println(bytesum);
440. fs.write(buffer, 0, byteread);
441. }
442. inStream.close();
443. }
444. }
445. catch (Exception e) {
446. System.out.println("复制单个文件操作出错");
447. e.printStackTrace();
448. }
449.
450.//19.复制一个文件夹下所有的文件到另一个目录
451.//import java.io.*;
452.File copyfiles=new File(str1);
453.File[] files=copyfiles.listFiles();
454.for(int i=0;i<files.length;i++){
455. if(!files[i].isDirectory()){
456. int bytesum = 0;
457. int byteread = 0;
458. try {
459. InputStream inStream = new FileInputStream(files[i]); //读入原文件
460. FileOutputStream fs = new FileOutputStream(new File(str2,files[i].getName());
461. byte[] buffer = new byte[5120];
462. int length;
463. while ( (byteread = inStream.read(buffer)) != -1) {
464. bytesum += byteread; //字节数 文件大小
465. System.out.println(bytesum);
466. fs.write(buffer, 0, byteread);
467. }
468. inStream.close();
469. } catch (Exception e) {
470. System.out.println("复制单个文件操作出错");
471. e.printStackTrace();
472. }
473. }
474.}
475.
476.//20.提取扩展名
477.String str2=str1.substring(str1.lastIndexOf(".")+1);