标签:
package com.mzq.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
/**
*
* <br>
* <b>文件操作总结</b></br>
*
* <pre>
* 直接利用java的底层编写
* </pre>
*
* @author moziqi 2014-7-27
*/
public final class FileUtil {
/**
* 创建文件夹
*
* @param folderName
* @return
*/
public static boolean createFolder(String folderName) {
flag = false;
File file = new File(folderName);
if (!file.exists()) {
flag = file.mkdir();
}
return flag;
}
/**
* 创建文件
*
* @param fileName
* @param content
* @return
*/
public static boolean createFile(String fileName, String content) {
File file = new File(fileName);
flag = true;
try {
if (!file.exists()) {
file.createNewFile();
}
FileWriter resultFile = new FileWriter(file);
PrintWriter myFile = new PrintWriter(resultFile);
myFile.println(content);
resultFile.close();
} catch (IOException e) {
System.out.println("新建文件操作出错");
flag = false;
e.printStackTrace();
}
return flag;
}
/**
* 删除文件
*
* @param fileName
* @return
*/
public static boolean deleteFile(String fileName) {
File file = new File(fileName);
flag = false;
// 路径为文件且不为空则进行删除
if (file.isFile() && file.exists()) {
flag = file.delete();
}
return flag;
}
/**
* 删除文件夹
*
* @param directory
* @return
*/
public static boolean deleteDirectory(String directory) {
// 如果directory不以文件分隔符结尾,自动添加文件分隔符
if (!directory.endsWith(File.separator)) {
directory = directory + File.separator;
}
File dirFile = new File(directory);
// 如果dir对应的文件不存在,或者不是一个目录,则退出
if (!dirFile.exists() || !dirFile.isDirectory()) {
return false;
}
flag = true;
// 删除文件夹下的所有文件(包括子目录)
File[] files = dirFile.listFiles();
for (int i = 0; i < files.length; i++) {
// 删除子文件
if (files[i].isFile()) {
flag = deleteFile(files[i].getAbsolutePath());
if (!flag)
break;
} // 删除子目录
else {
flag = deleteDirectory(files[i].getAbsolutePath());
if (!flag)
break;
}
}
if (!flag)
return false;
// 删除当前目录
if (dirFile.delete()) {
return true;
} else {
return false;
}
}
/**
* 文件删除操作
*
* @param fileName
* @return
*/
public static boolean DeleteFolder(String fileName) {
flag = false;
File file = new File(fileName);
// 判断目录或文件是否存在
if (!file.exists()) { // 不存在返回 false
return flag;
} else {
// 判断是否为文件
if (file.isFile()) { // 为文件时调用删除文件方法
return deleteFile(fileName);
} else { // 为目录时调用删除目录方法
return deleteDirectory(fileName);
}
}
}
/**
* 复制文件
*
* @param src
* @param dest
* @throws IOException
*/
public static void copyFile(String src, String dest) throws IOException {
FileInputStream in = new FileInputStream(src);
File file = new File(dest);
if (!file.exists())
file.createNewFile();
FileOutputStream out = new FileOutputStream(file);
int c;
byte buffer[] = new byte[1024];
while ((c = in.read(buffer)) != -1) {
for (int i = 0; i < c; i++)
out.write(buffer[i]);
}
in.close();
out.close();
}
/**
* 转移文件目录
*
* @param filename
* @param oldpath
* @param newpath
* @param cover
*/
public static void changeDirectory(String filename, String oldpath,
String newpath, boolean cover) {
if (!oldpath.equals(newpath)) {
File oldfile = new File(oldpath + "/" + filename);
File newfile = new File(newpath + "/" + filename);
if (newfile.exists()) {// 若在待转移目录下,已经存在待转移文件
if (cover)// 覆盖
oldfile.renameTo(newfile);
else
System.out.println("在新目录下已经存在:" + filename);
} else {
oldfile.renameTo(newfile);
}
}
}
private static Boolean flag = null;
}
参考链接如下:
http://www.cnblogs.com/springcsc/archive/2009/12/03/1616367.html
http://www.cnblogs.com/zhuocheng/archive/2011/12/12/2285290.html
标签:
原文地址:http://my.oschina.net/moziqi/blog/295214