import org.apache.commons.io.FileUtils; import org.apache.commons.io.filefilter.*; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.io.*; /** * 文件工具箱 * * */ public final class FileToolkit { private static final Log log = LogFactory.getLog(FileToolkit.class); /** * 複製文件或者目錄,複製前後文件完全一樣。 * * @param resFilePath 源文件路徑 * @param distFolder 目標文件夾 * @IOException 當操作發生異常時拋出 */ public static void copyFile(String resFilePath, String distFolder) throws IOException { File resFile = new File(resFilePath); File distFile = new File(distFolder); if (resFile.isDirectory()) { FileUtils.copyDirectoryToDirectory(resFile, distFile); } else if (resFile.isFile()) { FileUtils.copyFileToDirectory(resFile, distFile, true); } } /** * 刪除一個文件或者目錄 * * @param targetPath 文件或者目錄路徑 * @IOException 當操作發生異常時拋出 */ public static void deleteFile(String targetPath) throws IOException { File targetFile = new File(targetPath); if (targetFile.isDirectory()) { FileUtils.deleteDirectory(targetFile); } else if (targetFile.isFile()) { targetFile.delete(); } } /** * 移動文件或者目錄,移動前後文件完全一樣,如果目標文件夾不存在則創建。 * * @param resFilePath 源文件路徑 * @param distFolder 目標文件夾 * @IOException 當操作發生異常時拋出 */ public static void moveFile(String resFilePath, String distFolder) throws IOException { File resFile = new File(resFilePath); File distFile = new File(distFolder); if (resFile.isDirectory()) { FileUtils.moveDirectoryToDirectory(resFile, distFile, true); } else if (resFile.isFile()) { FileUtils.moveFileToDirectory(resFile, distFile, true); } } /** * 重命名文件或文件夾 * * @param resFilePath 源文件路徑 * @param newFileName 重命名 * @return 操作成功標識 */ public static boolean renameFile(String resFilePath, String newFileName) { String newFilePath = StringToolkit.formatPath(StringToolkit.getParentPath(resFilePath) + "/" + newFileName); File resFile = new File(resFilePath); File newFile = new File(newFilePath); return resFile.renameTo(newFile); } /** * 讀取文件或者目錄的大小 * * @param distFilePath 目標文件或者文件夾 * @return 文件或者目錄的大小,如果獲取失敗,則返回-1 */ public static long genFileSize(String distFilePath) { File distFile = new File(distFilePath); if (distFile.isFile()) { return distFile.length(); } else if (distFile.isDirectory()) { return FileUtils.sizeOfDirectory(distFile); } return -1L; } /** * 判斷一個文件是否存在 * * @param filePath 文件路徑 * @return 存在返回true,否則返回false */ public static boolean isExist(String filePath) { return new File(filePath).exists(); } /** * 本地某個目錄下的文件列表(不遞歸) * * @param folder ftp上的某個目錄 * @param suffix 文件的後綴名(比如.mov.xml) * @return 文件名稱列表 */ public static String[] listFilebySuffix(String folder, String suffix) { IOFileFilter fileFilter1 = new SuffixFileFilter(suffix); IOFileFilter fileFilter2 = new NotFileFilter(DirectoryFileFilter.INSTANCE); FilenameFilter filenameFilter = new AndFileFilter(fileFilter1, fileFilter2); return new File(folder).list(filenameFilter); } /** * 將字符串寫入指定文件(當指定的父路徑中文件夾不存在時,會最大限度去創建,以保證保存成功!) * * @param res 原字符串 * @param filePath 文件路徑 * @return 成功標記 */ public static boolean string2File(String res, String filePath) { boolean flag = true; BufferedReader bufferedReader = null; BufferedWriter bufferedWriter = null; try { File distFile = new File(filePath); if (!distFile.getParentFile().exists()) distFile.getParentFile().mkdirs(); bufferedReader = new BufferedReader(new StringReader(res)); bufferedWriter = new BufferedWriter(new FileWriter(distFile)); char buf[] = new char[1024]; //字符緩衝區 int len; while ((len = bufferedReader.read(buf)) != -1) { bufferedWriter.write(buf, 0, len); } bufferedWriter.flush(); bufferedReader.close(); bufferedWriter.close(); } catch (IOException e) { flag = false; e.printStackTrace(); } return flag; } } ------------------------------------------------------------------------------------------------------------- import java.io.File; import java.util.ArrayList; import java.util.List; import java.util.Properties; /** * 字符串工具箱 * * @author leizhimin 2008-12-15 22:40:12 */ public final class StringToolkit { /** * 將一個字符串的首字母改爲大寫或者小寫 * * @param srcString 源字符串 * @param flag 大小寫標識,ture小寫,false大些 * @return 改寫後的新字符串 */ public static String toLowerCaseInitial(String srcString, boolean flag) { StringBuilder sb = new StringBuilder(); if (flag) { sb.append(Character.toLowerCase(srcString.charAt(0))); } else { sb.append(Character.toUpperCase(srcString.charAt(0))); } sb.append(srcString.substring(1)); return sb.toString(); } /** * 將一個字符串按照句點(.)分隔,返回最後一段 * * @param clazzName 源字符串 * @return 句點(.)分隔後的最後一段字符串 */ public static String getLastName(String clazzName) { String[] ls = clazzName.split("\\."); return ls[ls.length - 1]; } /** * 格式化文件路徑,將其中不規範的分隔轉換爲標準的分隔符,並且去掉末尾的"/"符號。 * * @param path 文件路徑 * @return 格式化後的文件路徑 */ public static String formatPath(String path) { String reg0 = "\\\\+"; String reg = "\\\\+|/+"; String temp = path.trim().replaceAll(reg0, "/"); temp = temp.replaceAll(reg, "/"); if (temp.endsWith("/")) { temp = temp.substring(0, temp.length() - 1); } if (System.getProperty("file.separator").equals("\\")) { temp= temp.replace('/','\\'); } return temp; } /** * 格式化文件路徑,將其中不規範的分隔轉換爲標準的分隔符,並且去掉末尾的"/"符號(適用於FTP遠程文件路徑或者Web資源的相對路徑)。 * * @param path 文件路徑 * @return 格式化後的文件路徑 */ public static String formatPath4Ftp(String path) { String reg0 = "\\\\+"; String reg = "\\\\+|/+"; String temp = path.trim().replaceAll(reg0, "/"); temp = temp.replaceAll(reg, "/"); if (temp.endsWith("/")) { temp = temp.substring(0, temp.length() - 1); } return temp; } public static void main(String[] args) { System.out.println(System.getProperty("file.separator")); Properties p = System.getProperties(); System.out.println(formatPath("C:///\\xxxx\\\\\\\\\\///\\\\R5555555.txt")); // List<String> result = series2List("asdf | sdf|siii|sapp|aaat| ", "\\|"); // System.out.println(result.size()); // for (String s : result) { // System.out.println(s); // } } /** * 獲取文件父路徑 * * @param path 文件路徑 * @return 文件父路徑 */ public static String getParentPath(String path) { return new File(path).getParent(); } /** * 獲取相對路徑 * * @param fullPath 全路徑 * @param rootPath 根路徑 * @return 相對根路徑的相對路徑 */ public static String getRelativeRootPath(String fullPath, String rootPath) { String relativeRootPath = null; String _fullPath = formatPath(fullPath); String _rootPath = formatPath(rootPath); if (_fullPath.startsWith(_rootPath)) { relativeRootPath = fullPath.substring(_rootPath.length()); } else { throw new RuntimeException("要處理的兩個字符串沒有包含關係,處理失敗!"); } if (relativeRootPath == null) return null; else return formatPath(relativeRootPath); } /** * 獲取當前系統換行符 * * @return 系統換行符 */ public static String getSystemLineSeparator() { return System.getProperty("line.separator"); } /** * 將用“|”分隔的字符串轉換爲字符串集合列表,剔除分隔後各個字符串前後的空格 * * @param series 將用“|”分隔的字符串 * @return 字符串集合列表 */ public static List<String> series2List(String series) { return series2List(series, "\\|"); } /** * 將用正則表達式regex分隔的字符串轉換爲字符串集合列表,剔除分隔後各個字符串前後的空格 * * @param series 用正則表達式分隔的字符串 * @param regex 分隔串聯串的正則表達式 * @return 字符串集合列表 */ private static List<String> series2List(String series, String regex) { List<String> result = new ArrayList<String>(); if (series != null && regex != null) { for (String s : series.split(regex)) { if (s.trim() != null && !s.trim().equals("")) result.add(s.trim()); } } return result; } /** * @param strList 字符串集合列表 * @return 通過“|”串聯爲一個字符串 */ public static String list2series(List<String> strList) { StringBuffer series = new StringBuffer(); for (String s : strList) { series.append(s).append("|"); } return series.toString(); } /** * 將字符串的首字母轉爲小寫 * * @param resStr 源字符串 * @return 首字母轉爲小寫後的字符串 */ public static String firstToLowerCase(String resStr) { if (resStr == null) { return null; } else if ("".equals(resStr.trim())) { return ""; } else { StringBuffer sb = new StringBuffer(); Character c = resStr.charAt(0); if (Character.isLetter(c)) { if (Character.isUpperCase(c)) c = Character.toLowerCase(c); sb.append(resStr); sb.setCharAt(0, c); return sb.toString(); } } return resStr; } /** * 將字符串的首字母轉爲大寫 * * @param resStr 源字符串 * @return 首字母轉爲大寫後的字符串 */ public static String firstToUpperCase(String resStr) { if (resStr == null) { return null; } else if ("".equals(resStr.trim())) { return ""; } else { StringBuffer sb = new StringBuffer(); Character c = resStr.charAt(0); if (Character.isLetter(c)) { if (Character.isLowerCase(c)) c = Character.toUpperCase(c); sb.append(resStr); sb.setCharAt(0, c); return sb.toString(); } } return resStr; } }
FileUtils
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.