您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

Java 文件操作工具类

2024/6/16 21:57:00发布25次查看
文件及其文件夹的复制,删除,拷贝等操作类
package util; import java.io.file; import java.io.fileinputstream; import java.io.fileoutputstream; import java.io.filenamefilter; public class fileutil { private final static string file_suffix = .java.drl; private final static string file_temp = c:/temp/; /** * 将已存在的drl文件删除 * * @param objectpath */ public static void deleteexisteddrlfile(string objectpath) { file filepath = new file(objectpath); if (!filepath.exists()) { system.out.println(目录不存在!); } else { if (filepath.isdirectory()) { file[] list = filepath.listfiles(new filenamefilter() { public boolean accept(file dir, string name) { return name.endswith(file_suffix); } }); for (int i = 0; i < list.length; i++) { list[i].delete(); } } } } /** * 创建文件夹,如果有则不创建 * * @param objectpath */ public static boolean crearedirectory(string objectpath) { boolean flag = true; file filepath = new file(objectpath); if (!filepath.exists()) { filepath.mkdir(); flag = false; } return flag; } /** * 查看某文件夹下面是否有文件,有文件则创建一个temp文件夹,将文件拷贝到temp目录下(备份文件) 没有文件怎什么都不做 * 备份后,把原文件夹里文件删除 * * @param objectpath */ public static void backupfile(string objectpath, string dirname) { string backuppath; if (!file_temp.endswith(file.separator)) { backuppath = file_temp + file.separator + dirname; } else { backuppath = file_temp + dirname; } file backupfilepath = new file(backuppath); if (!backupfilepath.exists()) { backupfilepath.mkdirs(); } file filepath = new file(objectpath); if (!filepath.exists()) { system.out.println(目录不存在!); } else { if (filepath.isdirectory()) { file[] list = filepath.listfiles(); if (list != null && list.length != 0) { copyfolder(objectpath, backuppath);// 文件备份 for (int i = 0; i < list.length; i++) { list[i].delete(); } } } } } /** * 复原文件,把文件从备份文件夹拷贝到原来文件夹 * * @param objectpath * @param dirname */ public static void recoverfile(string objectpath, string dirname) { string backuppath; if (objectpath.endswith(file.separator)) { objectpath = new stringbuffer(objectpath).append(dirname) .tostring(); } else { objectpath = new stringbuffer(objectpath) .append(file.separatorchar).append(dirname).tostring(); } if (!file_temp.endswith(file.separator)) { backuppath = file_temp + file.separator + dirname; } else { backuppath = file_temp + dirname; } file backupfilepath = new file(backuppath); if (!backupfilepath.exists()) { backupfilepath.mkdirs(); } file filepath = new file(objectpath); if (!filepath.exists()) { system.out.println(目录不存在!); } else { if (filepath.isdirectory()) { file[] list = filepath.listfiles(); if (list != null && list.length != 0) { copyfolder(backuppath, objectpath);// 文件复原 } } } } /** * 复制整个文件夹内容 * * @param oldpath * string 原文件路径 如:c:/fqf * @param newpath * string 复制后路径 如:f:/fqf/ff * @return boolean */ public static void copyfolder(string oldpath, string newpath) { try { (new file(newpath)).mkdir(); // 如果文件夹不存在 则建立新文件夹 file a = new file(oldpath); string[] file = a.list(); file temp = null; 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()) {// 如果是子文件夹 copyfolder(oldpath + / + file[i], newpath + / + file[i]); } } } catch (exception e) { system.out.println(复制整个文件夹内容操作出错); e.printstacktrace(); } } /** * 删除备份文件和存放备份文件的文件夹 * * @param objectpath */ public static void deletefileanddirectory(string dirname) { string objectpath; if (!file_temp.endswith(file.separator)) { objectpath = file_temp + file.separator + dirname; } else { objectpath = file_temp + dirname; } file filepath = new file(objectpath); if (!filepath.exists()) { filepath.mkdirs(); system.out.println(目录不存在!); } else { if (filepath.isdirectory()) { file[] list = filepath.listfiles(); for (int i = 0; i < list.length; i++) { list[i].delete(); } } filepath.delete(); } } /** * 判断某文件夹下是否存在文件,存在返回true * * @param objectpath * @return */ public static boolean existfileindirectory(string objectpath) { boolean flag = false; file filepath = new file(objectpath); if (filepath.exists()) { if (filepath.isdirectory()) { file[] list = filepath.listfiles(); if (list != null && list.length != 0) { flag = true; } } } return flag; } /** * 删除某个文件夹 * @param objectpath */ public static void deletedirectory(string objectpath) { file filepath = new file(objectpath); if (filepath.exists()) { filepath.delete(); } } /** * 将已存在的文件删除 * * @param objectpath */ public static boolean deleteexistedfile(string objectpath,final string filename) { boolean flag =false; file filepath = new file(objectpath); if (filepath.exists()) { if (filepath.isdirectory()) { file[] list = filepath.listfiles(new filenamefilter() { public boolean accept(file dir, string name) { return name.equals(filename); } }); for (int i = 0; i < list.length; i++) { list[i].delete(); } flag=true; } } return flag; } }
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product