java基础之文件操作

超、凢脫俗 2021-09-28 02:48 377阅读 0赞

文件操作工具类(关于文件创建、写入、删除)

  1. package util;
  2. import java.io.BufferedReader;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.InputStreamReader;
  8. import java.io.PrintWriter;
  9. /**
  10. * 文件操作工具类(关于文件创建、写入、删除)
  11. *
  12. * @ClassName: FileUtil
  13. * @Description:
  14. * @author wxy
  15. * @date 2018年3月15日 下午3:03:54
  16. *
  17. */
  18. public class FileUtil {
  19. //生成文件路径
  20. private static String filePath = "G:\\WEBCOPY\\";
  21. //文件路径+名称
  22. private static String filePathAndName;
  23. /**
  24. * 创建文件
  25. *
  26. * @Title: createFile
  27. * @Description:
  28. * @param @param fileName 文件名称
  29. * @param @param FileContent 文件内容
  30. * @param @param fileType 文件类型
  31. * @param @return 是否创建成功,成功则返回true
  32. * @return boolean
  33. * @throws
  34. */
  35. public static boolean createFile(String fileName,String fileContent,String fileType){
  36. Boolean bool = false;
  37. filePathAndName = filePath+fileName+"."+fileType;
  38. File file = new File(filePathAndName);
  39. try {
  40. //如果文件不存在,则创建新的文件
  41. if(!file.exists()){
  42. file.createNewFile();
  43. bool = true;
  44. System.err.println("success to create file:"+filePathAndName);
  45. //文件创建成功后,向文件写入数据
  46. writeFileContent(filePathAndName,fileContent);
  47. }
  48. } catch (Exception e) {
  49. e.printStackTrace();
  50. }
  51. return bool;
  52. }
  53. /**
  54. * 向文件写入内容
  55. *
  56. * @Title: writeFileContent
  57. * @Description:
  58. * @param @param filePath
  59. * @param @param fileContent
  60. * @param @return
  61. * @return boolean
  62. * @throws
  63. */
  64. public static boolean writeFileContent(String filePath,String fileContent){
  65. Boolean bool = false;
  66. String filein = fileContent+"\r\n";//新写入的行,换行
  67. String temp = "";
  68. FileInputStream fis = null;
  69. InputStreamReader isr = null;
  70. BufferedReader br = null;
  71. FileOutputStream fos = null;
  72. PrintWriter pw = null;
  73. try {
  74. File file = new File(filePath);//文件路径(包括文件名称)
  75. //将文件读入输入流
  76. fis = new FileInputStream(file);
  77. isr = new InputStreamReader(fis);
  78. br = new BufferedReader(isr);
  79. StringBuffer buffer = new StringBuffer();
  80. //文件原有内容
  81. for(int i=0;(temp=br.readLine())!=null;i++){
  82. buffer.append(temp);
  83. //行与行之间的分隔符相当于"\n"
  84. buffer = buffer.append(System.getProperty("line.separator"));
  85. }
  86. buffer.append(filein);
  87. fos = new FileOutputStream(file);
  88. pw = new PrintWriter(fos);
  89. pw.write(buffer.toString().toCharArray());
  90. pw.flush();
  91. bool = true;
  92. } catch (Exception e) {
  93. e.printStackTrace();
  94. }
  95. try {
  96. if(pw!=null){
  97. pw.close();
  98. }
  99. if(fos!=null){
  100. fos.close();
  101. }
  102. if(br!=null){
  103. br.close();
  104. }
  105. if(isr!=null){
  106. isr.close();
  107. }
  108. if(fis!=null){
  109. fis.close();
  110. }
  111. } catch (Exception e) {
  112. }
  113. return bool;
  114. }
  115. /**
  116. * 删除文件
  117. *
  118. * @Title: delFile
  119. * @Description:
  120. * @param @param filePath 文件路径+名称+文件类型
  121. * @param @return
  122. * @return boolean
  123. * @throws
  124. */
  125. public static boolean delFile(String filePath){
  126. Boolean bool = false;
  127. File file = new File(filePath);
  128. try {
  129. if(file.exists()){
  130. file.delete();
  131. bool = true;
  132. }
  133. } catch (Exception e) {
  134. e.printStackTrace();
  135. }
  136. return bool;
  137. }
  138. }

发表评论

表情:
评论列表 (有 0 条评论,377人围观)

还没有评论,来说两句吧...

相关阅读

    相关 python基础文件操作

    文件操作有很多种   我们在这里可以大体分一下,文件的操作其实可以分为对文件整体的操作(创建文件,删除文件,重命名文件,获取文件属性)以及对文件内容的操作(修改文件内容)