关于文件操作的若干方法(File类)
来源:互联网 发布:centos jdk 环境变量 编辑:程序博客网 时间:2024/05/18 00:44
程序代码
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;
/**
* <p>Title: File 常用操作(部分)</p>
* <p>Description: 业务用</p>
* <p>Copyright: Copyright (c) 2006 www.easydone.cn</p>
* <p>Company: 北京聚能易成科技有限公司</p>
* @authory dirboy
* @version 1.0
*/
public class FileOperate {
/**
* 创建目录
* @param folderPath:目录路径
* @return
* @throws IOException
*/
public static boolean createFolder(String folderPath) throws IOException{
boolean result = false;
File f = new File(folderPath);
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;
}
}
- 关于文件操作的若干方法(File类)
- 关于js操作word和excel的若干方法汇总
- 关于js操作word和excel的若干方法汇总
- 关于JavaScript 操作word和excel的若干方法汇总
- 操作文件File类方法
- 操作文件File类方法
- Java关于File文件类操作总结
- File类文件的操作
- 操作文件的类-----File
- 关于链表的若干操作
- 关于强制类型转换的若干方法
- 关于EditText使用的若干参考方法
- 关于文本分类的若干方法
- 关于cookie的若干方法汇总
- 关于js操作word和excel,js导出word和excel的若干方法汇总[转]
- 关于js操作word和excel,js导出word和excel的若干方法汇总[转]
- File操作 - File类/方法
- 上传文件的若干种方法
- Lucene深入研究(2)
- 小试从数据库读取数据到文件
- java操作Excel、PDF文件
- prototype 用法
- 专门为PB,VB程序获取本机IP而制作的动态库
- 关于文件操作的若干方法(File类)
- [翻译+推荐]你需要知道的:WCF、WF、ADO.NET SyncServices和ClickOnce
- 毕业3年,月薪从5k到20k
- 利用uploadbean实现上传
- log4j日志使用详解
- 生成唯一的随机数
- Java设计模式-----Command模式
- resin配置错误
- 英文歌曲