NO.72 [file]针对文件路径串的常用工具类

本文介绍了一个Java文件操作工具类,提供了文件名、文件类型、MIME类型等常用功能,支持Win、Unix路径。
 

特点:支持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;
	}
}

在处理 Scala 或 Java 代码时,如果使用 `java.io.File` 来访问 DBFS(Databricks File System)路径,会遇到 `java.io.IOException` 错误,因为 `java.io.File` 并不支持 DBFS 这分布式文件系统路径。DBFS 是 Databricks 提供的虚拟文件系统抽象,底层实际可能指向云存储(如 AWS S3、Azure Blob Storage 等),而 `java.io.File` 仅适用于本地文件系统路径。 ### 解决方案 要修复该问题,应使用 Databricks 提供的 `dbutils` 工具或 Apache Spark 的 `SparkSession` 来访问 DBFS 路径,而不是依赖 `java.io.File`。以下是几种可行的替代方法: #### 方法一:使用 `dbutils.fs` 操作 DBFS 路径 Databricks 提供了 `dbutils.fs` API 来操作 DBFS 中的文件和目录,例如: ```scala dbutils.fs.mkdirs("dbfs:/path/to/dir") dbutils.fs.put("dbfs:/path/to/file.txt", "content", true) ``` 该方式可以避免因使用 `java.io.File` 而导致的路径识别问题 [^1]。 #### 方法二:使用 Spark 的 `SparkSession` 读写 DBFS 文件 通过 Spark 的 `read` 和 `write` 接口访问 DBFS 路径: ```scala val df = spark.read.text("dbfs:/path/to/file.txt") df.write.text("dbfs:/path/to/output.txt") ``` 这种方法利用了 Spark 内置的文件系统抽象,能够正确识别 DBFS 路径。 #### 方法三:使用 Hadoop 配置访问 DBFS 如果需要在低层级代码中访问 DBFS,可以使用 Hadoop 的 `Configuration` 和 `FileSystem` : ```scala import org.apache.hadoop.conf.Configuration import org.apache.hadoop.fs._ val conf = new Configuration() val fs = FileSystem.get(new URI("dbfs:/"), conf) val path = new Path("dbfs:/path/to/file.txt") if (!fs.exists(path)) { fs.create(path) } ``` 该方式通过 Hadoop 文件系统接口访问 DBFS,避免了 `java.io.File` 的局限性。 ### 注意事项 - `java.io.File` 不适用于分布式文件系统路径,如 `dbfs://`、`s3a://`、`wasb://` 等。 - 在 Databricks 环境中,建议优先使用 `dbutils.fs` 或 Spark 的文件读写接口处理 DBFS 路径。 - 若需兼容本地文件系统和 DBFS,可封装路径处理逻辑,根据路径前缀(如 `dbfs://`)动态选择访问方式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值