文件路径工具类-FilePathUtil
==============================================文件路径工具类
import java.io.File; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Random; import org.apache.commons.io.FilenameUtils; 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 prefixFilePath(String filePath, String prefix){ String sourceName = extractFileName(filePath); return filePath.replace(sourceName, prefix+sourceName); } /** 追加文件名后缀 * @param filePath 例如 xxx.png * @param suffix 例如xxx_yyyyMMdd.png * @return */ public static String suffixFilePath(String filePath, String suffix){ String extension = FilenameUtils.getExtension(filePath); return filePath.replace("."+extension, suffix+"."+extension); } /** 随机唯一文件名字 * @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(); } }
==============================================文件路径工具测试类
import org.apache.commons.io.FilenameUtils; import org.junit.Test; import com.icarus._BaseTest; public class FilePathUtilTest extends _BaseTest{ /** * 工具类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); String dirName = "E:\\home\\commUtils\\temp\\"; fullPathNoEndSeparator = FilenameUtils.getFullPathNoEndSeparator(dirName); System.out.println(fullPathNoEndSeparator); } /** * 获取文件目录 */ @Test public void test_getFilePath() { String conTextPath = FilePathUtil.getConTextPath(); System.out.println(conTextPath); } /** * 修改文件名 */ @Test public void test_prefixFilePath() { 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.prefixFilePath(filePath, "temp_"); System.out.println(fileName); } /** * 修改文件名 */ @Test public void test_suffixFilePath() { 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.suffixFilePath(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); }