java文件和文件夹复制、删除、移动操作

[java]view plaincopy

  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.FileOutputStream;
  4. import java.io.FileWriter;
  5. import java.io.InputStream;
  6. import java.io.PrintWriter;
  7. public class CopyFile {
  8. public CopyFile() {
  9. }
  10. /**
  11. * 新建目录
  12. * @param folderPath String 如 c:/fqf
  13. * @return boolean
  14. */
  15. public void newFolder(String folderPath) {
  16. try {
  17. String filePath = folderPath;
  18. filePath = filePath.toString();
  19. java.io.File myFilePath = new java.io.File(filePath);
  20. if (!myFilePath.exists()) {
  21. myFilePath.mkdir();
  22. }
  23. }
  24. catch (Exception e) {
  25. System.out.println("新建目录操作出错");
  26. e.printStackTrace();
  27. }
  28. }
  29. /**
  30. * 新建文件
  31. * @param filePathAndName String 文件路径及名称 如c:/fqf.txt
  32. * @param fileContent String 文件内容
  33. * @return boolean
  34. */
  35. public void newFile(String filePathAndName, String fileContent) {
  36. try {
  37. String filePath = filePathAndName;
  38. filePath = filePath.toString(); //取的路径及文件名
  39. File myFilePath = new File(filePath);
  40. /**如果文件不存在就建一个新文件*/
  41. if (!myFilePath.exists()) {
  42. myFilePath.createNewFile();
  43. }
  44. FileWriter resultFile = new FileWriter(myFilePath); //用来写入字符文件的便捷类, 在给出 File 对象的情况下构造一个 FileWriter 对象
  45. PrintWriter myFile = new PrintWriter(resultFile); //向文本输出流打印对象的格式化表示形式,使用指定文件创建不具有自动行刷新的新 PrintWriter。
  46. String strContent = fileContent;
  47. myFile.println(strContent);
  48. resultFile.close();
  49. }
  50. catch (Exception e) {
  51. System.out.println("新建文件操作出错");
  52. e.printStackTrace();
  53. }
  54. }
  55. /**
  56. * 删除文件
  57. * @param filePathAndName String 文件路径及名称 如c:/fqf.txt
  58. * @param fileContent String
  59. * @return boolean
  60. */
  61. public void delFile(String filePathAndName) {
  62. try {
  63. String filePath = filePathAndName;
  64. filePath = filePath.toString();
  65. java.io.File myDelFile = new java.io.File(filePath);
  66. myDelFile.delete();
  67. }
  68. catch (Exception e) {
  69. System.out.println("删除文件操作出错");
  70. e.printStackTrace();
  71. }
  72. }
  73. /**
  74. * 删除文件夹
  75. * @param filePathAndName String 文件夹路径及名称 如c:/fqf
  76. * @param fileContent String
  77. * @return boolean
  78. */
  79. public void delFolder(String folderPath) {
  80. try {
  81. delAllFile(folderPath); //删除完里面所有内容
  82. String filePath = folderPath;
  83. filePath = filePath.toString();
  84. java.io.File myFilePath = new java.io.File(filePath);
  85. myFilePath.delete(); //删除空文件夹
  86. }
  87. catch (Exception e) {
  88. System.out.println("删除文件夹操作出错");
  89. e.printStackTrace();
  90. }
  91. }
  92. /**
  93. * 删除文件夹里面的所有文件
  94. * @param path String 文件夹路径 如 c:/fqf
  95. */
  96. public void delAllFile(String path) {
  97. File file = new File(path);
  98. if (!file.exists()) {
  99. return;
  100. }
  101. if (!file.isDirectory()) {
  102. return;
  103. }
  104. String[] tempList = file.list();
  105. File temp = null;
  106. for (int i = 0; i < tempList.length; i++) {
  107. if (path.endsWith(File.separator)) {
  108. temp = new File(path + tempList[i]);
  109. }
  110. else {
  111. temp = new File(path + File.separator + tempList[i]);
  112. }
  113. if (temp.isFile()) {
  114. temp.delete();
  115. }
  116. if (temp.isDirectory()) {
  117. delAllFile(path+"/"+ tempList[i]);//先删除文件夹里面的文件
  118. delFolder(path+"/"+ tempList[i]);//再删除空文件夹
  119. }
  120. }
  121. }
  122. /**
  123. * 复制单个文件
  124. * @param oldPath String 原文件路径 如:c:/fqf.txt
  125. * @param newPath String 复制后路径 如:f:/fqf.txt
  126. * @return boolean
  127. */
  128. public void copyFile(String oldPath, String newPath) {
  129. try {
  130. // int bytesum = 0;
  131. int byteread = 0;
  132. File oldfile = new File(oldPath);
  133. if (oldfile.exists()) { //文件存在时
  134. InputStream inStream = new FileInputStream(oldPath); //读入原文件
  135. FileOutputStream fs = new FileOutputStream(newPath);
  136. byte[] buffer = new byte[1444];
  137. // int length;
  138. while ( (byteread = inStream.read(buffer)) != -1) {
  139. // bytesum += byteread; //字节数 文件大小
  140. // System.out.println(bytesum);
  141. fs.write(buffer, 0, byteread);
  142. }
  143. inStream.close();
  144. }
  145. }
  146. catch (Exception e) {
  147. System.out.println("复制单个文件操作出错");
  148. e.printStackTrace();
  149. }
  150. }
  151. /**
  152. * 复制整个文件夹内容
  153. * @param oldPath String 原文件路径 如:c:/fqf
  154. * @param newPath String 复制后路径 如:f:/fqf/ff
  155. * @return boolean
  156. */
  157. public void copyFolder(String oldPath, String newPath) {
  158. try {
  159. (new File(newPath)).mkdirs(); //如果文件夹不存在 则建立新文件夹
  160. File a=new File(oldPath);
  161. String[] file=a.list();
  162. File temp=null;
  163. for (int i = 0; i < file.length; i++) {
  164. if(oldPath.endsWith(File.separator)){
  165. temp=new File(oldPath+file[i]);
  166. }
  167. else{
  168. temp=new File(oldPath+File.separator+file[i]);
  169. }
  170. if(temp.isFile()){
  171. FileInputStream input = new FileInputStream(temp);
  172. FileOutputStream output = new FileOutputStream(newPath + "/" +
  173. (temp.getName()).toString());
  174. byte[] b = new byte[1024 * 5];
  175. int len;
  176. while ( (len = input.read(b)) != -1) {
  177. output.write(b, 0, len);
  178. }
  179. output.flush();
  180. output.close();
  181. input.close();
  182. }
  183. if(temp.isDirectory()){//如果是子文件夹
  184. copyFolder(oldPath+"/"+file[i],newPath+"/"+file[i]);
  185. }
  186. }
  187. }
  188. catch (Exception e) {
  189. System.out.println("复制整个文件夹内容操作出错");
  190. e.printStackTrace();
  191. }
  192. }
  193. /**
  194. * 移动文件到指定目录
  195. * @param oldPath String 如:c:/fqf.txt
  196. * @param newPath String 如:d:/fqf.txt
  197. */
  198. public void moveFile(String oldPath, String newPath) {
  199. copyFile(oldPath, newPath);
  200. delFile(oldPath);
  201. }
  202. /**
  203. * 移动文件到指定目录
  204. * @param oldPath String 如:c:/fqf.txt
  205. * @param newPath String 如:d:/fqf.txt
  206. */
  207. public void moveFolder(String oldPath, String newPath) {
  208. copyFolder(oldPath, newPath);
  209. delFolder(oldPath);
  210. }
  211. public static void main(String[] args){
  212. CopyFile file = new CopyFile();
  213. // file.newFolder("newFolder22222");
  214. file.delAllFile("E:/1");
  215. }
  216. // 拷贝文件
  217. private void copyFile2(String source, String dest) {
  218. try {
  219. File in = new File(source);
  220. File out = new File(dest);
  221. FileInputStream inFile = new FileInputStream(in);
  222. FileOutputStream outFile = new FileOutputStream(out);
  223. byte[] buffer = new byte[10240];
  224. int i = 0;
  225. while ((i = inFile.read(buffer)) != -1) {
  226. outFile.write(buffer, 0, i);
  227. }//end while
  228. inFile.close();
  229. outFile.close();
  230. }//end try
  231. catch (Exception e) {
  232. }//end catch
  233. }//end copyFile
  234. }