【转载】android 文件夹文件操作代码大全

android的文件操作要有权限:

 

  1. <uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> 
  1. <uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> 

 

 

 

 

SD卡下的文件操作:

1、判断SD卡是否插入

 

  1. Environment.getExternalStorageState().equals( 
  2.         android.os.Environment.MEDIA_MOUNTED); 
  1. Environment.getExternalStorageState().equals( 
  2. android.os.Environment.MEDIA_MOUNTED); 

 

 

 

2、获得sd卡根目录:

 

  1. File skRoot = Environment.getExternalStorageDirectory(); 
  1. File skRoot = Environment.getExternalStorageDirectory(); 

 

 

 

私有目录下的文件操作:

1、获得私有根目录:

 

  1. File fileRoot = Context.getFilesDir()+"//"
  1. File fileRoot = Context.getFilesDir()+"//"

 

 

 

还未整理

 

文件夹或文件夹操作:

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

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

 

  1. String path = File.getPath();//相对  
  2. String path = File.getAbsoultePath();//绝对 
  1. String path = File.getPath();//相对  
  2. String path = File.getAbsoultePath();//绝对 

 

 

 

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

 

  1. String parentPath = File.getParent(); 
  1. String parentPath = File.getParent(); 

 

 

 

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

 

  1. String Name = File.getName(); 
  1. String Name = File.getName(); 

 

 

 

2、建立文件或文件夹

 

  1. File.mkDir(); //建立文件夹  
  2. File.createNewFile();//建立文件 
  1. File.mkDir(); //建立文件夹  
  2. File.createNewFile();//建立文件 

 

 

 

3、判断是文件或文件夹

 

  1. File.isDirectory() 
  1. File.isDirectory() 

 

 

 

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

 

  1. File[] files = File.listFiles(); 
  1. File[] files = File.listFiles(); 

 

 

 

5、修改文件夹和文件名

 

  1. File.renameTo(dest); 
  1. File.renameTo(dest); 

 

 

 

6、删除文件夹或文件

 

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

  

 

 

 

 

getPath与getAbsoultePath的区别:

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

 

  1. publicstaticvoid 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. publicstaticvoid 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. publicstaticvoid test3() throws Exception{ 
  32.         File file = new File("D://Text.txt"); 
  33.         System.out.println(file.getCanonicalPath()); 
  1. publicstaticvoid 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. ./test1.txt 
  12. D:/workspace/test/./test1.txt 
  13. -----默认绝对路径:取得路径相同------ 
  14. D:/workspace/test/test1.txt 
  15. D:/workspace/test/test1.txt 
  16. ---------------------------------------------------- 
  17. publicstaticvoid test2() throws Exception{ 
  18. File file = new File("..//src//test1.txt"); 
  19. System.out.println(file.getAbsolutePath()); 
  20. System.out.println(file.getCanonicalPath()); 
  21. D:/workspace/test/../src/test1.txt 
  22. D:/workspace/src/test1.txt 
  23. -------------------------------------------- 
  24. publicstaticvoid test3() throws Exception{ 
  25. File file = new File("D://Text.txt"); 
  26. System.out.println(file.getCanonicalPath()); 

 

 

 

(1),确定D盘下没有Text.txt这个文件,直接执行这段代码,得到的结果是: D:/Text.txt注意这里试大写的Text.txt (2)在D盘下建立一个文件,名叫text.txt,再次执行代码,得到结果 D:/text.txt同样的代码得到不同的结果。

posted @ 2013-05-23 14:09  hustshea  阅读(497)  评论(0)    收藏  举报