---
title: "常用 java File 操作类"
author: "xpz"
date: "2007-11-13T09:17:09Z"
tags: []
slug: "java-file"
draft: false
lastmod: "2007-11-13T09:17:09Z"
id: 140100
url: "https://xpz.iteye.com/blog/140100"
---

java 代码

001. package org.easydone.file;
002. import java.io.File;
003. import java.io.FileWriter;
004. import java.io.IOException;
005. import java.io.PrintWriter;
006. import java.text.SimpleDateFormat;
007. import java.util.Date;
008. /\**
009. * &lt;p&gt;Title: File 常用操作(部分)&lt;/p&gt;
010. * &lt;p&gt;Description: 业务用&lt;/p&gt;
011. * &lt;p&gt;Copyright: Copyright (c) 2006 www.easydone.cn&lt;/p&gt;
012. * &lt;p&gt;Company: 北京聚能易成科技有限公司&lt;/p&gt;
013. * @authory dirboy
014. * @version 1.0
015. \*/
016. public class FileOperate {
017. /\**
018. * 创建目录
019. * @param folderPath:目录路径
020. * @return
021. * @throws IOException
022. \*/
023. public static boolean createFolder(String folderPath) throws IOException{
024. boolean result = false;
025. File f = new File(folderPath);
026. result = f.mkdirs();
027. return result;
028. }
029. /\**
030. * 删除目录下所有文件
031. * @param directory (File 对象)
032. \*/
033. public void emptyDirectory(File directory) {
034. File\[] entries = directory.listFiles();
035. for (int i = 0; i &lt; entries.length; i++) {
036. entries\[i].delete();
037. }
038. }
039. /\**
040. * 创建文件
041. * @param filepath:文件所在目录路径,比如:c:/test/test.txt
042. * @return
043. \*/
044. public static boolean makeFile(String filepath) throws IOException{
045. boolean result = false;
046. File file = new File(filepath);
047. result = file.createNewFile();
048. file = null;
049. return result;
050. }
051. /\**
052. * 删除文件
053. * @param filepath:文件所在物理路径
054. * @return
055. \*/
056. public static boolean isDel(String filepath){
057. boolean result = false;
058. File file = new File(filepath);
059. result = file.delete();
060. file = null;
061. return result;
062. }
063. /\**
064. * 文件重命名
065. * @param filepath:文件所在物理路径
066. * @param destname:新文件名
067. * @return
068. \*/
069. public static boolean renamefile(String filepath,String destname){
070. boolean result = false;
071. File f = new File(filepath);
072. String fileParent = f.getParent();
073. String filename = f.getName();
074. File rf = new File(fileParent+"//"+destname);
075. if(f.renameTo(rf)){
076. result = true;
077. }
078. f = null;
079. rf = null;
080. return result;
081. }
082. /\**
083. * 将文件内容写入数据库中
084. * @param filepath:文件所在物理路径
085. * @param content:写入内容
086. * @throws Exception
087. \*/
088. public static void WriteFile(String filepath,String content) throws Exception {
089. FileWriter filewriter = new FileWriter(filepath,true);//写入多行
090. PrintWriter printwriter = new PrintWriter(filewriter);
091. printwriter.println(content);
092. printwriter.flush();
093. printwriter.close();
094. filewriter.close();
095. }
096. /\**
097. * 日志备份
098. * @param filePath:日志备份路径
099. * @param baksize:日志备份大小参考值(字节大小)
100. * @throws IOException
101. \*/
102. public static void logBak(String filePath,long baksize) throws IOException{
103. File f = new File(filePath);
104. long len = f.length();
105. SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyyMMddHHmmss");
106. String s = simpledateformat.format(new Date());
107. String fileName = f.getName();
108. int dot = fileName.indexOf(".");
109. String bakName = s+fileName.substring(dot);
110. System.out.println(bakName);
111. if(len&gt;=baksize){
112. renamefile(filePath,bakName);
113. makeFile(filePath);
114. }
115. f = null;
116. }
117. }