ITeye博客

常用 java File 操作类

xpz · 2007-11-13 09:17 · java · Markdown

java 代码

  1. package org.easydone.file;
  2. import java.io.File;
  3. import java.io.FileWriter;
  4. import java.io.IOException;
  5. import java.io.PrintWriter;
  6. import java.text.SimpleDateFormat;
  7. import java.util.Date;
  8. /**
  9. * <p>Title: File 常用操作(部分)</p>
  10. * <p>Description: 业务用</p>
  11. * <p>Copyright: Copyright (c) 2006 www.easydone.cn</p>
  12. * <p>Company: 北京聚能易成科技有限公司</p>
  13. * @authory dirboy
  14. * @version 1.0
  15. */
  16. public class FileOperate {
  17. /**
  18. * 创建目录
  19. * @param folderPath:目录路径
  20. * @return
  21. * @throws IOException
  22. */
  23. public static boolean createFolder(String folderPath) throws IOException{
  24. boolean result = false;
  25. File f = new File(folderPath);
  26. result = f.mkdirs();
  27. return result;
  28. }
  29. /**
  30. * 删除目录下所有文件
  31. * @param directory (File 对象)
  32. */
  33. public void emptyDirectory(File directory) {
  34. File[] entries = directory.listFiles();
  35. for (int i = 0; i < entries.length; i++) {
  36. entries[i].delete();
  37. }
  38. }
  39. /**
  40. * 创建文件
  41. * @param filepath:文件所在目录路径,比如:c:/test/test.txt
  42. * @return
  43. */
  44. public static boolean makeFile(String filepath) throws IOException{
  45. boolean result = false;
  46. File file = new File(filepath);
  47. result = file.createNewFile();
  48. file = null;
  49. return result;
  50. }
  51. /**
  52. * 删除文件
  53. * @param filepath:文件所在物理路径
  54. * @return
  55. */
  56. public static boolean isDel(String filepath){
  57. boolean result = false;
  58. File file = new File(filepath);
  59. result = file.delete();
  60. file = null;
  61. return result;
  62. }
  63. /**
  64. * 文件重命名
  65. * @param filepath:文件所在物理路径
  66. * @param destname:新文件名
  67. * @return
  68. */
  69. public static boolean renamefile(String filepath,String destname){
  70. boolean result = false;
  71. File f = new File(filepath);
  72. String fileParent = f.getParent();
  73. String filename = f.getName();
  74. File rf = new File(fileParent+"//"+destname);
  75. if(f.renameTo(rf)){
  76. result = true;
  77. }
  78. f = null;
  79. rf = null;
  80. return result;
  81. }
  82. /**
  83. * 将文件内容写入数据库中
  84. * @param filepath:文件所在物理路径
  85. * @param content:写入内容
  86. * @throws Exception
  87. */
  88. public static void WriteFile(String filepath,String content) throws Exception {
  89. FileWriter filewriter = new FileWriter(filepath,true);//写入多行
  90. PrintWriter printwriter = new PrintWriter(filewriter);
  91. printwriter.println(content);
  92. printwriter.flush();
  93. printwriter.close();
  94. filewriter.close();
  95. }
  96. /**
  97. * 日志备份
  98. * @param filePath:日志备份路径
  99. * @param baksize:日志备份大小参考值(字节大小)
  100. * @throws IOException
  101. */
  102. public static void logBak(String filePath,long baksize) throws IOException{
  103. File f = new File(filePath);
  104. long len = f.length();
  105. SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyyMMddHHmmss");
  106. String s = simpledateformat.format(new Date());
  107. String fileName = f.getName();
  108. int dot = fileName.indexOf(".");
  109. String bakName = s+fileName.substring(dot);
  110. System.out.println(bakName);
  111. if(len>=baksize){
  112. renamefile(filePath,bakName);
  113. makeFile(filePath);
  114. }
  115. f = null;
  116. }
  117. }

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。