Java教程

文件路径工具类-FilePathUtil

本文主要是介绍文件路径工具类-FilePathUtil,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

==============================================文件路径工具类

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import com.taoxw.utils.string.StringFormatUtil;

public class FilePathUtil {
    
      /**
       * 获取windows/linux的项目根目录
       * @return
       */
      public static String getConTextPath(){
        String fileUrl = Thread.currentThread().getContextClassLoader().getResource("").getPath();
        if("usr".equals(fileUrl.substring(1,4))){
          fileUrl = (fileUrl.substring(0,fileUrl.length()-16));//linux
        }else{
          fileUrl = (fileUrl.substring(1,fileUrl.length()-10));//windows
        }
        return fileUrl;
      }
    
    /**
     * 获取文件目录
     * @param filePath
     * @return
     *         目录路径,不以'/'或操作系统的文件分隔符结尾
     */
    public static String extractDirPath(String filePath) {
        int separatePos = Math.max(filePath.lastIndexOf('/'), filePath.lastIndexOf('\\')); // 分隔目录和文件名的位置
        return separatePos == -1 ? null : filePath.substring(0, separatePos);
    }
    
    /** 解析文件路径,获取文件名
     * @param filePath  C:/taoxw/20190103.txt
     * @return
     */
    public static String extractFileName(String filePath){
        if(filePath != null){
            if(filePath.lastIndexOf("/")>=0){
                return filePath.substring(filePath.lastIndexOf("/")+1);
            }else if(filePath.lastIndexOf("\\")>=0){
                return filePath.substring(filePath.lastIndexOf("\\")+1);
            }else{
                return null;
            }
        }else{
            return null;
        }
    }
    
    /**
     * String filename = "E:\\home\\commUtils\\temp\\Person03.txt";
     * String baseName = FilenameUtils.getBaseName(filename);
     *     return == Person03
     * String extension = FilenameUtils.getExtension(filename);
     *     return == txt
     * String fullPath = FilenameUtils.getFullPath(filename);
     *     return == E:\home\commUtils\temp\
     * String fullPathNoEndSeparator = FilenameUtils.getFullPathNoEndSeparator(filename);
     *     return == E:\home\commUtils\temp
     * String path = FilenameUtils.getPath(filename);
     *     return == home\commUtils\temp\
     * String pathNoEndSeparator = FilenameUtils.getPathNoEndSeparator(filename);
     *     return == home\commUtils\temp
     * String prefix = FilenameUtils.getPrefix(filename);
     *     return == E:\
     */
    
    /** 解析文件,获取文件名
     * @param file
     * @return
     */
    public static String getFileName(File file){
        return file.getName();
    }
    
    /** 追加文件名前缀
     * @param filePath 例如 xxx.png
     * @param prefix 例如temp_xxx.png
     * @return
     */
    public static String updateFilePath(String filePath, String prefix){
        String sourceName = extractFileName(filePath);
        return filePath.replace(sourceName, prefix+sourceName);
    }
    
    /** 随机唯一文件名字
     * @param suffixName  .txt .jpg .JPG .png .PNG
     * @return
     */
    public static String createFileName(String suffixName){
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        StringBuilder fileNameBuilder = new StringBuilder(df.format(new Date()));
        fileNameBuilder.append("_");
        fileNameBuilder.append(StringFormatUtil.leftPadForZero(Integer.toString(new Random().nextInt(1000)), 3));
        fileNameBuilder.append(".");
        fileNameBuilder.append(suffixName);
        return fileNameBuilder.toString();
    }
    public static String createFileName(String preffixName, String suffixName){
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        StringBuilder fileNameBuilder = new StringBuilder(preffixName);
        fileNameBuilder.append("_");
        fileNameBuilder.append(df.format(new Date()));
        fileNameBuilder.append("_");
        fileNameBuilder.append(StringFormatUtil.leftPadForZero(Integer.toString(new Random().nextInt(1000)), 3));
        fileNameBuilder.append(".");
        fileNameBuilder.append(suffixName);
        return fileNameBuilder.toString();
    }
}

 

==============================================文件路径工具测试类

    /**
     * 工具类FilenameUtil相关方法
     */
    @Test
    public void test_FilenameUtil() {
        String filename = "E:\\home\\commUtils\\temp\\Person03.txt";
        String baseName = FilenameUtils.getBaseName(filename);
        System.out.println(baseName);
        String extension = FilenameUtils.getExtension(filename);
        System.out.println(extension);
        String fullPath = FilenameUtils.getFullPath(filename);
        System.out.println(fullPath);
        String fullPathNoEndSeparator = FilenameUtils.getFullPathNoEndSeparator(filename);
        System.out.println(fullPathNoEndSeparator);
        String path = FilenameUtils.getPath(filename);
        System.out.println(path);
        String pathNoEndSeparator = FilenameUtils.getPathNoEndSeparator(filename);
        System.out.println(pathNoEndSeparator);
        String prefix = FilenameUtils.getPrefix(filename);
        System.out.println(prefix);
    }
    
    /**
     * 获取文件目录
     */
    @Test
    public void test_getFilePath() {
        String conTextPath = FilePathUtil.getConTextPath();
        System.out.println(conTextPath);
    }
    
    /**
     * 修改文件名
     */
    @Test
    public void test_updateFilePath() {
        String filePath = "E:\\home\\commUtils\\temp\\Person.txt";
//        String filePath = "E:/home/commUtils/temp/Person.txt";
//        String filePath = "E://home//commUtils//temp//Person.txt";
//        String filePath = "/home/commUtils/temp/Person.txt";
//        String filePath = "/home/commUtils/temp/Person-tx.tx.txt";
        String fileName = FilePathUtil.updateFilePath(filePath, "temp_");
        System.out.println(fileName);
    }
    
    /**
     * 解析文件路径,获取文件名
     */
    @Test
    public void test_getFileName() {
//        String filePath = "E:\\home\\commUtils\\temp\\Person.txt";
//        String filePath = "E:/home/commUtils/temp/Person.txt";
//        String filePath = "E://home//commUtils//temp//Person.txt";
//        String filePath = "/home/commUtils/temp/Person.txt";
        String filePath = "/home/commUtils/temp/Person-tx.tx.txt";
        String fileName = FilePathUtil.extractFileName(filePath);
        System.out.println(fileName);
    }
    
    /**
     * 创建文件名
     */
    @Test
    public void test_createFileName() {
        String fileName01 = FilePathUtil.createFileName("txt");
        System.out.println(fileName01);
        String fileName02 = FilePathUtil.createFileName("taoxw", "txt");
        System.out.println(fileName02);
    }

 

这篇关于文件路径工具类-FilePathUtil的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!