java自定义文件操作类实例代码(一)

2014-11-23 23:38:05 · 作者: · 浏览: 0
  1. package cn.edu.tongji.cims.wade.system;
  2. import java.io.*;
  3. public class FileOperate {
  4. public FileOperate() {
  5. }
  6. /**
  7. * 新建目录
  8. * @param folderPath String 如 c:/fqf
  9. * @return boolean
  10. */
  11. public void newFolder(String folderPath) {
  12. try {
  13. String filePath = folderPath;
  14. filePath = filePath.toString();
  15. java.io.File myFilePath = new java.io.File(filePath);
  16. if (!myFilePath.exists()) {
  17. myFilePath.mkdir();
  18. }
  19. }
  20. catch (Exception e) {
  21. System.out.println("新建目录操作出错");
  22. e.printStackTrace();
  23. }
  24. }
  25. /**
  26. * 新建文件
  27. * @param filePathAndName String 文件路径及名称 如c:/fqf.txt
  28. * @param fileContent String 文件内容
  29. * @return boolean
  30. */
  31. public void newFile(String filePathAndName, String fileContent) {
  32. try {
  33. String filePath = filePathAndName;
  34. filePath = filePath.toString();
  35. File myFilePath = new File(filePath);
  36. if (!myFilePath.exists()) {
  37. myFilePath.createNewFile();
  38. }
  39. FileWriter resultFile = new FileWriter(myFilePath);
  40. PrintWriter myFile = new PrintWriter(resultFile);
  41. String strContent = fileContent;
  42. myFile.println(strContent);
  43. resultFile.close();
  44. }
  45. catch (Exception e) {
  46. System.out.println("新建目录操作出错");
  47. e.printStackTrace();
  48. }
  49. }
  50. /**
  51. * 删除文件
  52. * @param filePathAndName String 文件路径及名称 如c:/fqf.txt
  53. * @param fileContent String
  54. * @return boolean
  55. */
  56. public void delFile(String filePathAndName) {
  57. try {
  58. String filePath = filePathAndName;
  59. filePath = filePath.toString();
  60. java.io.File myDelFile = new java.io.File(filePath);
  61. myDelFile.delete();
  62. }
  63. catch (Exception e) {
  64. System.out.println("删除文件操作出错");
  65. e.printStackTrace();
  66. }
  67. }
  68. /**
  69. * 删除文件夹
  70. * @param filePathAndName String 文件夹路径及名称 如c:/fqf
  71. * @param fileContent String
  72. * @return boolean
  73. */
  74. public void delFolder(String folderPath) {
  75. try {
  76. delAllFile(folderPath); //删除完里面所有内容
  77. String filePath = folderPath;
  78. filePath = filePath.toString();
  79. java.io.File myFilePath = new java.io.File(filePath);
  80. myFilePath.delete(); //删除空文件夹
  81. }
  82. catch (Exception e) {
  83. System.out.println("删除文件夹操作出错");
  84. e.printStackTrace();
  85. }
  86. }
  87. /**
  88. * 删除文件夹里面的所有文件
  89. * @param path String 文件夹路径 如 c:/fqf
  90. */
  91. public void delAllFile(String path) {
  92. File file = new File(path);
  93. if (!file.exists()) {
  94. return;
  95. }
  96. if (!file.isDirectory()) {
  97. return;
  98. }
  99. String[] tempList = file.list();
  100. File temp = null;
  101. for (int i = 0; i < tempList.length; i ) {
  102. if (path.endsWith(File.separator)) {
  103. temp = new File(path tempList[i]);
  104. }
  105. else {
  106. temp = new File(path File.separator tempList[i]);
  107. }
  108. if (temp.isFile()) {
  109. temp.delete();
  110. }
  111. if (temp.isDirectory()) {
  112. delAllFile(path "/" tempList[i]);//先删除文件夹里面的文件
  113. delFolder(path "/" tempList[i]);//再删除空文件夹
  114. }
  115. }
  116. }
  117. /**
  118. * 复制单个文件
  119. * @param oldPath String 原文件路径 如:c:/fqf.txt
  120. * @param newPath String 复制后路径 如:f:/fqf.txt
  121. * @return boolean
  122. */
  123. public void copyFile(String oldPath, String newPath) {
  124. try {
  125. int bytesum = 0;
  126. int byteread = 0;
  127. File oldfile = new File(oldPath);
  128. if (oldfile.exists()) { //文件存在时
  129. InputStream inStream = new FileInputStream(oldPath); //读入原文件
  130. FileOutputStream fs = new FileOutputStream(newPath);
  131. byte[] buffer = new byte[1444];
  132. int length;
  133. while ( (byteread = inStream.read(buffer)) != -1) {
  134. bytesum = byteread; //字节数 文件大小
  135. System.out.println(bytesum);
  136. fs.write(buffer, 0, byteread);
  137. }
  138. inStream.close();
  139. }
  140. }
  141. catch (Exception e) {
  142. System.out.println("复制单个文件操作出错");