file工具类

实现了一些File有关的操作,封装到一个FileUtil工具类中.

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
import java.io.*;
* @author yh
* 文件工具类
*/
public class FileUtil {
* @param fromFileName 原文件
* @param toFileName 目标文件
* @return 是否删除成功
* 实现文件的拷贝
*/
@SuppressWarnings("unused")
public static boolean copyFile(String fromFileName, String toFileName) throws Exception {
File fromFile = new File(fromFileName);
File toFile = new File(toFileName);
fromFile.exists();
//如果原文件不存在返回false
if(!fromFile.isFile()) {
return false;
}
//如果目标文件不存在,则创建
if(!toFile.isFile()) {
toFile.createNewFile();
}
InputStream in = new BufferedInputStream(new FileInputStream(fromFile));
OutputStream out = new BufferedOutputStream(new FileOutputStream(toFile));
byte[] buf = new byte[64];
int len = 0;
while( (len = in.read(buf) ) != -1 ) {
out.write(buf, 0, len);
out.flush();
}
in.close();
out.close();
return true;
}
* @param name 待删除文件
* @return 是否删除成功
* 伪实现删除文件功能
*/
public static boolean deleteFile(String name) throws Exception {
File file = new File(name);
if(!file.exists()) {
return false;
}
file.delete();
return true;
}
}