NO.72 [file]針對文件路徑串的常用工具類

 

特點:支持WinUnix文件路徑;補充實現了Java IO File類中未實現的部分功能(非輸入輸出部分)

輸入輸出的工具類參見IOUtils:

NO.63 [file]IO常用工具類IOUtils(Java讀文件、寫文件、打Zip包)  

目前功能:

  1. getFileName返回一個路徑串的文件名部分;
  2. getFileNameWithoutPostfix返回一個路徑串的文件名部分(無後綴);
  3. getFileType返回一個路徑串的文件類型;
  4. getParentPath返回一個路徑串的父目錄路徑;
  5. getMimeTypeByFileType根據文件類型返回其對應的MIME類型;

 

源碼:

package amosryan.utility.file;

import java.io.File;
import java.util.Properties;
/**
 * 文件常用功能(非輸入輸出部分)
 * @author amosryan
 * @since 2009.01.14
 */
public class FileUtils {
	public static Properties mimeCastProp = new Properties();
	static {
		mimeCastProp.setProperty("DOC", "application/msword");
		mimeCastProp.setProperty("XLS", "application/vnd.ms-excel");
		mimeCastProp.setProperty("TXT", "text/plain");
		mimeCastProp.setProperty("XML", "text/xml");
		mimeCastProp.setProperty("PDF", "application/pdf");
	}

	/**
	 * 創建一個新文件
	 * @param newFile
	 * @throws Exception
	 */
	public static void createNewFile(File newFile) throws Exception {
		File parent = newFile.getParentFile();
		if (!parent.exists()) {
			parent.mkdirs();
		}
		newFile.createNewFile();
	}

	/**
	 * 獲取一個文件的文件類型(取文件名中.後部分)
	 * @param filePath
	 * @return
	 */
	public static String getFileType(String filePath) {
		filePath = getFileName(filePath);
		int index = filePath.lastIndexOf('.');
		if (index > -1) {
			filePath = filePath.substring(index + 1);
		}
		return filePath;
	}

	/**
	 * 獲取一個文件的文件名(無後綴,取文件名中.前部分)
	 * @param filePath
	 * @return
	 */
	public static String getFileNameWithoutPostfix(String filePath) {
		int index = getFileName(filePath).lastIndexOf('.');
		if (index > -1) {
			filePath = filePath.substring(0, index);
		}
		return filePath;
	}

	/**
	 * 獲取一個文件的文件名(取路徑中最後的目錄分隔符後部分)
	 * @param filePath
	 * @return
	 */
	public static String getFileName(String filePath) {
		int index = filePath.lastIndexOf("/");
		if (index > -1) {
			filePath = filePath.substring(index + 1);
		} else {
			index = filePath.lastIndexOf("\\");
			if (index > -1) {
				filePath = filePath.substring(index + 1);
			}
		}
		return filePath;
	}

	/**
	 * 獲取目錄串的父目錄串
	 * @param dirPath
	 * @return
	 */
	public static String getParentPath(String dirPath) {
		if(dirPath.endsWith("/")||dirPath.endsWith("\\")){
			dirPath = dirPath.substring(0,dirPath.length()-1);
		}
		int index = dirPath.lastIndexOf("/");
		if (index > -1) {
			dirPath = dirPath.substring(0,index);
		} else {
			index = dirPath.lastIndexOf("\\");
			if (index > -1) {
				dirPath = dirPath.substring(0,index);
			}
		}
		return dirPath;
	}

	/**
	 * 獲取文件路徑串的目錄路徑串
	 * @param filePath
	 * @return
	 */
	public static String getDirPath(String filePath) {
		return getParentPath(filePath);
	}

	/**
	 * 根據文件類型返回其對應的MIME類型
	 * @param fileType
	 * @return
	 */
	public static String getMimeTypeByFileType(String fileType) {
		String mimeType = mimeCastProp.getProperty(fileType.toUpperCase());
		if (mimeType == null) {
			mimeType = "application/x-msdownload";
		}
		return mimeType;
	}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章