常用 java File 操作类

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值