android的文件操作要有权限:

view plain copy to clipboard print ?
  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卡是否插入

view plain copy to clipboard print ?
  1. Environment.getExternalStorageState().equals(
  2. android.os.Environment.MEDIA_MOUNTED);
Environment.getExternalStorageState().equals( android.os.Environment.MEDIA_MOUNTED);

2、获得sd卡根目录:

view plain copy to clipboard print ?
  1. File skRoot = Environment.getExternalStorageDirectory();
File skRoot = Environment.getExternalStorageDirectory();

私有目录下的文件操作:

1、获得私有根目录:

view plain copy to clipboard print ?
  1. File fileRoot = Context.getFilesDir()+"//";
File fileRoot = Context.getFilesDir()+"//";

还未整理

文件夹或文件夹操作:

1、确定或获得文件夹和文件路径

a、获得文件或文件夹的绝对路径和相对路径。区别

view plain copy to clipboard print ?
  1. String path = File.getPath();//相对
  2. String path = File.getAbsoultePath();//绝对
String path = File.getPath();//相对 String path = File.getAbsoultePath();//绝对

b 、获得文件或文件夹的父目录

view plain copy to clipboard print ?
  1. String parentPath = File.getParent();
String parentPath = File.getParent();

c、获得文件或文件夹的名称:

view plain copy to clipboard print ?
  1. String Name = File.getName();
String Name = File.getName();

2、建立文件或文件夹

view plain copy to clipboard print ?
  1. File.mkDir(); //建立文件夹
  2. File.createNewFile();//建立文件
File.mkDir(); //建立文件夹 File.createNewFile();//建立文件

3、判断是文件或文件夹

view plain copy to clipboard print ?
  1. File.isDirectory()
File.isDirectory()

4、列出文件夹下的所有文件和文件夹名

view plain copy to clipboard print ?
  1. File[] files = File.listFiles();
File[] files = File.listFiles();

5、修改文件夹和文件名

view plain copy to clipboard print ?
  1. File.renameTo(dest);
File.renameTo(dest);

6、删除文件夹或文件

view plain copy to clipboard print ?
  1. File.delete();
File.delete();

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

getPath与getAbsoultePath的区别:

getAbsolutePath():返回抽象路径名的绝对路径名字符串。

view plain copy to clipboard print ?
  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. ./test1.txt
  13. D:/workspace/test/./test1.txt
  14. -----默认绝对路径:取得路径相同------
  15. D:/workspace/test/test1.txt
  16. D:/workspace/test/test1.txt
  17. ----------------------------------------------------
  18. public static void test2() throws Exception{
  19. File file = new File("..//src//test1.txt");
  20. System.out.println(file.getAbsolutePath());
  21. System.out.println(file.getCanonicalPath());
  22. }
  23. D:/workspace/test/../src/test1.txt
  24. D:/workspace/src/test1.txt
  25. --------------------------------------------
  26. public static void test3() throws Exception{
  27. File file = new File("D://Text.txt");
  28. 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同样的代码得到不同的结果。

更多相关文章

  1. android studio调整默认的debug.keystore位置。
  2. Android之自定义ProgressBar
  3. android 读取raw文件下文件内容
  4. Android(安卓)实现文件(图片)上传
  5. Android应用程序键盘(Keyboard)消息处理机制分析(12)
  6. eclipse中修改工程的Android版本
  7. android客户端利用sokcet通信和向Java服务端发请求,Java服务端把
  8. android ProgressBar 的使用
  9. android getResources的作用

随机推荐

  1. android 启动流程(中文)
  2. Android ART虚拟机(一)
  3. Android之AlarmManager(全局定时器/闹钟)
  4. Android中ListView控件的简单使用
  5. 【起航计划 013】2015 起航计划 Android(
  6. Android Gradle 学习笔记整理
  7. React Native 入门及其与Android&Flutter
  8. 【Appetite】ionic3实录(四)微调Segment,并
  9. Android自动化测试之虚拟机中软件安装方
  10. Android 中的数据绑定(1)