ec1c044daee2691f787d045d1400231b45de7df7.svn-base
3.3 KB
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
package com.espeed.tool;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
/**
* 用于创建文件夹的方法
* @param mkdirName
*/
public class createAndDeleteFile {
/**创建文件夹*/
public static void mkdir(String mkdirName)throws Exception{
File dirFile = new File(mkdirName);
boolean bFile = dirFile.exists();
if( bFile == true )
{
System.out.println("The folder exists.");
} else {
System.out.println("The folder do not exist,now trying to create a one...");
bFile = dirFile.mkdirs();
if( bFile == true )
{
System.out.println("Create successfully!");
System.out.println("创建文件夹");
}
else{
System.out.println("Disable to make the folder,please check the disk is full or not.");
System.out.println(" 文件夹创建失败,清确认磁盘没有写保护并且空件足够");
}
}
}
/**删除文件夹*/
public static void deleteFile(File file){
try {
if(file.exists()){
if(file.isFile()){
file.delete();
}else if(file.isDirectory()){
File files[] = file.listFiles();
for(int i=0;i<files.length;i++){
deleteFile(files[i]);
}
}
file.delete();
}else{
System.out.println("所删除的文件不存在!"+'\n');
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**用于创建文件的方法 */
public static void createFile(String mkdirName) throws Exception{
File dirFile = new File(mkdirName);
boolean bFile = dirFile.exists();
if( bFile == true ){
System.out.println("The folder exists.");
}else{
System.out.println("The folder do not exist,now trying to create a one...");
bFile = dirFile.createNewFile();
if( bFile == true ){
System.out.println("Create successfully!");
System.out.println("创建文件夹");
}else{
System.out.println("Disable to make the folder,please check the disk is full or not.");
System.out.println(" 文件夹创建失败,清确认磁盘没有写保护并且空件足够");
}
}
}
/**文件复制*/
public static void filecopy(File s, File t){
FileInputStream fi = null;
FileOutputStream fo = null;
FileChannel in = null;
FileChannel out = null;
try {
fi = new FileInputStream(s);
fo = new FileOutputStream(t);
in = fi.getChannel();//得到对应的文件通道
out = fo.getChannel();//得到对应的文件通道
in.transferTo(0, in.size(), out);//连接两个通道,并且从in通道读取,然后写入out通道
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fi.close();
in.close();
fo.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] agrs){
createAndDeleteFile caf = new createAndDeleteFile();
File file = new File("J:mail\\xieyong435298846@qq.comdraft7320131122195807.eml");
caf.deleteFile(file);
//mkdir("c:/aa/aa/aa");
}
}