Chinaunix首页 | 论坛 | 博客
  • 博客访问: 87166
  • 博文数量: 29
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 281
  • 用 户 组: 普通用户
  • 注册时间: 2014-01-02 18:00
文章分类
文章存档

2014年(29)

我的朋友

分类: Java

2014-01-06 11:41:49

整理的一些用到过的工具类,本文为文件操作相关。不是很全......

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
    
publicclassFileUtils {
/**
* 删除指定的目录或文件
*
* @param path 要删除的目录或文件
* @return 删除成功返回 true,否则返回 false。
*/
publicstaticbooleandeleteFolder(String path) {
booleanflag = false;
File file = newFile(path);
// 判断目录或文件是否存在
if(file.exists()) {
// 判断是否为文件
if(file.isFile()) { // 为文件时调用删除文件方法
returndeleteFile(path);
} else{ // 为目录时调用删除目录方法
returndeleteDirectory(path);
}
} else{
returnflag;
}
}
/**
* 删除单个文件
*
* @param path 要删除文件的文件名
* @return 单个文件删除成功返回true,否则返回false
*/
publicstaticbooleandeleteFile(String path) {
booleanflag = false;
File file = newFile(path);
// 路径为文件且不为空则进行删除
if(file.isFile() && file.exists()) {
file.delete();
flag = true;
}
returnflag;
}
/**
* 删除目录以及目录下的文件
*
* @param path 要删除目录的文件路径
*
* @return 目录删除成功返回true,否则返回false
*/
publicstaticbooleandeleteDirectory(String path) {
// 如果path不以文件分隔符结尾,自动添加文件分隔符
if(!path.endsWith(File.separator)) {
path = path + File.separator;
}
File dirFile = newFile(path);
// 如果dir对应的文件不存在,或者不是一个目录,则退出
if(!dirFile.exists() || !dirFile.isDirectory()) {
returnfalse;
}
booleanflag = true;
File[] files = dirFile.listFiles();
// 删除文件夹下的所有文件(包括子目录)
for(inti = 0; i < files.length; i++) {
// 删除子文件
if(files[i].isFile()) {
flag = deleteFile(files[i].getAbsolutePath());
if(!flag)
break;
} else{
// 删除子目录的递归调用,循环删除
flag = deleteDirectory(files[i].getAbsolutePath());
if(!flag)
break;
}
}
if(!flag)
returnfalse;
// 删除当前目录
if(dirFile.delete()) {
returntrue;
} else{
returnfalse;
}
}
/**
* 复制目录
*
* @param oldPath
*            需要复制的目录
* @param newPath
*            目标目录
*/
publicstaticbooleancopyDirectory(String oldPath, String newPath) {
BufferedReader br = null;
BufferedWriter bw = null;
try{
(newFile(newPath)).mkdirs(); // 如果目录不存在 则建立新目录
File a = newFile(oldPath);
String[] file = a.list();
File temp = null;
for(inti = 0; i < file.length; i++) {
if(oldPath.endsWith(File.separator)) {
temp = newFile(oldPath + file[i]);
} else{
temp = newFile(oldPath + File.separator + file[i]);
}
if(temp.isFile()) {
br = newBufferedReader(newInputStreamReader(
newFileInputStream(temp)));
bw = newBufferedWriter(newOutputStreamWriter(
newFileOutputStream(newPath + File.separator
+ (temp.getName()).toString())));
String data;
while((data = br.readLine()) != null) {
bw.write(data, 0, data.length());
}
bw.flush();
}
if(temp.isDirectory()) {// 如果是子文件夹
copyDirectory(oldPath + File.separator + file[i], newPath
+ File.separator + file[i]);
}
}
returntrue;
} catch(Exception e1) {
System.out.println("复制整个文件夹内容操作出错");
e1.printStackTrace();
returnfalse;
} finally{
try{
if(br != null) {
br.close();
}
if(bw != null) {
bw.close();
}
} catch(Exception e2) {
}
}
}
/**
*
* 测试方法
*
*/
publicstaticvoidmain(String[] args) {
FileUtils.deleteFolder("E:/testFolder");
}
}
阅读(662) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:Intent的一些简单用法

给主人留下些什么吧!~~