自己写的文件操作工具类
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; } }
相关文章推荐
- 分享一个自己利用学的IO流操作封装了一个小工具进行文件复制剪切和删除功能
- 【C语言】没事可以试试这个小程序,使用文件操作,模拟实现一个简单的文件拷贝工具!
- Linux下一些文件操作和一些简单命令的自己实现
- 自己写的文件统计工具
- Java文件IO操作工具类(复制、删除、重命名、创建路径),复制代码并创建FileControl类即可使用。
- java 自己写的一个自动分析对象的mysql数据库操作工具
- ( 转载)纯Java文件操作工具,支持文件、文件夹的复制、删除、移动
- Java文件操作增强工具
- 自己写的一个将指定目录下的文本文件集合成单独的一个大文本文件工具
- Java文件操作工具
- Python 小工具--实现获取文件夹下面所有文件操作
- Android文件操作工具类
- 通过EverEdit工具连接Linux系统远程操作文件
- 自己的两台电脑,在局域网中,可以快速共享两个电脑的文件,可以很方便操作。
- 自己练习的文件操作小程序
- C# 利用自带xsd.exe工具操作XML-如通过XML生成xsd文件
- Linux有时候执行了 rm -rf 等操作误删了文件绝对是一件可怕的事情,好在有一些解决的办法可以临时救急。这时我们就要用到一款叫做extundelete的工具了。
- Oracle备份工具、文件命名格式、rman操作
- 数据字典生成工具之旅(3):PowerDesign文件组成结构介绍及操作
- Java文件操作增强工具