常用 java File 操作类
来源:互联网 发布:江苏网络作家协会 编辑:程序博客网 时间:2024/06/12 01:49
常用 java File 操作类
<script src="http://easydone.com.cn/ads/ads_google_250.asp" type="text/javascript"></script>
业务常用,供大家学习:
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;
}
}
- 常用 java File 操作类
- java File 常用操作
- Java篇-File类之常用操作
- Java File 常用操作回顾
- java:File常用基础操作
- java file类操作常用类的结构
- java File 类常用操作,和java 文件流常用操作演示
- Java 常用类File
- 常用文件操作类File
- java.io.file 的常用文件操作
- Java 中,File 类的一些常用操作比如过滤,遍历等操作
- Java File类各种操作
- java file操作工具类
- Java IO操作、File类
- java文件属性操作File类
- Java:文件操作类File
- java高级File类操作
- Java File操作工具类
- 失落
- 自己的more命令code
- linux 用户管理
- strncmp
- Poem
- 常用 java File 操作类
- COM接口 C语言
- [转]ARP地址解析协议详解及其漏洞分析[21世纪安全网络--21SaFe.CoM]
- C与C++接口实现比较
- ADO连接数据库字符串大全(VP,Excel,文本,Sybase,.NET等)
- [转]HTTPS安全超文本传输协议(Secure Hypertext Transfer Protocol)
- 软件需求分析 四点
- 在Emacs下用C/C++编程
- 提取峰值