zoukankan      html  css  js  c++  java
  • Java 文件名操作的相关工具类

    项目中的操作文件名的工具类:

      1 import java.io.File;
      2 import java.util.regex.Matcher;
      3 import java.util.regex.Pattern;
      4 
      5 public class FileNameUtil {
      6     /**
      7      * 修改指定文件的扩展名
      8      * @param fileName
      9      * @param newExt
     10      * @return
     11      */
     12     public static String changeFileExt(String fileName, String newExt){
     13         return fileName.replaceAll("\.[^\.\\/]*$", "") + "." + newExt.replaceAll("^\.", "");
     14     }
     15     /**
     16      * 去掉文件的扩展名
     17      * @param fileName
     18      * @return
     19      */
     20     public static String removeFileExt(String fileName){
     21         return fileName.replaceAll("\.[^\\/\.]*$", "");
     22     }
     23     
     24     /**
     25      * 修正文件名错误,主要包括出现/的、双\的 
     26      * @param fileName
     27      * @return
     28      */
     29     public static String correctFileName(String fileName){
     30         return fileName.replaceAll("(?!^)[\\/]+", "\\");
     31     }
     32     /**
     33      * 修正文件名错误,主要包括出现/的、双\的 成为linux 
     34      * @param fileName
     35      * @return
     36      */
     37     public static String correctFileName4Linux(String fileName){
     38         return fileName.replaceAll("(?!^)[\\/]+", "/");
     39     }
     40     
     41     /**
     42      * 判断文件是否存在 
     43      * @param fileName
     44      * @return
     45      */
     46     public static boolean isFileExists(String fileName){
     47         //把一个或多个或/替换成1个
     48         File f = new File(correctFileName(fileName));
     49         try{
     50             return f.exists();
     51         }finally{
     52             f = null;
     53         }
     54     }
     55     
     56     /**
     57      * 连接两个文件名
     58      * @param base
     59      * @param addition
     60      * @return
     61      */
     62     public static String fileNameAdd(String base, String addition){
     63         return base.replaceAll("[\\/]+$", "") + "\" + addition.replaceAll("^[\\/]+", "");
     64     }
     65     
     66     /**
     67      * 是不是UNC路径 
     68      * @param fileName
     69      * @return
     70      */
     71     public static boolean isUNC(String fileName){
     72         return fileName.matches("^\\{2}[^\\/]+\\[\s\S]*$");
     73     }
     74     
     75     /**
     76      * 获取文件名的扩展名 
     77      * @param fileName
     78      * @return
     79      */
     80     public static String extractFileExt(String fileName){
     81         Pattern p = Pattern.compile("\.[^\\/.]+$");
     82         Matcher m = p.matcher(fileName);
     83         return m.find()? m.group() : "";
     84     }
     85     
     86     /**
     87      * 获取文件的路径(最后的会被去掉) 
     88      * @param fileName
     89      * @return
     90      */
     91     public static String extractFilePath(String fileName){
     92         return fileName.replaceAll("[\\/][^\\/]*$", "");
     93     }
     94     
     95     /**
     96      * 获取文件绝对路径的文件名部分 
     97      * @param fileName
     98      * @return
     99      */
    100     public static String extractFileName(String fileName){
    101         return fileName.replaceAll("^[\s\S]*[\\/]", "");
    102     }
    103     
    104     /**
    105      * 获取相对路径(去掉盘符或UNC的主机)
    106      * @param fileName
    107      * @return
    108      */
    109     public static String extractRelativePath(String fileName){
    110         if(isUNC(fileName)){
    111             return fileName.replaceAll("^\\{2}[^\\/]+[\\/]+", "");
    112         }else{
    113             return fileName.replaceAll("^.*\:\\+", "");
    114         }
    115     }
    116     
    117     /**
    118      * 把盘符 和 文件路径拼接起来 得到完整的文件地址,自动判断拼接的时候前面是不是有  斜杠 
    119      * @param driverOrLpath windows系统下的盘符,或者是linux系统下的路径
    120      * @param filename 文件的路径 如: 二次合成2011IPTV上海文广电影123456_变形金刚.ts
    121      */
    122     public static String joinPath(String driverOrLpath,String filename ){
    123         String d = driverOrLpath.replaceAll("[\\/]*$", "") ;
    124         filename = filename.replaceAll("^[\\/]*", ""); // 把开头的 斜杠都去掉,后面统一加
    125         
    126         return d + File.separator + filename;
    127     }
    128     
    129     /**
    130      * 功能:替换掉文件名字中的特殊字符
    131      * 时间:2016-01-21
    132      * @param filename
    133      * @return
    134      */
    135     public static String removeSpecialcharacter(String filename){
    136          Pattern pattern=Pattern.compile("[u4e00-u9fa5]");//中文汉字编码区间  
    137            Matcher matcher;
    138            char[] array = filename.toCharArray();
    139            for (int i = 0; i < array.length; i++) {
    140                 if((char)(byte)array[i]!=array[i]){//取出双字节字符
    141                     matcher=pattern.matcher(String.valueOf(array[i]));
    142                     if(!matcher.matches()){//中文汉字无需替换
    143                         filename=filename.replaceAll(String.valueOf(array[i]), "");//特殊字符用空字符串替换
    144                     }
    145                 }
    146             }
    147            return filename;
    148     }
    149     
    150     public static void main(String[] args) {
    151         
    152     }
    153 }

     得到一个文件/目录下文件名/路径  或者是子文件的子文件名....

     1     /*
     2      * 如果想要获得当前文件中的文件名只需要String [] fileName = file.list();就可以了。
     3      * 如果要包括文件中的文件名就可以用递归的方式。下面是两个具体的实现。 
     4      * 其中public static String [] getFileName(String path)是只得到当前文件中的文件名。
     5      * public static void getAllFileName(String path,ArrayList<String> fileName)是包括当前文件及其子文件的文件名。
     6      */
     7     public static String[] getFileName(String path) {
     8         File file = new File(path);
     9         String[] fileName = file.list();
    10         return fileName;
    11     }
    12     
    13     public static ArrayList<String> getFilePath(String parentPath) {
    14         ArrayList<String> childFilesPathList = new ArrayList<String>();
    15         File parentFile = new File(parentPath);
    16         File[] childFiles = parentFile.listFiles();
    17         for (File childFile : childFiles) {
    18             childFilesPathList.add(childFile.getAbsolutePath());
    19         }
    20         return childFilesPathList;
    21     }
    22 
    23     public static void getAllFileName(String path, ArrayList<String> fileName) {
    24         File file = new File(path);
    25         File[] files = file.listFiles();
    26         String[] names = file.list();
    27         if (names != null)
    28             fileName.addAll(Arrays.asList(names));
    29         for (File a : files) {
    30             if (a.isDirectory()) {
    31                 getAllFileName(a.getAbsolutePath(), fileName);
    32             }
    33         }
    34     }
  • 相关阅读:
    [Python] Python 学习
    [其它] 学习杂记
    [.NET] 《Effective C#》快速笔记(三)- 使用 C# 表达设计
    [.NET] 《Effective C#》快速笔记(二)- .NET 资源托管
    [python] 小游戏
    [.NET] 《Effective C#》快速笔记(一)- C# 语言习惯
    [angularjs] AngularJs 知识回顾
    [C#] 使用 StackExchange.Redis 封装属于自己的 RedisHelper
    [.NET] 一步步打造一个简单的 MVC 电商网站
    [.NET] 一步步打造一个简单的 MVC 电商网站
  • 原文地址:https://www.cnblogs.com/DreamDrive/p/5760220.html
Copyright © 2011-2022 走看看