android 开发中的文件操作

反正加s就对了,mkdir不能创建多个目录


android的文件操作要有权限:

  1. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>  
[xhtml]   view plain  copy
  1. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>  

 

 

SD卡下的文件操作:

1、判断SD卡是否插入

  1. Environment.getExternalStorageState().equals(  
  2.         android.os.Environment.MEDIA_MOUNTED);  
[java]   view plain  copy
  1. Environment.getExternalStorageState().equals(  
  2. android.os.Environment.MEDIA_MOUNTED);  

 

2、获得sd卡根目录:

  1. File skRoot = Environment.getExternalStorageDirectory();  
[java]   view plain  copy
  1. File skRoot = Environment.getExternalStorageDirectory();  

 

私有目录下的文件操作:

1、获得私有根目录:

  1. File fileRoot = Context.getFilesDir()+"//";  
[java]   view plain  copy
  1. File fileRoot = Context.getFilesDir()+"//";  

 

还未整理

 

文件夹或文件夹操作:

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

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

  1. String path = File.getPath();//相对   
  2. String path = File.getAbsoultePath();//绝对  
[java]   view plain  copy
  1. String path = File.getPath();//相对  
  2. String path = File.getAbsoultePath();//绝对  

 

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

  1. String parentPath = File.getParent();  
[java]   view plain  copy
  1. String parentPath = File.getParent();  

 

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

  1. String Name = File.getName();  
[java]   view plain  copy
  1. String Name = File.getName();  

 

2、建立文件或文件夹

  1. File.mkDir(); //建立文件夹   
  2. File.createNewFile();//建立文件  
[java]   view plain  copy
  1. File.mkDir(); //建立文件夹  
  2. File.createNewFile();//建立文件  

 

3、判断是文件或文件夹

  1. File.isDirectory()  
[java]   view plain  copy
  1. File.isDirectory()  

 

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

  1. File[] files = File.listFiles();  
[java]   view plain  copy
  1. File[] files = File.listFiles();  

 

5、修改文件夹和文件名

  1. File.renameTo(dest);  
[java]   view plain  copy
  1. File.renameTo(dest);  

 

6、删除文件夹或文件

  1. File.delete();  
[java]   view plain  copy
  1. File.delete();  

 

 

 

  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. }  
[java]   view plain  copy
  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. }  
  

 

 

getPath与getAbsoultePath的区别:

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

  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());  
[java]   view plain  copy
  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());  
分类: Android  2011-10-29 16:55   3317人阅读   评论(0)   收藏   举报

反正加s就对了,mkdir不能创建多个目录

 

《------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------》 

 

 

android的文件操作要有权限:

  1. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>  
[xhtml]   view plain  copy
  1. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>  

 

 

SD卡下的文件操作:

1、判断SD卡是否插入

  1. Environment.getExternalStorageState().equals(  
  2.         android.os.Environment.MEDIA_MOUNTED);  
[java]   view plain  copy
  1. Environment.getExternalStorageState().equals(  
  2. android.os.Environment.MEDIA_MOUNTED);  

 

2、获得sd卡根目录:

  1. File skRoot = Environment.getExternalStorageDirectory();  
[java]   view plain  copy
  1. File skRoot = Environment.getExternalStorageDirectory();  

 

私有目录下的文件操作:

1、获得私有根目录:

  1. File fileRoot = Context.getFilesDir()+"//";  
[java]   view plain  copy
  1. File fileRoot = Context.getFilesDir()+"//";  

 

还未整理

 

文件夹或文件夹操作:

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

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

  1. String path = File.getPath();//相对   
  2. String path = File.getAbsoultePath();//绝对  
[java]   view plain  copy
  1. String path = File.getPath();//相对  
  2. String path = File.getAbsoultePath();//绝对  

 

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

  1. String parentPath = File.getParent();  
[java]   view plain  copy
  1. String parentPath = File.getParent();  

 

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

  1. String Name = File.getName();  
[java]   view plain  copy
  1. String Name = File.getName();  

 

2、建立文件或文件夹

  1. File.mkDir(); //建立文件夹   
  2. File.createNewFile();//建立文件  
[java]   view plain  copy
  1. File.mkDir(); //建立文件夹  
  2. File.createNewFile();//建立文件  

 

3、判断是文件或文件夹

  1. File.isDirectory()  
[java]   view plain  copy
  1. File.isDirectory()  

 

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

  1. File[] files = File.listFiles();  
[java]   view plain  copy
  1. File[] files = File.listFiles();  

 

5、修改文件夹和文件名

  1. File.renameTo(dest);  
[java]   view plain  copy
  1. File.renameTo(dest);  

 

6、删除文件夹或文件

  1. File.delete();  
[java]   view plain  copy
  1. File.delete();  

 

 

 

  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. }  
[java]   view plain  copy
  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. }  
  

 

 

getPath与getAbsoultePath的区别:

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

  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());  
[java]   view plain  copy
  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());  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值