本站首页    管理页面    写新日志    退出


天堂之光 人间希望

你我共同品味

JAVA的浓香.

Linux的清芬.

«August 2025»
12
3456789
10111213141516
17181920212223
24252627282930
31


我的分类(专题)

日志更新

最新评论

留言板

链接

联系我

msn:zhanglincon@hotmail.com


Blog信息
blog名称:
日志总数:99
评论数量:281
留言数量:4
访问次数:808592
建立时间:2005年11月17日




[java文件操作专题]删除目录和文件
原创空间,  软件技术

zhanglincon 发表于 2008/2/13 12:19:42

import java.io.File;import java.io.FileWriter;import java.io.IOException;import java.io.PrintWriter;import java.text.SimpleDateFormat;import java.util.Date;   public class FileOperate { /*** 创建目录* @param folderPath:目录路径* @return* @throws IOException*/public static boolean createFolder(String folderPath) throws IOException{boolean result = false;File f = new File(folderPath); if(!f.exists()){result = f.mkdirs();}return result;}/*** 删除目录下所有文件* @param directory (File 对象)*/public void emptyDirectory(File directory) {File[] entries = directory.listFiles();for (int i = 0; i < entries.length; i++) {entries[i].delete();}} /*** 创建文件* @param filepath:文件所在目录路径,比如:c:/test/test.txt* @return*/public static boolean makeFile(String filepath) throws IOException{boolean result = false;File file = new File(filepath);result = file.createNewFile();file = null;return result;}/*** 删除文件* @param filepath:文件所在物理路径* @return*/public static boolean isDel(String filepath){boolean result = false;File file = new File(filepath);result = file.delete();file = null;return result;}/*** 文件重命名* @param filepath:文件所在物理路径* @param destname:新文件名* @return*/public static boolean renamefile(String filepath,String destname){boolean result = false;File f = new File(filepath);String fileParent = f.getParent();String filename = f.getName();File rf = new File(fileParent+"//"+destname);if(f.renameTo(rf)){result = true;}f = null;rf = null;return result;}/*** 将文件内容写入数据库中* @param filepath:文件所在物理路径* @param content:写入内容* @throws Exception*/public static void WriteFile(String filepath,String content) throws Exception {FileWriter filewriter = new FileWriter(filepath,true);//写入多行PrintWriter printwriter = new PrintWriter(filewriter);printwriter.println(content);printwriter.flush();printwriter.close();filewriter.close();}/*** 日志备份* @param filePath:日志备份路径* @param baksize:日志备份大小参考值(字节大小)* @throws IOException*/public static void logBak(String filePath,long baksize) throws IOException{File f = new File(filePath);long len = f.length();SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyyMMddHHmmss");String s = simpledateformat.format(new Date());String fileName = f.getName();int dot = fileName.indexOf(".");String bakName = s+fileName.substring(dot);System.out.println(bakName);if(len>=baksize){renamefile(filePath,bakName);makeFile(filePath);}f = null;} }


阅读全文(3415) | 回复(0) | 编辑 | 精华
 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.203 second(s), page refreshed 144765123 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号