常用 java File 操作类

业务常用,供大家学习:

引用地址:http://www.easydone.cn/014/200604022353065155.htm

package org.easydone.file;

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 {


public static boolean createFolder(String folderPath) throws IOException{
boolean result = false;
File f = new File(folderPath);
result = f.mkdirs();
return result;
}

public void emptyDirectory(File directory) {
File[] entries = directory.listFiles();
for (int i = 0; i < entries.length; i++) {
entries[i].delete();
}
}



public static boolean makeFile(String filepath) throws IOException{
boolean result = false;
File file = new File(filepath);
result = file.createNewFile();
file = null;
return result;
}

public static boolean isDel(String filepath){
boolean result = false;
File file = new File(filepath);
result = file.delete();
file = null;
return result;
}

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;
}

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();
}

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;
}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值