java自定義文件操作類實例代碼 – JAVA編程語言程序開發技術文章

  1. package cn.edu.tongji.cims.wade.system;   
  2. import java.io.*; 
  3. public class FileOperate { 
  4. public FileOperate() { 
  5. /**
  6. * 新建目錄
  7. * @param folderPath String 如 c:/fqf
  8. * @return boolean
  9. */ 
  10. public void newFolder(String folderPath) { 
  11. try { 
  12. String filePath = folderPath; 
  13. filePath = filePath.toString(); 
  14. java.io.File myFilePath = new java.io.File(filePath); 
  15. if (!myFilePath.exists()) { 
  16. myFilePath.mkdir(); 
  17. catch (Exception e) { 
  18. System.out.println(“新建目錄操作出錯”); 
  19. e.printStackTrace(); 
  20. /**
  21. * 新建文件
  22. * @param filePathAndName String 文件路徑及名稱 如c:/fqf.txt
  23. * @param fileContent String 文件內容
  24. * @return boolean
  25. */ 
  26. public void newFile(String filePathAndName, String fileContent) { 
  27. try { 
  28. String filePath = filePathAndName; 
  29. filePath = filePath.toString(); 
  30. File myFilePath = new File(filePath); 
  31. if (!myFilePath.exists()) { 
  32. myFilePath.createNewFile(); 
  33. FileWriter resultFile = new FileWriter(myFilePath); 
  34. PrintWriter myFile = new PrintWriter(resultFile); 
  35. String strContent = fileContent; 
  36. myFile.println(strContent); 
  37. resultFile.close(); 
  38. catch (Exception e) { 
  39. System.out.println(“新建目錄操作出錯”); 
  40. e.printStackTrace();  
  41. /**
  42. * 刪除文件
  43. * @param filePathAndName String 文件路徑及名稱 如c:/fqf.txt
  44. * @param fileContent String
  45. * @return boolean
  46. */ 
  47. public void delFile(String filePathAndName) { 
  48. try { 
  49. String filePath = filePathAndName; 
  50. filePath = filePath.toString(); 
  51. java.io.File myDelFile = new java.io.File(filePath); 
  52. myDelFile.delete(); 
  53. catch (Exception e) { 
  54. System.out.println(“刪除文件操作出錯”); 
  55. e.printStackTrace(); 
  56. /**
  57. * 刪除文件夾
  58. * @param filePathAndName String 文件夾路徑及名稱 如c:/fqf
  59. * @param fileContent String
  60. * @return boolean
  61. */ 
  62. public void delFolder(String folderPath) { 
  63. try { 
  64. delAllFile(folderPath); //刪除完裡面所有內容 
  65. String filePath = folderPath; 
  66. filePath = filePath.toString(); 
  67. java.io.File myFilePath = new java.io.File(filePath); 
  68. myFilePath.delete(); //刪除空文件夾    
  69. catch (Exception e) { 
  70. System.out.println(“刪除文件夾操作出錯”); 
  71. e.printStackTrace();    
  72. /**
  73. * 刪除文件夾裡面的所有文件
  74. * @param path String 文件夾路徑 如 c:/fqf
  75. */ 
  76. public void delAllFile(String path) { 
  77. File file = new File(path); 
  78. if (!file.exists()) { 
  79. return; 
  80. if (!file.isDirectory()) { 
  81. return; 
  82. String[] tempList = file.list(); 
  83. File temp = null; 
  84. for (int i = 0; i < tempList.length; i ) { 
  85. if (path.endsWith(File.separator)) { 
  86. temp = new File(path tempList[i]); 
  87. else { 
  88. temp = new File(path File.separator tempList[i]); 
  89. if (temp.isFile()) { 
  90. temp.delete(); 
  91. if (temp.isDirectory()) { 
  92. delAllFile(path “/” tempList[i]);//先刪除文件夾裡面的文件 
  93. delFolder(path “/” tempList[i]);//再刪除空文件夾 
  94. /**
  95. * 復制單個文件
  96. * @param oldPath String 原文件路徑 如:c:/fqf.txt
  97. * @param newPath String 復制後路徑 如:f:/fqf.txt
  98. * @return boolean
  99. */ 
  100. public void copyFile(String oldPath, String newPath) { 
  101. try { 
  102. int bytesum = 0; 
  103. int byteread = 0; 
  104. File oldfile = new File(oldPath); 
  105. if (oldfile.exists()) { //文件存在時 
  106. InputStream inStream = new FileInputStream(oldPath); //讀入原文件 
  107. FileOutputStream fs = new FileOutputStream(newPath); 
  108. byte[] buffer = new byte[1444]; 
  109. int length; 
  110. while ( (byteread = inStream.read(buffer)) != -1) { 
  111. bytesum = byteread; //字節數 文件大小 
  112. System.out.println(bytesum); 
  113. fs.write(buffer, 0, byteread); 
  114. inStream.close(); 
  115. catch (Exception e) { 
  116. System.out.println(“復制單個文件操作出錯”); 
  117. e.printStackTrace();    
  118. /**
  119. * 復制整個文件夾內容
  120. * @param oldPath String 原文件路徑 如:c:/fqf
  121. * @param newPath String 復制後路徑 如:f:/fqf/ff
  122. * @return boolean
  123. */ 
  124. public void copyFolder(String oldPath, String newPath) { 
  125. try { 
  126. (new File(newPath)).mkdirs(); //如果文件夾不存在 則建立新文件夾 
  127. File a=new File(oldPath); 
  128. String[] file=a.list(); 
  129. File temp=null; 
  130. for (int i = 0; i < file.length; i ) { 
  131. if(oldPath.endsWith(File.separator)){ 
  132. temp=new File(oldPath file[i]); 
  133. else{ 
  134. temp=new File(oldPath File.separator file[i]); 
  135. if(temp.isFile()){ 
  136. FileInputStream input = new FileInputStream(temp); 
  137. FileOutputStream output = new FileOutputStream(newPath “/”  
  138. (temp.getName()).toString()); 
  139. byte[] b = new byte[1024 * 5]; 
  140. int len; 
  141. while ( (len = input.read(b)) != -1) { 
  142. output.write(b, 0, len); 
  143. output.flush(); 
  144. output.close(); 
  145. input.close(); 
  146. if(temp.isDirectory()){//如果是子文件夾 
  147. copyFolder(oldPath “/” file[i],newPath “/” file[i]); 
  148. catch (Exception e) { 
  149. System.out.println(“復制整個文件夾內容操作出錯”); 
  150. e.printStackTrace();   
  151. /**
  152. * 移動文件到指定目錄
  153. * @param oldPath String 如:c:/fqf.txt
  154. * @param newPath String 如:d:/fqf.txt
  155. */ 
  156. public void moveFile(String oldPath, String newPath) { 
  157. copyFile(oldPath, newPath); 
  158. delFile(oldPath); 
  159. /**
  160. * 移動文件到指定目錄
  161. * @param oldPath String 如:c:/fqf.txt
  162. * @param newPath String 如:d:/fqf.txt
  163. */ 
  164. public void moveFolder(String oldPath, String newPath) { 
  165. copyFolder(oldPath, newPath); 
  166. delFolder(oldPath); 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *