您的位置:首页 > 其它

自己写的文件操作工具类

2011-11-25 10:09 246 查看
该工具类包括新建文件、新建文件夹、根据文件名删除文件或文件夹、删除文件夹下所有对象、在文件末尾追加写入等等操作

import java.io.*;

/**
* 文件操作工具类
* @author lifeng 2011-11-18 下午01:23:46
*/
public class FileOperator {

public FileOperator() {
}

/**
* 如果文件不存在,则在指定目录下创建
* @author lifeng
* @param parent 父路径文件对象
* @param fileName 文件名
* @return 返回需要的文件对象
* @throws IOException
*/
public static File createFileIfNotExist(File parent, String fileName)
throws IOException {
File result = null;
try {
if (!parent.exists())
parent.mkdirs();
result = new File((new StringBuilder(String.valueOf(parent
.getAbsolutePath()))).append(File.separator)
.append(fileName).toString());
if (!result.exists())
result.createNewFile();
} catch (IOException e) {
throw new IOException((new StringBuilder(
"\u65B0\u5EFA\u6587\u4EF6["))
.append(result.getAbsolutePath()).append("]\u51FA\u9519!")
.toString());
}
return result;
}

/**
* 如果文件夹不存在的话,在指定目录下创建文件夹
* @author lifeng
* @param parent 父路径文件对象
* @param folderName 文件夹名
* @return 返回所需文件对象
*/
public static File createFolderIfNotExist(File parent, String folderName) {
File result = null;
if (!parent.exists())
parent.mkdirs();
result = new File((new StringBuilder(String.valueOf(parent
.getAbsolutePath()))).append(File.separator).append(folderName)
.toString());
if (!result.exists())
result.mkdir();
return result;
}

/**
* 在制定文件的末端写入字符数据
* @author lifeng
* @param filePath 文件全路径
* @param fileData 字符串数据
* @throws IOException
*/
public static void writeIntoFile(String filePath, String fileData)
throws IOException {
try {
RandomAccessFile raf = new RandomAccessFile(filePath, "rw");
long fileLentgh = raf.length();
raf.seek(fileLentgh);
raf.writeBytes(fileData);
raf.close();
} catch (FileNotFoundException e) {
throw new FileNotFoundException(
(new StringBuilder(
"\u8FFD\u52A0\u5199\u5165-\u627E\u4E0D\u5230\u6587\u4EF6["))
.append(filePath).append("]!").toString());
} catch (IOException e) {
throw new IOException((new StringBuilder("\u5BF9\u6587\u4EF6["))
.append(filePath)
.append("]\u8FFD\u52A0\u5199\u5165\u51FA\u9519!")
.toString());
}
}

/**
* 将文件复制到指定目录下
* @author lifeng
* @param srcFile 源文件
* @param destPath 目标路径
* @throws IOException
*/
public static void moveFile(File srcFile, String destPath)
throws IOException {
byte b[] = new byte[1024];
int n = 0;
try {
File destFolder = new File(destPath);
if (!destFolder.exists())
destFolder.mkdirs();
File fo = new File(destPath, srcFile.getName());
fo.createNewFile();
FileInputStream fis = new FileInputStream(srcFile);
FileOutputStream fos = new FileOutputStream(fo);
while ((n = fis.read(b)) != -1)
fos.write(b, 0, n);
fis.close();
fos.close();
} catch (FileNotFoundException fnfe) {
throw new FileNotFoundException(
"\u590D\u5236\u79FB\u52A8-\u6587\u4EF6\u672A\u627E\u5230!");
} catch (IOException e) {
throw new IOException(
"\u590D\u5236\u79FB\u52A8-\u6587\u4EF6\u64CD\u4F5C\u5931\u8D25!");
}
}

/**
* 从jar包中获取文件对象
* @author lifeng
* @param filePath 所处路径,若为res/a.jar,则表示项目路径为:src/res/a.jar
* @return
*/
public static File getFile(String filePath) {
String plguinUrl = Thread.currentThread().getContextClassLoader()
.getResource(filePath).getPath();
File pluginFile = new File(plguinUrl);
return pluginFile;
}

/**
* 复制移动完整的文件目录,包括文件夹下的所有内容
* @author lifeng
* @param oldPath
* @param newPath
*/
public static void moveWholeFile(File srcFile, String newPath) {
try {
(new File(newPath)).mkdirs(); // 如果文件夹不存在 则建立新文件夹

String oldPath = srcFile.getAbsolutePath();
String[] file = srcFile.list();
File temp = srcFile;
for (int i = 0; i < file.length; i++) {
if (oldPath.endsWith(File.separator)) {
temp = new File(oldPath + file[i]);
} else {
temp = new File(oldPath + File.separator + file[i]);
}
if (temp.isFile()) {
FileInputStream input = new FileInputStream(temp);
FileOutputStream output = new FileOutputStream(newPath
+ "/" + (temp.getName()).toString());
byte[] b = new byte[1024 * 5];
int len;
while ((len = input.read(b)) != -1) {
output.write(b, 0, len);
}
output.flush();
output.close();
input.close();
}
if (temp.isDirectory()) { // 如果是子文件夹
moveWholeFile(new File(srcFile.getAbsoluteFile() + "/"
+ file[i]), newPath + "/" + file[i]);
}
}
} catch (Exception e) {
System.out.println("复制整个文件夹内容操作出错");
e.printStackTrace();
}
}

/**
* 删除所有path目录下的名为fileName的文件(flag:1表示删除文件,2表示删除文件夹)
* @parm path 文件路径
* @parm fileName 所要删除的文件名
* @parm flag 删除标志,1:删除文件,2:删除文件夹
* @author lifeng 2011-11-18
*/
public static int deleteFiles(String path, String fileName, int operateNum ,int flag) {
File pathFile = new File(path);
File[] files = pathFile.listFiles();
int tempNum = operateNum;
for (int i = 0; i < files.length; i++) {
File temp = files[i];
if (temp.isDirectory() && flag==2) {
if (temp.getName().equals(fileName)) {
delFolder(temp.getAbsolutePath(),operateNum);
tempNum++;
} else {
deleteFiles(temp.getAbsolutePath(),fileName,tempNum,flag);
}
} else if (temp.isFile() && flag==1){
if (temp.getName().equals(fileName)) {
temp.delete();
tempNum++;
}
}
}
return tempNum;
}

/**
* 删除文件夹及其下所有文件
* @author lifeng
* @param folderPath
*/
public static void delFolder(String folderPath,int operateNum) {
try {
delAllFile(folderPath,operateNum); // 删除完里面所有内容
String filePath = folderPath;
filePath = filePath.toString();
File myFilePath = new File(filePath);
myFilePath.delete(); // 删除空文件夹
operateNum++;
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* 删除指定文件夹下所有文件
* @author lifeng
* @param path 文件夹完整绝对路径
* @return
*/
public static boolean delAllFile(String path,int operateNum) {
boolean flag = false;
File file = new File(path);
if (!file.exists()) {
return flag;
}
if (!file.isDirectory()) {
return flag;
}
String[] tempList = file.list();
File temp = null;
for (int i = 0; i < tempList.length; i++) {
if (path.endsWith(File.separator)) {
temp = new File(path + tempList[i]);
} else {
temp = new File(path + File.separator + tempList[i]);
}
if (temp.isFile()) {
temp.delete();
operateNum++;
}
if (temp.isDirectory()) {
delAllFile(path + "/" + tempList[i],operateNum);// 先删除文件夹里面的文件
delFolder(path + "/" + tempList[i],operateNum);// 再删除空文件夹
flag = true;
}
}
return flag;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐