01.package otheri.common;
02.
03.import java.io.File;
04.import java.io.FileInputStream;
05.import java.io.FileOutputStream;
06.import java.io.IOException;
07.import java.io.InputStream;
08.import java.io.OutputStream;
09.
10.import otheri.io.Input;
11.import otheri.io.Output;
12.import android.content.Context;
13.import android.os.Environment;
14.
15.public class FileHelper {
16. private Context context;
17. private String SDPATH;
18. private String FILESPATH;
19.
20. public FileHelper(Context context) {
21. this.context = context;
22. SDPATH = Environment.getExternalStorageDirectory().getPath() + "\\";
23. FILESPATH = this.context.getFilesDir().getPath() + "\\";
24. }
25.
26. /**
27. * 在SD卡上创建文件
28. *
29. * @throws IOException
30. */
31. public File creatSDFile(String fileName) throws IOException {
32. File file = new File(SDPATH + fileName);
33. file.createNewFile();
34. return file;
35. }
36.
37. /**
38. * 删除SD卡上的文件
39. *
40. * @param fileName
41. */
42. public boolean delSDFile(String fileName) {
43. File file = new File(SDPATH + fileName);
44. if (file == null || !file.exists() || file.isDirectory())
45. return false;
46. file.delete();
47. return true;
48. }
49.
50. /**
51. * 在SD卡上创建目录
52. *
53. * @param dirName
54. */
55. public File creatSDDir(String dirName) {
56. File dir = new File(SDPATH + dirName);
57. dir.mkdir();
58. return dir;
59. }
60.
61. /**
62. * 删除SD卡上的目录
63. *
64. * @param dirName
65. */
66. public boolean delSDDir(String dirName) {
67. File dir = new File(SDPATH + dirName);
68. return delDir(dir);
69. }
70.
71. /**
72. * 修改SD卡上的文件或目录名
73. *
74. * @param fileName
75. */
76. public boolean renameSDFile(String oldfileName, String newFileName) {
77. File oleFile = new File(SDPATH + oldfileName);
78. File newFile = new File(SDPATH + newFileName);
79. return oleFile.renameTo(newFile);
80. }
81.
82. /**
83. * 拷贝SD卡上的单个文件
84. *
85. * @param path
86. * @throws IOException
87. */
88. public boolean copySDFileTo(String srcFileName, String destFileName)
89. throws IOException {
90. File srcFile = new File(SDPATH + srcFileName);
91. File destFile = new File(SDPATH + destFileName);
92. return copyFileTo(srcFile, destFile);
93. }
94.
95. /**
96. * 拷贝SD卡上指定目录的所有文件
97. *
98. * @param srcDirName
99. * @param destDirName
100. * @return
101. * @throws IOException
102. */
103. public boolean copySDFilesTo(String srcDirName, String destDirName)
104. throws IOException {
105. File srcDir = new File(SDPATH + srcDirName);
106. File destDir = new File(SDPATH + destDirName);
107. return copyFilesTo(srcDir, destDir);
108. }
109.
110. /**
111. * 移动SD卡上的单个文件
112. *
113. * @param srcFileName
114. * @param destFileName
115. * @return
116. * @throws IOException
117. */
118. public boolean moveSDFileTo(String srcFileName, String destFileName)
119. throws IOException {
120. File srcFile = new File(SDPATH + srcFileName);
121. File destFile = new File(SDPATH + destFileName);
122. return moveFileTo(srcFile, destFile);
123. }
124.
125. /**
126. * 移动SD卡上的指定目录的所有文件
127. *
128. * @param srcDirName
129. * @param destDirName
130. * @return
131. * @throws IOException
132. */
133. public boolean moveSDFilesTo(String srcDirName, String destDirName)
134. throws IOException {
135. File srcDir = new File(SDPATH + srcDirName);
136. File destDir = new File(SDPATH + destDirName);
137. return moveFilesTo(srcDir, destDir);
138. }
139.
140.
141. /*
142. * 将文件写入sd卡。如:writeSDFile("test.txt");
143. */
144. public Output writeSDFile(String fileName) throws IOException {
145. File file = new File(SDPATH + fileName);
146. FileOutputStream fos = new FileOutputStream(file);
147. return new Output(fos);
148. }
149.
150. /*
151. * 在原有文件上继续写文件。如:appendSDFile("test.txt");
152. */
153. public Output appendSDFile(String fileName) throws IOException {
154. File file = new File(SDPATH + fileName);
155. FileOutputStream fos = new FileOutputStream(file, true);
156. return new Output(fos);
157. }
158.
159. /*
160. * 从SD卡读取文件。如:readSDFile("test.txt");
161. */
162. public Input readSDFile(String fileName) throws IOException {
163. File file = new File(SDPATH + fileName);
164. FileInputStream fis = new FileInputStream(file);
165. return new Input(fis);
166. }
167.
168.
169. /**
170. * 建立私有文件
171. *
172. * @param fileName
173. * @return
174. * @throws IOException
175. */
176. public File creatDataFile(String fileName) throws IOException {
177. File file = new File(FILESPATH + fileName);
178. file.createNewFile();
179. return file;
180. }
181.
182. /**
183. * 建立私有目录
184. *
185. * @param dirName
186. * @return
187. */
188. public File creatDataDir(String dirName) {
189. File dir = new File(FILESPATH + dirName);
190. dir.mkdir();
191. return dir;
192. }
193.
194. /**
195. * 删除私有文件
196. *
197. * @param fileName
198. * @return
199. */
200. public boolean delDataFile(String fileName) {
201. File file = new File(FILESPATH + fileName);
202. return delFile(file);
203. }
204.
205. /**
206. * 删除私有目录
207. *
208. * @param dirName
209. * @return
210. */
211. public boolean delDataDir(String dirName) {
212. File file = new File(FILESPATH + dirName);
213. return delDir(file);
214. }
215.
216. /**
217. * 更改私有文件名
218. *
219. * @param oldName
220. * @param newName
221. * @return
222. */
223. public boolean renameDataFile(String oldName, String newName) {
224. File oldFile = new File(FILESPATH + oldName);
225. File newFile = new File(FILESPATH + newName);
226. return oldFile.renameTo(newFile);
227. }
228.
229. /**
230. * 在私有目录下进行文件复制
231. *
232. * @param srcFileName
233. * : 包含路径及文件名
234. * @param destFileName
235. * @return
236. * @throws IOException
237. */
238. public boolean copyDataFileTo(String srcFileName, String destFileName)
239. throws IOException {
240. File srcFile = new File(FILESPATH + srcFileName);
241. File destFile = new File(FILESPATH + destFileName);
242. return copyFileTo(srcFile, destFile);
243. }
244.
245. /**
246. * 复制私有目录里指定目录的所有文件
247. *
248. * @param srcDirName
249. * @param destDirName
250. * @return
251. * @throws IOException
252. */
253. public boolean copyDataFilesTo(String srcDirName, String destDirName)
254. throws IOException {
255. File srcDir = new File(FILESPATH + srcDirName);
256. File destDir = new File(FILESPATH + destDirName);
257. return copyFilesTo(srcDir, destDir);
258. }
259.
260. /**
261. * 移动私有目录下的单个文件
262. *
263. * @param srcFileName
264. * @param destFileName
265. * @return
266. * @throws IOException
267. */
268. public boolean moveDataFileTo(String srcFileName, String destFileName)
269. throws IOException {
270. File srcFile = new File(FILESPATH + srcFileName);
271. File destFile = new File(FILESPATH + destFileName);
272. return moveFileTo(srcFile, destFile);
273. }
274.
275. /**
276. * 移动私有目录下的指定目录下的所有文件
277. *
278. * @param srcDirName
279. * @param destDirName
280. * @return
281. * @throws IOException
282. */
283. public boolean moveDataFilesTo(String srcDirName, String destDirName)
284. throws IOException {
285. File srcDir = new File(FILESPATH + srcDirName);
286. File destDir = new File(FILESPATH + destDirName);
287. return moveFilesTo(srcDir, destDir);
288. }
289.
290. /*
291. * 将文件写入应用私有的files目录。如:writeFile("test.txt");
292. */
293. public Output wirteFile(String fileName) throws IOException {
294. OutputStream os = context.openFileOutput(fileName,
295. Context.MODE_WORLD_WRITEABLE);
296. return new Output(os);
297. }
298.
299. /*
300. * 在原有文件上继续写文件。如:appendFile("test.txt");
301. */
302. public Output appendFile(String fileName) throws IOException {
303. OutputStream os = context.openFileOutput(fileName, Context.MODE_APPEND);
304. return new Output(os);
305. }
306.
307. /*
308. * 从应用的私有目录files读取文件。如:readFile("test.txt");
309. */
310. public Input readFile(String fileName) throws IOException {
311. InputStream is = context.openFileInput(fileName);
312. return new Input(is);
313. }
314.
315.
316.
317. /**********************************************************************************************************/
318. /*********************************************************************************************************/
319. */
320. /**
321. * 删除一个文件
322. *
323. * @param file
324. * @return
325. */
326. public boolean delFile(File file) {
327. if (file.isDirectory())
328. return false;
329. return file.delete();
330. }
331.
332. /**
333. * 删除一个目录(可以是非空目录)
334. *
335. * @param dir
336. */
337. public boolean delDir(File dir) {
338. if (dir == null || !dir.exists() || dir.isFile()) {
339. return false;
340. }
341. for (File file : dir.listFiles()) {
342. if (file.isFile()) {
343. file.delete();
344. } else if (file.isDirectory()) {
345. delDir(file);// 递归
346. }
347. }
348. dir.delete();
349. return true;
350. }
351.
352. /**
353. * 拷贝一个文件,srcFile源文件,destFile目标文件
354. *
355. * @param path
356. * @throws IOException
357. */
358. public boolean copyFileTo(File srcFile, File destFile) throws IOException {
359. if (srcFile.isDirectory() || destFile.isDirectory())
360. return false;// 判断是否是文件
361. FileInputStream fis = new FileInputStream(srcFile);
362. FileOutputStream fos = new FileOutputStream(destFile);
363. int readLen = 0;
364. byte[] buf = new byte[1024];
365. while ((readLen = fis.read(buf)) != -1) {
366. fos.write(buf, 0, readLen);
367. }
368. fos.flush();
369. fos.close();
370. fis.close();
371. return true;
372. }
373.
374. /**
375. * 拷贝目录下的所有文件到指定目录
376. *
377. * @param srcDir
378. * @param destDir
379. * @return
380. * @throws IOException
381. */
382. public boolean copyFilesTo(File srcDir, File destDir) throws IOException {
383. if (!srcDir.isDirectory() || !destDir.isDirectory())
384. return false;// 判断是否是目录
385. if (!destDir.exists())
386. return false;// 判断目标目录是否存在
387. File[] srcFiles = srcDir.listFiles();
388. for (int i = 0; i < srcFiles.length; i++) {
389. if (srcFiles[i].isFile()) {
390. // 获得目标文件
391. File destFile = new File(destDir.getPath() + "\\"
392. + srcFiles[i].getName());
393. copyFileTo(srcFiles[i], destFile);
394. } else if (srcFiles[i].isDirectory()) {
395. File theDestDir = new File(destDir.getPath() + "\\"
396. + srcFiles[i].getName());
397. copyFilesTo(srcFiles[i], theDestDir);
398. }
399. }
400. return true;
401. }
402.
403. /**
404. * 移动一个文件
405. *
406. * @param srcFile
407. * @param destFile
408. * @return
409. * @throws IOException
410. */
411. public boolean moveFileTo(File srcFile, File destFile) throws IOException {
412. boolean iscopy = copyFileTo(srcFile, destFile);
413. if (!iscopy)
414. return false;
415. delFile(srcFile);
416. return true;
417. }
418.
419. /**
420. * 移动目录下的所有文件到指定目录
421. *
422. * @param srcDir
423. * @param destDir
424. * @return
425. * @throws IOException
426. */
427. public boolean moveFilesTo(File srcDir, File destDir) throws IOException {
428. if (!srcDir.isDirectory() || !destDir.isDirectory()) {
429. return false;
430. }
431. File[] srcDirFiles = srcDir.listFiles();
432. for (int i = 0; i < srcDirFiles.length; i++) {
433. if (srcDirFiles[i].isFile()) {
434. File oneDestFile = new File(destDir.getPath() + "\\"
435. + srcDirFiles[i].getName());
436. moveFileTo(srcDirFiles[i], oneDestFile);
437. delFile(srcDirFiles[i]);
438. } else if (srcDirFiles[i].isDirectory()) {
439. File oneDestFile = new File(destDir.getPath() + "\\"
440. + srcDirFiles[i].getName());
441. moveFilesTo(srcDirFiles[i], oneDestFile);
442. delDir(srcDirFiles[i]);
443. }
444.
445. }
446. return true;
447. }
448.}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/fenghome/archive/2010/06/13/5668598.aspx