Android基础之文件操作
2012-10-18 15:36
197 查看
android的文件操作要有权限:
[xhtml] view plaincopyprint?
1. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
SD卡下的文件操作:
1、判断SD卡是否插入
[java] view plaincopyprint?
1. Environment.getExternalStorageState().equals(
2. android.os.Environment.MEDIA_MOUNTED);
Environment.getExternalStorageState().equals( android.os.Environment.MEDIA_MOUNTED);
2、获得sd卡根目录:
[java] view plaincopyprint?
1. File skRoot = Environment.getExternalStorageDirectory();
File skRoot = Environment.getExternalStorageDirectory();
私有目录下的文件操作:
1、获得私有根目录:
[java] view plaincopyprint?
1. File fileRoot = Context.getFilesDir()+"//";
File fileRoot = Context.getFilesDir()+"//";
还未整理
文件夹或文件夹操作:
1、确定或获得文件夹和文件路径
a、获得文件或文件夹的绝对路径和相对路径。区别
[java] view plaincopyprint?
1. String path = File.getPath();//相对
2. String path = File.getAbsoultePath();//绝对
String path = File.getPath();//相对 String path = File.getAbsoultePath();//绝对
b 、获得文件或文件夹的父目录
[java] view plaincopyprint?
1. String parentPath = File.getParent();
String parentPath = File.getParent();
c、获得文件或文件夹的名称:
[java] view plaincopyprint?
1. String Name = File.getName();
String Name = File.getName();
2、建立文件或文件夹
[java] view plaincopyprint?
1. File.mkDir(); //建立文件夹
2. File.createNewFile();//建立文件
File.mkDir(); //建立文件夹 File.createNewFile();//建立文件
3、判断是文件或文件夹
[java] view plaincopyprint?
1. File.isDirectory()
File.isDirectory()
4、列出文件夹下的所有文件和文件夹名
[java] view plaincopyprint?
1. File[] files = File.listFiles();
File[] files = File.listFiles();
5、修改文件夹和文件名
[java] view plaincopyprint?
1. File.renameTo(dest);
File.renameTo(dest);
6、删除文件夹或文件
[java] view plaincopyprint?
1. File.delete();
File.delete();
[java] view plaincopyprint?
1. package otheri.common;
2.
3. import java.io.File;
4. import java.io.FileInputStream;
5. import java.io.FileOutputStream;
6. import java.io.IOException;
7. import java.io.InputStream;
8. import java.io.OutputStream;
9.
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. }
package otheri.common; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import otheri.io.Input; import otheri.io.Output; import android.content.Context;
import android.os.Environment; public class FileHelper { private Context context; private String SDPATH; private String FILESPATH; public FileHelper(Context context) { this.context = context; SDPATH = Environment.getExternalStorageDirectory().getPath() + "//";
FILESPATH = this.context.getFilesDir().getPath() + "//"; } /** * 在SD卡上创建文件 * * @throws IOException */ public File creatSDFile(String fileName) throws IOException { File file = new File(SDPATH + fileName); file.createNewFile(); return file; } /** * 删除SD卡上的文件
* * @param fileName */ public boolean delSDFile(String fileName) { File file = new File(SDPATH + fileName); if (file == null || !file.exists() || file.isDirectory()) return false; file.delete(); return true; } /** * 在SD卡上创建目录 * * @param dirName */ public File
creatSDDir(String dirName) { File dir = new File(SDPATH + dirName); dir.mkdir(); return dir; } /** * 删除SD卡上的目录 * * @param dirName */ public boolean delSDDir(String dirName) { File dir = new File(SDPATH + dirName); return delDir(dir); } /** * 修改SD卡上的文件或目录名
* * @param fileName */ public boolean renameSDFile(String oldfileName, String newFileName) { File oleFile = new File(SDPATH + oldfileName); File newFile = new File(SDPATH + newFileName); return oleFile.renameTo(newFile); } /** * 拷贝SD卡上的单个文件 * * @param path
* @throws IOException */ public boolean copySDFileTo(String srcFileName, String destFileName) throws IOException { File srcFile = new File(SDPATH + srcFileName); File destFile = new File(SDPATH + destFileName); return copyFileTo(srcFile, destFile); } /** *
拷贝SD卡上指定目录的所有文件 * * @param srcDirName * @param destDirName * @return * @throws IOException */ public boolean copySDFilesTo(String srcDirName, String destDirName) throws IOException { File srcDir = new File(SDPATH + srcDirName); File destDir = new File(SDPATH
+ destDirName); return copyFilesTo(srcDir, destDir); } /** * 移动SD卡上的单个文件 * * @param srcFileName * @param destFileName * @return * @throws IOException */ public boolean moveSDFileTo(String srcFileName, String destFileName) throws IOException { File srcFile
= new File(SDPATH + srcFileName); File destFile = new File(SDPATH + destFileName); return moveFileTo(srcFile, destFile); } /** * 移动SD卡上的指定目录的所有文件 * * @param srcDirName * @param destDirName * @return * @throws IOException */ public boolean moveSDFilesTo(String
srcDirName, String destDirName) throws IOException { File srcDir = new File(SDPATH + srcDirName); File destDir = new File(SDPATH + destDirName); return moveFilesTo(srcDir, destDir); } /* * 将文件写入sd卡。如:writeSDFile("test.txt"); */ public Output writeSDFile(String
fileName) throws IOException { File file = new File(SDPATH + fileName); FileOutputStream fos = new FileOutputStream(file); return new Output(fos); } /* * 在原有文件上继续写文件。如:appendSDFile("test.txt"); */ public Output appendSDFile(String fileName) throws IOException
{ File file = new File(SDPATH + fileName); FileOutputStream fos = new FileOutputStream(file, true); return new Output(fos); } /* * 从SD卡读取文件。如:readSDFile("test.txt"); */ public Input readSDFile(String fileName) throws IOException { File file = new File(SDPATH
+ fileName); FileInputStream fis = new FileInputStream(file); return new Input(fis); } /** * 建立私有文件 * * @param fileName * @return * @throws IOException */ public File creatDataFile(String fileName) throws IOException { File file = new File(FILESPATH + fileName);
file.createNewFile(); return file; } /** * 建立私有目录 * * @param dirName * @return */ public File creatDataDir(String dirName) { File dir = new File(FILESPATH + dirName); dir.mkdir(); return dir; } /** * 删除私有文件 * * @param fileName * @return */ public boolean delDataFile(String
fileName) { File file = new File(FILESPATH + fileName); return delFile(file); } /** * 删除私有目录 * * @param dirName * @return */ public boolean delDataDir(String dirName) { File file = new File(FILESPATH + dirName); return delDir(file); } /** * 更改私有文件名 * * @param
oldName * @param newName * @return */ public boolean renameDataFile(String oldName, String newName) { File oldFile = new File(FILESPATH + oldName); File newFile = new File(FILESPATH + newName); return oldFile.renameTo(newFile); } /** * 在私有目录下进行文件复制 * * @param
srcFileName * : 包含路径及文件名 * @param destFileName * @return * @throws IOException */ public boolean copyDataFileTo(String srcFileName, String destFileName) throws IOException { File srcFile = new File(FILESPATH + srcFileName); File destFile = new File(FILESPATH
+ destFileName); return copyFileTo(srcFile, destFile); } /** * 复制私有目录里指定目录的所有文件 * * @param srcDirName * @param destDirName * @return * @throws IOException */ public boolean copyDataFilesTo(String srcDirName, String destDirName) throws IOException { File srcDir
= new File(FILESPATH + srcDirName); File destDir = new File(FILESPATH + destDirName); return copyFilesTo(srcDir, destDir); } /** * 移动私有目录下的单个文件 * * @param srcFileName * @param destFileName * @return * @throws IOException */ public boolean moveDataFileTo(String
srcFileName, String destFileName) throws IOException { File srcFile = new File(FILESPATH + srcFileName); File destFile = new File(FILESPATH + destFileName); return moveFileTo(srcFile, destFile); } /** * 移动私有目录下的指定目录下的所有文件 * * @param srcDirName * @param destDirName
* @return * @throws IOException */ public boolean moveDataFilesTo(String srcDirName, String destDirName) throws IOException { File srcDir = new File(FILESPATH + srcDirName); File destDir = new File(FILESPATH + destDirName); return moveFilesTo(srcDir, destDir);
} /* * 将文件写入应用私有的files目录。如:writeFile("test.txt"); */ public Output wirteFile(String fileName) throws IOException { OutputStream os = context.openFileOutput(fileName, Context.MODE_WORLD_WRITEABLE); return new Output(os); } /* * 在原有文件上继续写文件。如:appendFile("test.txt");
*/ public Output appendFile(String fileName) throws IOException { OutputStream os = context.openFileOutput(fileName, Context.MODE_APPEND); return new Output(os); } /* * 从应用的私有目录files读取文件。如:readFile("test.txt"); */ public Input readFile(String fileName) throws
IOException { InputStream is = context.openFileInput(fileName); return new Input(is); } /**********************************************************************************************************/ /*********************************************************************************************************/
*/ /** * 删除一个文件 * * @param file * @return */ public boolean delFile(File file) { if (file.isDirectory()) return false; return file.delete(); } /** * 删除一个目录(可以是非空目录) * * @param dir */ public boolean delDir(File dir) { if (dir == null || !dir.exists() || dir.isFile())
{ return false; } for (File file : dir.listFiles()) { if (file.isFile()) { file.delete(); } else if (file.isDirectory()) { delDir(file);// 递归 } } dir.delete(); return true; } /** * 拷贝一个文件,srcFile源文件,destFile目标文件 * * @param path * @throws IOException */ public
boolean copyFileTo(File srcFile, File destFile) throws IOException { if (srcFile.isDirectory() || destFile.isDirectory()) return false;// 判断是否是文件 FileInputStream fis = new FileInputStream(srcFile); FileOutputStream fos = new FileOutputStream(destFile); int
readLen = 0; byte[] buf = new byte[1024]; while ((readLen = fis.read(buf)) != -1) { fos.write(buf, 0, readLen); } fos.flush(); fos.close(); fis.close(); return true; } /** * 拷贝目录下的所有文件到指定目录 * * @param srcDir * @param destDir * @return * @throws IOException
*/ public boolean copyFilesTo(File srcDir, File destDir) throws IOException { if (!srcDir.isDirectory() || !destDir.isDirectory()) return false;// 判断是否是目录 if (!destDir.exists()) return false;// 判断目标目录是否存在 File[] srcFiles = srcDir.listFiles(); for (int i =
0; i < srcFiles.length; i++) { if (srcFiles[i].isFile()) { // 获得目标文件 File destFile = new File(destDir.getPath() + "//" + srcFiles[i].getName()); copyFileTo(srcFiles[i], destFile); } else if (srcFiles[i].isDirectory()) { File theDestDir = new File(destDir.getPath()
+ "//" + srcFiles[i].getName()); copyFilesTo(srcFiles[i],
getPath与getAbsoultePath的区别:
getAbsolutePath():返回抽象路径名的绝对路径名字符串。
[java] view plaincopyprint?
1. public static void test1(){
2. File file1 = new File(".//test1.txt");
3. File file2 = new File("D://workspace//test//test1.txt");
4. System.out.println("-----默认相对路径:取得路径不同------");
5. System.out.println(file1.getPath());
6. System.out.println(file1.getAbsolutePath());
7. System.out.println("-----默认绝对路径:取得路径相同------");
8. System.out.println(file2.getPath());
9. System.out.println(file2.getAbsolutePath());
10.
11. }
12.
13. -----默认相对路径:取得路径不同------
14. ./test1.txt
15. D:/workspace/test/./test1.txt
16. -----默认绝对路径:取得路径相同------
17. D:/workspace/test/test1.txt
18. D:/workspace/test/test1.txt
19.
20. ----------------------------------------------------
21.
22. public static void test2() throws Exception{
23. File file = new File("..//src//test1.txt");
24. System.out.println(file.getAbsolutePath());
25. System.out.println(file.getCanonicalPath());
26. }
27. D:/workspace/test/../src/test1.txt
28. D:/workspace/src/test1.txt
29.
30. --------------------------------------------
31. public static void test3() throws Exception{
32. File file = new File("D://Text.txt");
33. System.out.println(file.getCanonicalPath());
public static void test1(){ File file1 = new File(".//test1.txt"); File file2 = new File("D://workspace//test//test1.txt"); System.out.println("-----默认相对路径:取得路径不同------"); System.out.println(file1.getPath()); System.out.println(file1.getAbsolutePath()); System.out.println("-----默认绝对路径:取得路径相同------");
System.out.println(file2.getPath()); System.out.println(file2.getAbsolutePath()); } -----默认相对路径:取得路径不同------ ./test1.txt D:/workspace/test/./test1.txt -----默认绝对路径:取得路径相同------ D:/workspace/test/test1.txt D:/workspace/test/test1.txt ----------------------------------------------------
public static void test2() throws Exception{ File file = new File("..//src//test1.txt"); System.out.println(file.getAbsolutePath()); System.out.println(file.getCanonicalPath()); } D:/workspace/test/../src/test1.txt D:/workspace/src/test1.txt --------------------------------------------
public static void test3() throws Exception{ File file = new File("D://Text.txt"); System.out.println(file.getCanonicalPath());
(1),确定D盘下没有Text.txt这个文件,直接执行这段代码,得到的结果是:
D:/Text.txt注意这里试大写的Text.txt
(2)在D盘下建立一个文件,名叫text.txt,再次执行代码,得到结果
D:/text.txt同样的代码得到不同的结果。
theDestDir); } } return true; } /** * 移动一个文件 * * @param srcFile * @param destFile * @return * @throws IOException */ public boolean moveFileTo(File srcFile, File destFile) throws IOException { boolean iscopy = copyFileTo(srcFile, destFile); if (!iscopy) return
false; delFile(srcFile); return true; } /** * 移动目录下的所有文件到指定目录 * * @param srcDir * @param destDir * @return * @throws IOException */ public boolean moveFilesTo(File srcDir, File destDir) throws IOException { if (!srcDir.isDirectory() || !destDir.isDirectory())
{ return false; } File[] srcDirFiles = srcDir.listFiles(); for (int i = 0; i < srcDirFiles.length; i++) { if (srcDirFiles[i].isFile()) { File oneDestFile = new File(destDir.getPath() + "//" + srcDirFiles[i].getName()); moveFileTo(srcDirFiles[i], oneDestFile);
delFile(srcDirFiles[i]); } else if (srcDirFiles[i].isDirectory()) { File oneDestFile = new File(destDir.getPath() + "//" + srcDirFiles[i].getName()); moveFilesTo(srcDirFiles[i], oneDestFile); delDir(srcDirFiles[i]); } } return true; } }
[xhtml] view plaincopyprint?
1. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
SD卡下的文件操作:
1、判断SD卡是否插入
[java] view plaincopyprint?
1. Environment.getExternalStorageState().equals(
2. android.os.Environment.MEDIA_MOUNTED);
Environment.getExternalStorageState().equals( android.os.Environment.MEDIA_MOUNTED);
2、获得sd卡根目录:
[java] view plaincopyprint?
1. File skRoot = Environment.getExternalStorageDirectory();
File skRoot = Environment.getExternalStorageDirectory();
私有目录下的文件操作:
1、获得私有根目录:
[java] view plaincopyprint?
1. File fileRoot = Context.getFilesDir()+"//";
File fileRoot = Context.getFilesDir()+"//";
还未整理
文件夹或文件夹操作:
1、确定或获得文件夹和文件路径
a、获得文件或文件夹的绝对路径和相对路径。区别
[java] view plaincopyprint?
1. String path = File.getPath();//相对
2. String path = File.getAbsoultePath();//绝对
String path = File.getPath();//相对 String path = File.getAbsoultePath();//绝对
b 、获得文件或文件夹的父目录
[java] view plaincopyprint?
1. String parentPath = File.getParent();
String parentPath = File.getParent();
c、获得文件或文件夹的名称:
[java] view plaincopyprint?
1. String Name = File.getName();
String Name = File.getName();
2、建立文件或文件夹
[java] view plaincopyprint?
1. File.mkDir(); //建立文件夹
2. File.createNewFile();//建立文件
File.mkDir(); //建立文件夹 File.createNewFile();//建立文件
3、判断是文件或文件夹
[java] view plaincopyprint?
1. File.isDirectory()
File.isDirectory()
4、列出文件夹下的所有文件和文件夹名
[java] view plaincopyprint?
1. File[] files = File.listFiles();
File[] files = File.listFiles();
5、修改文件夹和文件名
[java] view plaincopyprint?
1. File.renameTo(dest);
File.renameTo(dest);
6、删除文件夹或文件
[java] view plaincopyprint?
1. File.delete();
File.delete();
[java] view plaincopyprint?
1. package otheri.common;
2.
3. import java.io.File;
4. import java.io.FileInputStream;
5. import java.io.FileOutputStream;
6. import java.io.IOException;
7. import java.io.InputStream;
8. import java.io.OutputStream;
9.
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. }
package otheri.common; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import otheri.io.Input; import otheri.io.Output; import android.content.Context;
import android.os.Environment; public class FileHelper { private Context context; private String SDPATH; private String FILESPATH; public FileHelper(Context context) { this.context = context; SDPATH = Environment.getExternalStorageDirectory().getPath() + "//";
FILESPATH = this.context.getFilesDir().getPath() + "//"; } /** * 在SD卡上创建文件 * * @throws IOException */ public File creatSDFile(String fileName) throws IOException { File file = new File(SDPATH + fileName); file.createNewFile(); return file; } /** * 删除SD卡上的文件
* * @param fileName */ public boolean delSDFile(String fileName) { File file = new File(SDPATH + fileName); if (file == null || !file.exists() || file.isDirectory()) return false; file.delete(); return true; } /** * 在SD卡上创建目录 * * @param dirName */ public File
creatSDDir(String dirName) { File dir = new File(SDPATH + dirName); dir.mkdir(); return dir; } /** * 删除SD卡上的目录 * * @param dirName */ public boolean delSDDir(String dirName) { File dir = new File(SDPATH + dirName); return delDir(dir); } /** * 修改SD卡上的文件或目录名
* * @param fileName */ public boolean renameSDFile(String oldfileName, String newFileName) { File oleFile = new File(SDPATH + oldfileName); File newFile = new File(SDPATH + newFileName); return oleFile.renameTo(newFile); } /** * 拷贝SD卡上的单个文件 * * @param path
* @throws IOException */ public boolean copySDFileTo(String srcFileName, String destFileName) throws IOException { File srcFile = new File(SDPATH + srcFileName); File destFile = new File(SDPATH + destFileName); return copyFileTo(srcFile, destFile); } /** *
拷贝SD卡上指定目录的所有文件 * * @param srcDirName * @param destDirName * @return * @throws IOException */ public boolean copySDFilesTo(String srcDirName, String destDirName) throws IOException { File srcDir = new File(SDPATH + srcDirName); File destDir = new File(SDPATH
+ destDirName); return copyFilesTo(srcDir, destDir); } /** * 移动SD卡上的单个文件 * * @param srcFileName * @param destFileName * @return * @throws IOException */ public boolean moveSDFileTo(String srcFileName, String destFileName) throws IOException { File srcFile
= new File(SDPATH + srcFileName); File destFile = new File(SDPATH + destFileName); return moveFileTo(srcFile, destFile); } /** * 移动SD卡上的指定目录的所有文件 * * @param srcDirName * @param destDirName * @return * @throws IOException */ public boolean moveSDFilesTo(String
srcDirName, String destDirName) throws IOException { File srcDir = new File(SDPATH + srcDirName); File destDir = new File(SDPATH + destDirName); return moveFilesTo(srcDir, destDir); } /* * 将文件写入sd卡。如:writeSDFile("test.txt"); */ public Output writeSDFile(String
fileName) throws IOException { File file = new File(SDPATH + fileName); FileOutputStream fos = new FileOutputStream(file); return new Output(fos); } /* * 在原有文件上继续写文件。如:appendSDFile("test.txt"); */ public Output appendSDFile(String fileName) throws IOException
{ File file = new File(SDPATH + fileName); FileOutputStream fos = new FileOutputStream(file, true); return new Output(fos); } /* * 从SD卡读取文件。如:readSDFile("test.txt"); */ public Input readSDFile(String fileName) throws IOException { File file = new File(SDPATH
+ fileName); FileInputStream fis = new FileInputStream(file); return new Input(fis); } /** * 建立私有文件 * * @param fileName * @return * @throws IOException */ public File creatDataFile(String fileName) throws IOException { File file = new File(FILESPATH + fileName);
file.createNewFile(); return file; } /** * 建立私有目录 * * @param dirName * @return */ public File creatDataDir(String dirName) { File dir = new File(FILESPATH + dirName); dir.mkdir(); return dir; } /** * 删除私有文件 * * @param fileName * @return */ public boolean delDataFile(String
fileName) { File file = new File(FILESPATH + fileName); return delFile(file); } /** * 删除私有目录 * * @param dirName * @return */ public boolean delDataDir(String dirName) { File file = new File(FILESPATH + dirName); return delDir(file); } /** * 更改私有文件名 * * @param
oldName * @param newName * @return */ public boolean renameDataFile(String oldName, String newName) { File oldFile = new File(FILESPATH + oldName); File newFile = new File(FILESPATH + newName); return oldFile.renameTo(newFile); } /** * 在私有目录下进行文件复制 * * @param
srcFileName * : 包含路径及文件名 * @param destFileName * @return * @throws IOException */ public boolean copyDataFileTo(String srcFileName, String destFileName) throws IOException { File srcFile = new File(FILESPATH + srcFileName); File destFile = new File(FILESPATH
+ destFileName); return copyFileTo(srcFile, destFile); } /** * 复制私有目录里指定目录的所有文件 * * @param srcDirName * @param destDirName * @return * @throws IOException */ public boolean copyDataFilesTo(String srcDirName, String destDirName) throws IOException { File srcDir
= new File(FILESPATH + srcDirName); File destDir = new File(FILESPATH + destDirName); return copyFilesTo(srcDir, destDir); } /** * 移动私有目录下的单个文件 * * @param srcFileName * @param destFileName * @return * @throws IOException */ public boolean moveDataFileTo(String
srcFileName, String destFileName) throws IOException { File srcFile = new File(FILESPATH + srcFileName); File destFile = new File(FILESPATH + destFileName); return moveFileTo(srcFile, destFile); } /** * 移动私有目录下的指定目录下的所有文件 * * @param srcDirName * @param destDirName
* @return * @throws IOException */ public boolean moveDataFilesTo(String srcDirName, String destDirName) throws IOException { File srcDir = new File(FILESPATH + srcDirName); File destDir = new File(FILESPATH + destDirName); return moveFilesTo(srcDir, destDir);
} /* * 将文件写入应用私有的files目录。如:writeFile("test.txt"); */ public Output wirteFile(String fileName) throws IOException { OutputStream os = context.openFileOutput(fileName, Context.MODE_WORLD_WRITEABLE); return new Output(os); } /* * 在原有文件上继续写文件。如:appendFile("test.txt");
*/ public Output appendFile(String fileName) throws IOException { OutputStream os = context.openFileOutput(fileName, Context.MODE_APPEND); return new Output(os); } /* * 从应用的私有目录files读取文件。如:readFile("test.txt"); */ public Input readFile(String fileName) throws
IOException { InputStream is = context.openFileInput(fileName); return new Input(is); } /**********************************************************************************************************/ /*********************************************************************************************************/
*/ /** * 删除一个文件 * * @param file * @return */ public boolean delFile(File file) { if (file.isDirectory()) return false; return file.delete(); } /** * 删除一个目录(可以是非空目录) * * @param dir */ public boolean delDir(File dir) { if (dir == null || !dir.exists() || dir.isFile())
{ return false; } for (File file : dir.listFiles()) { if (file.isFile()) { file.delete(); } else if (file.isDirectory()) { delDir(file);// 递归 } } dir.delete(); return true; } /** * 拷贝一个文件,srcFile源文件,destFile目标文件 * * @param path * @throws IOException */ public
boolean copyFileTo(File srcFile, File destFile) throws IOException { if (srcFile.isDirectory() || destFile.isDirectory()) return false;// 判断是否是文件 FileInputStream fis = new FileInputStream(srcFile); FileOutputStream fos = new FileOutputStream(destFile); int
readLen = 0; byte[] buf = new byte[1024]; while ((readLen = fis.read(buf)) != -1) { fos.write(buf, 0, readLen); } fos.flush(); fos.close(); fis.close(); return true; } /** * 拷贝目录下的所有文件到指定目录 * * @param srcDir * @param destDir * @return * @throws IOException
*/ public boolean copyFilesTo(File srcDir, File destDir) throws IOException { if (!srcDir.isDirectory() || !destDir.isDirectory()) return false;// 判断是否是目录 if (!destDir.exists()) return false;// 判断目标目录是否存在 File[] srcFiles = srcDir.listFiles(); for (int i =
0; i < srcFiles.length; i++) { if (srcFiles[i].isFile()) { // 获得目标文件 File destFile = new File(destDir.getPath() + "//" + srcFiles[i].getName()); copyFileTo(srcFiles[i], destFile); } else if (srcFiles[i].isDirectory()) { File theDestDir = new File(destDir.getPath()
+ "//" + srcFiles[i].getName()); copyFilesTo(srcFiles[i],
getPath与getAbsoultePath的区别:
getAbsolutePath():返回抽象路径名的绝对路径名字符串。
[java] view plaincopyprint?
1. public static void test1(){
2. File file1 = new File(".//test1.txt");
3. File file2 = new File("D://workspace//test//test1.txt");
4. System.out.println("-----默认相对路径:取得路径不同------");
5. System.out.println(file1.getPath());
6. System.out.println(file1.getAbsolutePath());
7. System.out.println("-----默认绝对路径:取得路径相同------");
8. System.out.println(file2.getPath());
9. System.out.println(file2.getAbsolutePath());
10.
11. }
12.
13. -----默认相对路径:取得路径不同------
14. ./test1.txt
15. D:/workspace/test/./test1.txt
16. -----默认绝对路径:取得路径相同------
17. D:/workspace/test/test1.txt
18. D:/workspace/test/test1.txt
19.
20. ----------------------------------------------------
21.
22. public static void test2() throws Exception{
23. File file = new File("..//src//test1.txt");
24. System.out.println(file.getAbsolutePath());
25. System.out.println(file.getCanonicalPath());
26. }
27. D:/workspace/test/../src/test1.txt
28. D:/workspace/src/test1.txt
29.
30. --------------------------------------------
31. public static void test3() throws Exception{
32. File file = new File("D://Text.txt");
33. System.out.println(file.getCanonicalPath());
public static void test1(){ File file1 = new File(".//test1.txt"); File file2 = new File("D://workspace//test//test1.txt"); System.out.println("-----默认相对路径:取得路径不同------"); System.out.println(file1.getPath()); System.out.println(file1.getAbsolutePath()); System.out.println("-----默认绝对路径:取得路径相同------");
System.out.println(file2.getPath()); System.out.println(file2.getAbsolutePath()); } -----默认相对路径:取得路径不同------ ./test1.txt D:/workspace/test/./test1.txt -----默认绝对路径:取得路径相同------ D:/workspace/test/test1.txt D:/workspace/test/test1.txt ----------------------------------------------------
public static void test2() throws Exception{ File file = new File("..//src//test1.txt"); System.out.println(file.getAbsolutePath()); System.out.println(file.getCanonicalPath()); } D:/workspace/test/../src/test1.txt D:/workspace/src/test1.txt --------------------------------------------
public static void test3() throws Exception{ File file = new File("D://Text.txt"); System.out.println(file.getCanonicalPath());
(1),确定D盘下没有Text.txt这个文件,直接执行这段代码,得到的结果是:
D:/Text.txt注意这里试大写的Text.txt
(2)在D盘下建立一个文件,名叫text.txt,再次执行代码,得到结果
D:/text.txt同样的代码得到不同的结果。
theDestDir); } } return true; } /** * 移动一个文件 * * @param srcFile * @param destFile * @return * @throws IOException */ public boolean moveFileTo(File srcFile, File destFile) throws IOException { boolean iscopy = copyFileTo(srcFile, destFile); if (!iscopy) return
false; delFile(srcFile); return true; } /** * 移动目录下的所有文件到指定目录 * * @param srcDir * @param destDir * @return * @throws IOException */ public boolean moveFilesTo(File srcDir, File destDir) throws IOException { if (!srcDir.isDirectory() || !destDir.isDirectory())
{ return false; } File[] srcDirFiles = srcDir.listFiles(); for (int i = 0; i < srcDirFiles.length; i++) { if (srcDirFiles[i].isFile()) { File oneDestFile = new File(destDir.getPath() + "//" + srcDirFiles[i].getName()); moveFileTo(srcDirFiles[i], oneDestFile);
delFile(srcDirFiles[i]); } else if (srcDirFiles[i].isDirectory()) { File oneDestFile = new File(destDir.getPath() + "//" + srcDirFiles[i].getName()); moveFilesTo(srcDirFiles[i], oneDestFile); delDir(srcDirFiles[i]); } } return true; } }
相关文章推荐
- Android基础笔记一之测试与文件操作
- Android开发笔记(三十二)文件基础操作
- [Android基础]文件读写操作
- 【Android基础】文件操作
- 【android基础】android文件操作
- android 基础 文件下载 文件操作 网络操作
- Android的NDK开发(5)————Android JNI层实现文件的read、write与seek操作
- android的文件操作
- 基础框架-文件操作-目录操作
- Android 文件操作
- Python文件基础操作(入门1)
- java 文件基础操作
- Android 绘图基础:Bitmap(位图)与Matrix(矩阵)实现图片5种操作(平移、旋转、错切、缩放、对称)
- Android入门之文件系统操作(一)简单的文件浏览器 (转)
- JavaScript文件操作(1)-基础
- Android文件读写操作(assets 文件、 raw文件、内部存储文件、外部存储文件)
- Android下SD卡文件操作与数据读写
- android操作sdcard中的多媒体文件(一)——音乐列表的制作
- unix基础教程6 与文件相关的操作
- android中file文件常见操作