【Java工具】之文件工具类(五)

因为篇幅过多,分成两部分展示。上一篇
15.获得某一文件夹下的所有文件的路径集合
/** * 获得某一文件夹下的所有文件的路径集合 * * @param filePath *文件夹路径 * @return ArrayList,其中的每个元素是一个文件的路径的字符串 */ public static ArrayList getFilePathFromFolder(String filePath) { ArrayList fileNames = new ArrayList(); File file = new File(filePath); try { File[] tempFile = file.listFiles(); for (int i = 0; i < tempFile.length; i++) { if (tempFile[i].isFile()) { String tempFileName = tempFile[i].getName(); fileNames.add(makeFilePath(filePath, tempFileName)); } } } catch (Exception e) { // fileNames.add("尚无文件到达!"); // e.printStackTrace(); // log4j.info("Can not find files!"+e.getMessage()); } return fileNames; }

16.递归遍历文件目录,获取所有文件路径
/** * 递归遍历文件目录,获取所有文件路径 * * @param filePath * @return 2012-1-4 */ public static ArrayList getAllFilePathFromFolder(String filePath) { ArrayList filePaths = new ArrayList(); File file = new File(filePath); try { File[] tempFile = file.listFiles(); for (int i = 0; i < tempFile.length; i++) { String tempFileName = tempFile[i].getName(); String path = makeFilePath(filePath, tempFileName); if (tempFile[i].isFile()) { filePaths.add(path); } else { ArrayList tempFilePaths = getAllFilePathFromFolder(path); if (tempFilePaths.size() > 0) { for (String tempPath : tempFilePaths) { filePaths.add(tempPath); } } } } } catch (Exception e) { // fileNames.add("尚无文件到达!"); // e.printStackTrace(); // log4j.info("Can not find files!"+e.getMessage()); } return filePaths; }

17.获得某一文件夹下的所有TXT,txt文件名的集合
/** * 获得某一文件夹下的所有TXT,txt文件名的集合 * * @param filePath *文件夹路径 * @return ArrayList,其中的每个元素是一个文件名的字符串 */ @SuppressWarnings("rawtypes") public static ArrayList getFileNameFromFolder(String filePath) { ArrayList fileNames = new ArrayList(); File file = new File(filePath); File[] tempFile = file.listFiles(); for (int i = 0; i < tempFile.length; i++) { if (tempFile[i].isFile()) fileNames.add(tempFile[i].getName()); } return fileNames; }

18.获得某一文件夹下的所有文件的总数
/** * 获得某一文件夹下的所有文件的总数 * * @param filePath *文件夹路径 * @return int 文件总数 */ public static int getFileCount(String filePath) { int count = 0; try { File file = new File(filePath); if (!isFolderExist(filePath)) return count; File[] tempFile = file.listFiles(); for (int i = 0; i < tempFile.length; i++) { if (tempFile[i].isFile()) count++; } } catch (Exception fe) { count = 0; } return count; }

19.获得某一路径下要求匹配的文件的个数
/** * 获得某一路径下要求匹配的文件的个数 * * @param filePath *文件夹路径 * @param matchs *需要匹配的文件名字符串,如".*a.*",如果传空字符串则不做匹配工作 直接返回路径下的文件个数 * @return int 匹配文件名的文件总数 */ public static int getFileCount(String filePath, String matchs) { int count = 0; if (!isFolderExist(filePath)) return count; if (matchs.equals("") || matchs == null) return getFileCount(filePath); File file = new File(filePath); // log4j.info("filePath in getFileCount: " + filePath); // log4j.info("matchs in getFileCount: " + matchs); File[] tempFile = file.listFiles(); for (int i = 0; i < tempFile.length; i++) { if (tempFile[i].isFile()) if (Pattern.matches(matchs, tempFile[i].getName())) count++; } return count; }public static int getStrCountFromFile(String filePath, String str) { if (!isFileExist(filePath)) return 0; FileReader fr = null; BufferedReader br = null; int count = 0; try { fr = new FileReader(filePath); br = new BufferedReader(fr); String line = null; while ((line = br.readLine()) != null) { if (line.indexOf(str) != -1) count++; } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (br != null) br.close(); if (fr != null) fr.close(); } catch (Exception e) { e.printStackTrace(); } } return count; }

20.获得某一文件的行数
/** * 获得某一文件的行数 * * @param filePath *文件夹路径 * * @return int 行数 */ public static int getFileLineCount(String filePath) { if (!isFileExist(filePath)) return 0; FileReader fr = null; BufferedReader br = null; int count = 0; try { fr = new FileReader(filePath); br = new BufferedReader(fr); while ((br.readLine()) != null) { count++; } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (br != null) br.close(); if (fr != null) fr.close(); } catch (Exception e) { e.printStackTrace(); } } return count; }

21.判断某一文件是否为空
/** * 判断某一文件是否为空 * * @param filePath 文件的路径字符串,e.g. "c:\cs.txt" * @return 如果文件为空返回true, 否则返回false * @throws IOException */ public static boolean ifFileIsNull(String filePath) throws IOException { boolean result = false; FileReader fr = new FileReader(filePath); if (fr.read() == -1) { result = true; // log4j.info(filePath + " is null!"); } else { // log4j.info(filePath + " not null!"); } fr.close(); return result; }

22.判断文件是否存在
/** * 判断文件是否存在 * * @param fileName 文件路径字符串,e.g. "c:\cs.txt" * @return 若文件存在返回true,否则返回false */ public static boolean isFileExist(String fileName) { // 判断文件名是否为空 if (fileName == null || fileName.length() == 0) { // log4j.error("File length is 0!"); return false; } else { // 读入文件 判断文件是否存在 File file = new File(fileName); if (!file.exists() || file.isDirectory()) { // log4j.error(fileName + "is not exist!"); return false; } } return true; }

23.判断文件夹是否存在
/** * 判断文件夹是否存在 * * @param folderPath 文件夹路径字符串,e.g. "c:\cs" * @return 若文件夹存在返回true, 否则返回false */ public static boolean isFolderExist(String folderPath) { File file = new File(folderPath); return file.isDirectory() ? true : false; }

24.获得文件的大小
/** * 获得文件的大小 * * @param filePath 文件路径字符串,e.g. "c:\cs.txt" * @return 返回文件的大小,单位kb,如果文件不存在返回null */ public static Double getFileSize(String filePath) { if (!isFileExist(filePath)) return null; else { File file = new File(filePath); double intNum = Math.ceil(file.length() / 1024.0); return new Double(intNum); }}

【【Java工具】之文件工具类(五)】25. 获得文件的大小,字节表示
/** * 获得文件的大小,字节表示 * * @param filePath 文件路径字符串,e.g. "c:\cs.txt" * @return 返回文件的大小,单位kb,如果文件不存在返回null */ public static Double getFileByteSize(String filePath) { if (!isFileExist(filePath)) return null; else { File file = new File(filePath); double intNum = Math.ceil(file.length()); return new Double(intNum); }}

26.获得外汇牌价文件的大小(字节)
/** * 获得外汇牌价文件的大小(字节) * * @param filePath 文件路径字符串,e.g. "c:\cs.txt" * @return 返回文件的大小,单位kb,如果文件不存在返回null */ public static Double getWhpjFileSize(String filePath) { if (!isFileExist(filePath)) return null; else { File file = new File(filePath); return new Double(file.length()); }}

27.获得文件的最后修改时间
/** * 获得文件的最后修改时间 * * @param filePath 文件路径字符串,e.g. "c:\cs.txt" * @return 返回文件最后的修改日期的字符串,如果文件不存在返回null */ public static String fileModifyTime(String filePath) { if (!isFileExist(filePath)) return null; else { File file = new File(filePath); long timeStamp = file.lastModified(); SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm"); String tsForm = formatter.format(new Date(timeStamp)); return tsForm; } }

28.遍历某一文件夹下的所有文件,返回一个ArrayList
/** * 遍历某一文件夹下的所有文件,返回一个ArrayList,每个元素又是一个子ArrayList, * 子ArrayList包含三个字段,依次是文件的全路径(String),文件的修改日期(String), 文件的大小(Double) * * @param folderPath 某一文件夹的路径 * @return ArrayList */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static ArrayList getFilesSizeModifyTime(String folderPath) { List returnList = new ArrayList(); List filePathList = getFilePathFromFolder(folderPath); for (int i = 0; i < filePathList.size(); i++) { List tempList = new ArrayList(); String filePath = (String) filePathList.get(i); String modifyTime = FileUtils.fileModifyTime(filePath); Double fileSize = FileUtils.getFileSize(filePath); tempList.add(filePath); tempList.add(modifyTime); tempList.add(fileSize); returnList.add(tempList); } return (ArrayList) returnList; }

29.获得某一文件夹下的所有TXT,txt文件名的集合
/** * 获得某一文件夹下的所有TXT,txt文件名的集合 * * @param filePath *文件夹路径 * @return ArrayList,其中的每个元素是一个文件名的字符串 */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static ArrayList getTxtFileNameFromFolder(String filePath) { ArrayList fileNames = new ArrayList(); File file = new File(filePath); File[] tempFile = file.listFiles(); for (int i = 0; i < tempFile.length; i++) { if (tempFile[i].isFile()) if (tempFile[i].getName().indexOf("TXT") != -1 || tempFile[i].getName().indexOf("txt") != -1) { fileNames.add(tempFile[i].getName()); } } return fileNames; }

30.获得某一文件夹下的所有xml,XML文件名的集合
/** * 获得某一文件夹下的所有xml,XML文件名的集合 * * @param filePath *文件夹路径 * @return ArrayList,其中的每个元素是一个文件名的字符串 */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static ArrayList getXmlFileNameFromFolder(String filePath) { ArrayList fileNames = new ArrayList(); File file = new File(filePath); File[] tempFile = file.listFiles(); for (int i = 0; i < tempFile.length; i++) { if (tempFile[i].isFile()) if (tempFile[i].getName().indexOf("XML") != -1 || tempFile[i].getName().indexOf("xml") != -1) { fileNames.add(tempFile[i].getName()); } } return fileNames; }

31.校验文件是否存在
/** * 校验文件是否存在 * * @param fileName *String 文件名称 * @param mapErrorMessage *Map 错误信息Map集 * @return boolean 校验值 */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static boolean checkFile(String fileName, HashMap mapErrorMessage) { if (mapErrorMessage == null) mapErrorMessage = new HashMap(); // 判断文件名是否为空 if (fileName == null) { fileName = ""; } // 判断文件名长度是否为0 if (fileName.length() == 0) { mapErrorMessage.put("errorMessage", "fileName length is 0"); return false; } else { // 读入文件 判断文件是否存在 File file = new File(fileName); if (!file.exists() || file.isDirectory()) { mapErrorMessage.put("errorMessage", fileName + "is not exist!"); return false; } } return true; }

32.校验文件是否存在
/** * 校验文件是否存在 add by fzhang * * @param fileName *String 文件名称 * @return boolean 校验值 */ public static boolean checkFile(String fileName) { // 判断文件名是否为空 if (fileName == null) { fileName = ""; } // 判断文件名长度是否为0 if (fileName.length() == 0) {// log4j.info("File name length is 0."); return false; } else { // 读入文件 判断文件是否存在 File file = new File(fileName); if (!file.exists() || file.isDirectory()) { // log4j.info(fileName +"is not exist!"); return false; } } return true; }

33.新建目录
/** * 新建目录 * * @param folderPath *String 如 c:/fqf * @return boolean */ public static void newFolder(String folderPath) { try { String filePath = folderPath; filePath = filePath.toString(); java.io.File myFilePath = new java.io.File(filePath); if (!myFilePath.exists()) { myFilePath.mkdir(); } } catch (Exception e) { System.out.println("新建目录操作出错"); e.printStackTrace(); } }

34.重新缓存发送失败的缓存文件
/** * 重新缓存发送失败的缓存文件 * * @author Herman.Xiong * @throws IOException */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static void sessionData(String path, List list) throws IOException {BufferedWriter bw = new BufferedWriter(new FileWriter(path)); for (List tempList : list) {for (String str : tempList) { if (str != null && !str.equals("")) { bw.write(str); bw.newLine(); bw.flush(); } } } bw.close(); }

35.在指定的文本中对比数据
/** * 在指定的文本中对比数据 * * @param urladdr * @param filePath * @return boolean */ public static boolean compareUrl(String urladdr, String filePath, FileChannel fc) { boolean isExist = false; Charset charset = Charset.forName("UTF-8"); CharsetDecoder decoder = charset.newDecoder(); try { int sz = (int) fc.size(); MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, sz); CharBuffer cb = decoder.decode(bb); String s = String.valueOf(cb); int n = s.indexOf(urladdr); if (n > -1) { // log4j.info(filePath + " the article already exists " + // urladdr); } else { isExist = true; } } catch (Exception e) { // log4j.error("document alignment error" + e); } finally { try { // if(!Util.isEmpty(fc)) // { // fc.close(); // } // if(!Util.isEmpty(fis)) // { // fis.close(); // } } catch (Exception e2) { // log4j.error(e2); } } return isExist; }

转载地址请移步这里。如有问题,劳烦通知删除!
【Java工具】之文件工具类(五)
文章图片

    推荐阅读