JAVA基础学习篇----FILE OPERATOR

JAVA文件操作相关:
  1. packagecom.bytecode.openexcel.util;
  2. importjava.io.*;
  3. publicclassFileOperate{
  4. publicFileOperate(){
  5. }
  6. /**
  7. *新建目录
  8. *
  9. *@paramfolderPath
  10. *String如c:/fqf
  11. *@returnboolean
  12. */
  13. publicvoidnewFolder(StringfolderPath){
  14. try{
  15. StringfilePath=folderPath;
  16. filePath=filePath.toString();
  17. java.io.FilemyFilePath=newjava.io.File(filePath);
  18. if(!myFilePath.exists()){
  19. myFilePath.mkdir();
  20. }
  21. }catch(Exceptione){
  22. System.out.println("新建目录操作出错");
  23. e.printStackTrace();
  24. }
  25. }
  26. /**
  27. *新建文件
  28. *
  29. *@paramfilePathAndName
  30. *String文件路径及名称如c:/fqf.txt
  31. *@paramfileContent
  32. *String文件内容
  33. *@returnboolean
  34. */
  35. publicvoidnewFile(StringfilePathAndName,StringfileContent){
  36. try{
  37. StringfilePath=filePathAndName;
  38. filePath=filePath.toString();
  39. FilemyFilePath=newFile(filePath);
  40. if(!myFilePath.exists()){
  41. myFilePath.createNewFile();
  42. }
  43. FileWriterresultFile=newFileWriter(myFilePath);
  44. PrintWritermyFile=newPrintWriter(resultFile);
  45. StringstrContent=fileContent;
  46. myFile.println(strContent);
  47. resultFile.close();
  48. }catch(Exceptione){
  49. System.out.println("新建文件操作出错");
  50. e.printStackTrace();
  51. }
  52. }
  53. /**
  54. *删除文件
  55. *
  56. *@paramfilePathAndName
  57. *String文件路径及名称如c:/fqf.txt
  58. *@paramfileContent
  59. *String
  60. *@returnboolean
  61. */
  62. publicvoiddelFile(StringfilePathAndName){
  63. try{
  64. StringfilePath=filePathAndName;
  65. filePath=filePath.toString();
  66. java.io.FilemyDelFile=newjava.io.File(filePath);
  67. myDelFile.delete();
  68. }catch(Exceptione){
  69. System.out.println("删除文件操作出错");
  70. e.printStackTrace();
  71. }
  72. }
  73. /**
  74. *删除文件夹
  75. *
  76. *@paramfilePathAndName
  77. *String文件夹路径及名称如c:/fqf
  78. *@paramfileContent
  79. *String
  80. *@returnboolean
  81. */
  82. publicvoiddelFolder(StringfolderPath){
  83. try{
  84. delAllFile(folderPath);//删除完里面所有内容
  85. StringfilePath=folderPath;
  86. filePath=filePath.toString();
  87. java.io.FilemyFilePath=newjava.io.File(filePath);
  88. myFilePath.delete();//删除空文件夹
  89. }catch(Exceptione){
  90. System.out.println("删除文件夹操作出错");
  91. e.printStackTrace();
  92. }
  93. }
  94. /**
  95. *删除文件夹里面的所有文件
  96. *
  97. *@parampath
  98. *String文件夹路径如c:/fqf
  99. */
  100. publicvoiddelAllFile(Stringpath){
  101. Filefile=newFile(path);
  102. if(!file.exists()){
  103. return;
  104. }
  105. if(!file.isDirectory()){
  106. return;
  107. }
  108. String[]tempList=file.list();
  109. Filetemp=null;
  110. for(inti=0;i<tempList.length;i++){
  111. if(path.endsWith(File.separator)){
  112. temp=newFile(path+tempList[i]);
  113. }else{
  114. temp=newFile(path+File.separator+tempList[i]);
  115. }
  116. if(temp.isFile()){
  117. temp.delete();
  118. }
  119. if(temp.isDirectory()){
  120. delAllFile(path+"/"+tempList[i]);//先删除文件夹里面的文件
  121. delFolder(path+"/"+tempList[i]);//再删除空文件夹
  122. }
  123. }
  124. }
  125. /**
  126. *复制单个文件
  127. *
  128. *@paramoldPath
  129. *String原文件路径如:c:/fqf.txt
  130. *@paramnewPath
  131. *String复制后路径如:f:/fqf.txt
  132. *@returnboolean
  133. */
  134. publicvoidcopyFile(StringoldPath,StringnewPath){
  135. try{
  136. intbytesum=0;
  137. intbyteread=0;
  138. Fileoldfile=newFile(oldPath);
  139. if(oldfile.exists()){//文件存在时
  140. InputStreaminStream=newFileInputStream(oldPath);//读入原文件
  141. FileOutputStreamfs=newFileOutputStream(newPath);
  142. byte[]buffer=newbyte[1444];
  143. intlength;
  144. while((byteread=inStream.read(buffer))!=-1){
  145. bytesum+=byteread;//字节数文件大小
  146. System.out.println(bytesum);
  147. fs.write(buffer,0,byteread);
  148. }
  149. inStream.close();
  150. }
  151. }catch(Exceptione){
  152. System.out.println("复制单个文件操作出错");
  153. e.printStackTrace();
  154. }
  155. }
  156. /**
  157. *复制整个文件夹内容
  158. *
  159. *@paramoldPath
  160. *String原文件路径如:c:/fqf
  161. *@paramnewPath
  162. *String复制后路径如:f:/fqf/ff
  163. *@returnboolean
  164. */
  165. publicvoidcopyFolder(StringoldPath,StringnewPath){
  166. try{
  167. (newFile(newPath)).mkdirs();//如果文件夹不存在则建立新文件夹
  168. Filea=newFile(oldPath);
  169. String[]file=a.list();
  170. Filetemp=null;
  171. for(inti=0;i<file.length;i++){
  172. if(oldPath.endsWith(File.separator)){
  173. temp=newFile(oldPath+file[i]);
  174. }else{
  175. temp=newFile(oldPath+File.separator+file[i]);
  176. }
  177. if(temp.isFile()){
  178. FileInputStreaminput=newFileInputStream(temp);
  179. FileOutputStreamoutput=newFileOutputStream(newPath
  180. +"/"+(temp.getName()).toString());
  181. byte[]b=newbyte[1024*5];
  182. intlen;
  183. while((len=input.read(b))!=-1){
  184. output.write(b,0,len);
  185. }
  186. output.flush();
  187. output.close();
  188. input.close();
  189. }
  190. if(temp.isDirectory()){//如果是子文件夹
  191. copyFolder(oldPath+"/"+file[i],newPath+"/"
  192. +file[i]);
  193. }
  194. }
  195. }catch(Exceptione){
  196. System.out.println("复制整个文件夹内容操作出错");
  197. e.printStackTrace();
  198. }
  199. }
  200. /**
  201. *移动文件到指定目录
  202. *
  203. *@paramoldPath
  204. *String如:c:/fqf.txt
  205. *@paramnewPath
  206. *String如:d:/fqf.txt
  207. */
  208. publicvoidmoveFile(StringoldPath,StringnewPath){
  209. copyFile(oldPath,newPath);
  210. delFile(oldPath);
  211. }
  212. /**
  213. *移动文件到指定目录
  214. *
  215. *@paramoldPath
  216. *String如:c:/fqf.txt
  217. *@paramnewPath
  218. *String如:d:/fqf.txt
  219. */
  220. publicvoidmoveFolder(StringoldPath,StringnewPath){
  221. copyFolder(oldPath,newPath);
  222. delFolder(oldPath);
  223. }
  224. }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值