给定文件路径使用 Scala 找到文件扩展名?

Given the file path find the file extension using Scala?

我正在尝试查找文件类型,以便根据文件类型读取文件。 输入有不同的文件格式,例如 CVS、excel 和 orc 等..,

例如输入=>"D:\resources\core_dataset.csv" 我期待输出 => csv

您可以按如下方式实现:

import java.nio.file.Paths

val path = "/home/gmc/exists.csv"
val fileName = Paths.get(path).getFileName            // Convert the path string to a Path object and get the "base name" from that path.
val extension = fileName.toString.split("\.").last   // Split the "base name" on a . and take the last element - which is the extension.
// The above produces:

extension: String = csv