Java custom file operation instance code

Source: Internet
Author: User

  1. Package cn.edu. tongji. cims. wade. system;
  2. Import java. io .*;
  3. Public class FileOperate {
  4. Public FileOperate (){
  5. }
  6. /**
  7. * Create a directory
  8. * @ Param folderPath String such as 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 ("Directory creation error ");
  22. E. printStackTrace ();
  23. }
  24. }
  25. /**
  26. * Create a file
  27. * @ Param filePathAndName String file path and name such as c:/fqf.txt
  28. * @ Param fileContent String File Content
  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 ("Directory creation error ");
  47. E. printStackTrace ();
  48. }
  49. }
  50. /**
  51. * Delete an object
  52. * @ Param filePathAndName String file path and name such as 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 ("File Deletion error ");
  65. E. printStackTrace ();
  66. }
  67. }
  68. /**
  69. * Delete A folder
  70. * @ Param filePathAndName String folder path and name such as c:/fqf
  71. * @ Param fileContent String
  72. * @ Return boolean
  73. */
  74. Public void delFolder (String folderPath ){
  75. Try {
  76. DelAllFile (folderPath); // delete all contents
  77. String filePath = folderPath;
  78. FilePath = filePath. toString ();
  79. Java. io. File myFilePath = new java. io. File (filePath );
  80. MyFilePath. delete (); // delete an empty folder
  81. }
  82. Catch (Exception e ){
  83. System. out. println ("Folder deletion error ");
  84. E. printStackTrace ();
  85. }
  86. }
  87. /**
  88. * Delete all files in the folder
  89. * @ Param path String folder path such as 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]); // delete the files in the folder first
  113. DelFolder (path "/" tempList [I]); // Delete the empty folder.
  114. }
  115. }
  116. }
  117. /**
  118. * Copy a single file
  119. * @ Param oldPath String original file path, for example, c:/fqf.txt
  120. * @ Param newPath: the copied String path is 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 () {// when the file exists
  129. InputStream inStream = new FileInputStream (oldPath); // read the original file
  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; // file size in bytes
  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 ("An error occurred while copying a single file ");
  143. E. printStackTrace ();
  144. }
  145. }
  146. /**
  147. * Copy the entire folder.
  148. * @ Param oldPath String original file path, for example, c:/fqf
  149. * @ Param newPath: path after String copying, for example, f:/fqf/ff.
  150. * @ Return boolean
  151. */
  152. Public void copyFolder (String oldPath, String newPath ){
  153. Try {
  154. (New File (newPath). mkdirs (); // create a new folder if the folder does not exist
  155. File a = new File (oldPath );
  156. String [] file = a. list ();
  157. File temp = null;
  158. For (int I = 0; I <file. length; I ){
  159. If (oldPath. endsWith (File. separator )){
  160. Temp = new File (oldPath file [I]);
  161. }
  162. Else {
  163. Temp = new File (oldPath File. separator file [I]);
  164. }
  165. If (temp. isFile ()){
  166. FileInputStream input = new FileInputStream (temp );
  167. FileOutputStream output = new FileOutputStream (newPath "/"
  168. (Temp. getName (). toString ());
  169. Byte [] B = new byte [1, 1024*5];
  170. Int len;
  171. While (len = input. read (B ))! =-1 ){
  172. Output. write (B, 0, len );
  173. }
  174. Output. flush ();
  175. Output. close ();
  176. Input. close ();
  177. }
  178. If (temp. isDirectory () {// if it is a subfolder
  179. CopyFolder (oldPath "/" file [I], newPath "/" file [I]);
  180. }
  181. }
  182. }
  183. Catch (Exception e ){
  184. System. out. println ("An error occurred while copying the entire folder ");
  185. E. printStackTrace ();
  186. }
  187. }
  188. /**
  189. * Move a file to a specified directory
  190. * @ Param oldPath String such as: c:/fqf.txt
  191. * @ Param newPath String such as: d:/fqf.txt
  192. */
  193. Public void moveFile (String oldPath, String newPath ){
  194. CopyFile (oldPath, newPath );
  195. DelFile (oldPath );
  196. }
  197. /**
  198. * Move a file to a specified directory
  199. * @ Param oldPath String such as: c:/fqf.txt
  200. * @ Param newPath String such as: d:/fqf.txt
  201. */
  202. Public void moveFolder (String oldPath, String newPath ){
  203. CopyFolder (oldPath, newPath );
  204. DelFolder (oldPath );
  205. }
  206. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.