提交 | 用户 | 时间
|
58d006
|
1 |
|
A |
2 |
package com.mandi.common.ParamFileter; |
|
3 |
|
|
4 |
|
|
5 |
import java.io.BufferedReader; |
|
6 |
import java.io.File; |
|
7 |
import java.io.FileInputStream; |
|
8 |
import java.io.FileOutputStream; |
|
9 |
import java.io.FileWriter; |
|
10 |
import java.io.IOException; |
|
11 |
import java.io.InputStream; |
|
12 |
import java.io.InputStreamReader; |
|
13 |
import java.io.PrintWriter; |
|
14 |
import java.io.RandomAccessFile; |
|
15 |
import java.text.SimpleDateFormat; |
|
16 |
import java.util.ArrayList; |
|
17 |
import java.util.Date; |
|
18 |
import java.util.List; |
|
19 |
|
|
20 |
|
|
21 |
/** |
|
22 |
* Function: 实现文件的简单处理,复制和移动文件、目录等 <br/> |
|
23 |
* Date: 2016年4月19日 上午10:54:24 <br/> |
|
24 |
* @author guolq |
|
25 |
*/ |
|
26 |
public class Fileoperation { |
|
27 |
public Fileoperation() { |
|
28 |
} |
|
29 |
|
|
30 |
/** |
|
31 |
* 新建目录 |
|
32 |
* @param folderPath String 如 c:/fqf |
|
33 |
* @return boolean |
|
34 |
*/ |
|
35 |
public void newFolder(String folderPath) { |
|
36 |
try { |
|
37 |
String filePath = folderPath; |
|
38 |
filePath = filePath.toString(); |
|
39 |
java.io.File myFilePath = new java.io.File(filePath); |
|
40 |
if (!myFilePath.exists()) { |
|
41 |
myFilePath.mkdir(); |
|
42 |
} |
|
43 |
} |
|
44 |
catch (Exception e) { |
|
45 |
System.out.println("新建目录操作出错"); |
|
46 |
e.printStackTrace(); |
|
47 |
} |
|
48 |
} |
|
49 |
|
|
50 |
/** |
|
51 |
* 新建文件 |
|
52 |
* @param filePathAndName String 文件路径及名称 如c:/fqf.txt |
|
53 |
* @param fileContent String 文件内容 |
|
54 |
* @return boolean |
|
55 |
*/ |
|
56 |
public void newFile(String filePathAndName, String fileContent) { |
|
57 |
|
|
58 |
try { |
|
59 |
String filePath = filePathAndName; |
|
60 |
filePath = filePath.toString(); //取的路径及文件名 |
|
61 |
File myFilePath = new File(filePath); |
|
62 |
/**如果文件不存在就建一个新文件*/ |
|
63 |
if (!myFilePath.exists()) { |
|
64 |
myFilePath.createNewFile(); |
|
65 |
} |
|
66 |
FileWriter resultFile = new FileWriter(myFilePath); //用来写入字符文件的便捷类, 在给出 File 对象的情况下构造一个 FileWriter 对象 |
|
67 |
PrintWriter myFile = new PrintWriter(resultFile); //向文本输出流打印对象的格式化表示形式,使用指定文件创建不具有自动行刷新的新 PrintWriter。 |
|
68 |
String strContent = fileContent; |
|
69 |
myFile.println(strContent); |
|
70 |
resultFile.close(); |
|
71 |
|
|
72 |
} |
|
73 |
catch (Exception e) { |
|
74 |
System.out.println("新建文件操作出错"); |
|
75 |
e.printStackTrace(); |
|
76 |
|
|
77 |
} |
|
78 |
|
|
79 |
} |
|
80 |
|
|
81 |
/** |
|
82 |
* 删除文件 |
|
83 |
* @param filePathAndName String 文件路径及名称 如c:/fqf.txt |
|
84 |
* @param fileContent String |
|
85 |
* @return boolean |
|
86 |
*/ |
|
87 |
public static void delFile(String filePathAndName) { |
|
88 |
try { |
|
89 |
String filePath = filePathAndName; |
|
90 |
filePath = filePath.toString(); |
|
91 |
java.io.File myDelFile = new java.io.File(filePath); |
|
92 |
myDelFile.delete(); |
|
93 |
|
|
94 |
} |
|
95 |
catch (Exception e) { |
|
96 |
System.out.println("删除文件操作出错"); |
|
97 |
e.printStackTrace(); |
|
98 |
|
|
99 |
} |
|
100 |
|
|
101 |
} |
|
102 |
|
|
103 |
/** |
|
104 |
* 删除文件夹 |
|
105 |
* @param filePathAndName String 文件夹路径及名称 如c:/fqf |
|
106 |
* @param fileContent String |
|
107 |
* @return boolean |
|
108 |
*/ |
|
109 |
public void delFolder(String folderPath) { |
|
110 |
try { |
|
111 |
delAllFile(folderPath); //删除完里面所有内容 |
|
112 |
String filePath = folderPath; |
|
113 |
filePath = filePath.toString(); |
|
114 |
java.io.File myFilePath = new java.io.File(filePath); |
|
115 |
myFilePath.delete(); //删除空文件夹 |
|
116 |
|
|
117 |
} |
|
118 |
catch (Exception e) { |
|
119 |
System.out.println("删除文件夹操作出错"); |
|
120 |
e.printStackTrace(); |
|
121 |
|
|
122 |
} |
|
123 |
|
|
124 |
} |
|
125 |
|
|
126 |
/** |
|
127 |
* 删除文件夹里面的所有文件 |
|
128 |
* @param path String 文件夹路径 如 c:/fqf |
|
129 |
*/ |
|
130 |
public void delAllFile(String path) { |
|
131 |
File file = new File(path); |
|
132 |
if (!file.exists()) { |
|
133 |
return; |
|
134 |
} |
|
135 |
if (!file.isDirectory()) { |
|
136 |
return; |
|
137 |
} |
|
138 |
String[] tempList = file.list(); |
|
139 |
File temp = null; |
|
140 |
for (int i = 0; i < tempList.length; i++) { |
|
141 |
if (path.endsWith(File.separator)) { |
|
142 |
temp = new File(path + tempList[i]); |
|
143 |
} |
|
144 |
else { |
|
145 |
temp = new File(path + File.separator + tempList[i]); |
|
146 |
} |
|
147 |
if (temp.isFile()) { |
|
148 |
temp.delete(); |
|
149 |
} |
|
150 |
if (temp.isDirectory()) { |
|
151 |
delAllFile(path+"/"+ tempList[i]);//先删除文件夹里面的文件 |
|
152 |
delFolder(path+"/"+ tempList[i]);//再删除空文件夹 |
|
153 |
} |
|
154 |
} |
|
155 |
} |
|
156 |
|
|
157 |
/** |
|
158 |
* 复制单个文件 |
|
159 |
* @param oldPath String 原文件路径 如:c:/fqf.txt |
|
160 |
* @param newPath String 复制后路径 如:f:/fqf.txt |
|
161 |
* @return boolean |
|
162 |
*/ |
|
163 |
public static void copyFile(String oldPath, String newPath) { |
|
164 |
try { |
|
165 |
int byteread = 0; |
|
166 |
File oldfile = new File(oldPath); |
|
167 |
if (oldfile.exists()) { //文件存在时 |
|
168 |
InputStream inStream = new FileInputStream(oldPath); //读入原文件 |
|
169 |
FileOutputStream fs = new FileOutputStream(newPath); |
|
170 |
byte[] buffer = new byte[1444]; |
|
171 |
while ( (byteread = inStream.read(buffer)) != -1) { |
|
172 |
fs.write(buffer, 0, byteread); |
|
173 |
} |
|
174 |
inStream.close(); |
|
175 |
fs.close(); |
|
176 |
} |
|
177 |
} |
|
178 |
catch (Exception e) { |
|
179 |
System.out.println("复制单个文件操作出错"); |
|
180 |
e.printStackTrace(); |
|
181 |
} |
|
182 |
|
|
183 |
} |
|
184 |
|
|
185 |
/** |
|
186 |
* 复制整个文件夹内容 |
|
187 |
* @param oldPath String 原文件路径 如:c:/fqf |
|
188 |
* @param newPath String 复制后路径 如:f:/fqf/ff |
|
189 |
* @return boolean |
|
190 |
*/ |
|
191 |
public void copyFolder(String oldPath, String newPath) { |
|
192 |
|
|
193 |
try { |
|
194 |
(new File(newPath)).mkdirs(); //如果文件夹不存在 则建立新文件夹 |
|
195 |
File a=new File(oldPath); |
|
196 |
String[] file=a.list(); |
|
197 |
File temp=null; |
|
198 |
for (int i = 0; i < file.length; i++) { |
|
199 |
if(oldPath.endsWith(File.separator)){ |
|
200 |
temp=new File(oldPath+file[i]); |
|
201 |
} |
|
202 |
else{ |
|
203 |
temp=new File(oldPath+File.separator+file[i]); |
|
204 |
} |
|
205 |
|
|
206 |
if(temp.isFile()){ |
|
207 |
FileInputStream input = new FileInputStream(temp); |
|
208 |
FileOutputStream output = new FileOutputStream(newPath + "/" + |
|
209 |
(temp.getName()).toString()); |
|
210 |
byte[] b = new byte[1024 * 5]; |
|
211 |
int len; |
|
212 |
while ( (len = input.read(b)) != -1) { |
|
213 |
output.write(b, 0, len); |
|
214 |
} |
|
215 |
output.flush(); |
|
216 |
output.close(); |
|
217 |
input.close(); |
|
218 |
} |
|
219 |
if(temp.isDirectory()){//如果是子文件夹 |
|
220 |
copyFolder(oldPath+"/"+file[i],newPath+"/"+file[i]); |
|
221 |
} |
|
222 |
} |
|
223 |
} |
|
224 |
catch (Exception e) { |
|
225 |
System.out.println("复制整个文件夹内容操作出错"); |
|
226 |
e.printStackTrace(); |
|
227 |
|
|
228 |
} |
|
229 |
|
|
230 |
} |
|
231 |
|
|
232 |
/** |
|
233 |
* 移动文件到指定目录 |
|
234 |
* @param oldPath String 如:c:/fqf.txt |
|
235 |
* @param newPath String 如:d:/fqf.txt |
|
236 |
*/ |
|
237 |
public static void moveFile(String oldPath, String newPath) { |
|
238 |
copyFile(oldPath, newPath); |
|
239 |
delFile(oldPath); |
|
240 |
|
|
241 |
} |
|
242 |
|
|
243 |
/** |
|
244 |
* 移动文件到指定目录 |
|
245 |
* @param oldPath String 如:c:/fqf.txt |
|
246 |
* @param newPath String 如:d:/fqf.txt |
|
247 |
*/ |
|
248 |
public void moveFolder(String oldPath, String newPath) { |
|
249 |
copyFolder(oldPath, newPath); |
|
250 |
delFolder(oldPath); |
|
251 |
|
|
252 |
} |
|
253 |
|
|
254 |
|
|
255 |
public static List<String> txt2list(File f){ |
|
256 |
List<String> list=new ArrayList<String>(); |
|
257 |
try |
|
258 |
{ |
|
259 |
if(f.isFile()&&f.exists()) |
|
260 |
{ |
|
261 |
InputStreamReader read = new InputStreamReader(new FileInputStream(f),"gbk"); |
|
262 |
BufferedReader reader=new BufferedReader(read); |
|
263 |
String line; |
|
264 |
while ((line = reader.readLine()) != null) |
|
265 |
{ |
|
266 |
list.add(line); |
|
267 |
} |
|
268 |
read.close(); |
|
269 |
} |
|
270 |
} catch (Exception e) |
|
271 |
{ |
|
272 |
e.printStackTrace(); |
|
273 |
} |
|
274 |
return list; |
|
275 |
} |
|
276 |
|
|
277 |
|
|
278 |
|
|
279 |
/** |
|
280 |
* 文件末尾追加内容 |
|
281 |
* @param fileName |
|
282 |
* @param content |
|
283 |
*/ |
|
284 |
public static void AppendContentToFile(String fileName, String content) { |
|
285 |
try { |
|
286 |
// 打开一个随机访问文件流,按读写方式 |
|
287 |
RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw"); |
|
288 |
// 文件长度,字节数 |
|
289 |
long fileLength = randomFile.length(); |
|
290 |
// 将写文件指针移到文件尾。 |
|
291 |
randomFile.seek(fileLength); |
|
292 |
SimpleDateFormat sdf=new SimpleDateFormat("HH:mm:ss"); |
|
293 |
String nowstr=sdf.format(new Date())+" "; |
|
294 |
randomFile.write((nowstr+content + "\r\n").getBytes()); |
|
295 |
randomFile.close(); |
|
296 |
} catch (IOException e) { |
|
297 |
// e.printStackTrace(); |
|
298 |
} |
|
299 |
} |
|
300 |
|
|
301 |
/** |
|
302 |
* 获得文件名字 |
|
303 |
* @return |
|
304 |
*/ |
|
305 |
public static String getFileName(){ |
|
306 |
String ccr="D:\\YKTlog\\AppendLog"; |
|
307 |
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); |
|
308 |
Date now=new Date(); |
|
309 |
String nowstr=sdf.format(now); |
|
310 |
return ccr+"\\sniper_"+nowstr+".txt"; |
|
311 |
} |
|
312 |
|
|
313 |
public static String gettestFileName(){ |
|
314 |
String ccr="D:\\YKTlog\\AppendLog"; |
|
315 |
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); |
|
316 |
Date now=new Date(); |
|
317 |
String nowstr=sdf.format(now); |
|
318 |
return ccr+"\\sniper_test_"+nowstr+".txt"; |
|
319 |
} |
|
320 |
} |