当前位置:编程学习 > 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); 
补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,