时髦地将路径拆分为名称和父级

  • 本文关键字:拆分 路径 groovy path
  • 更新时间 :
  • 英文 :


我正在尝试将路径拆分为父路径和名称。

尝试时

String path = "/root/file"
File file = new File(path)
println("Name: " + file.name)
println("Parent: " + file.parent)

我们得到

Name: file
Parent: /root

有了 Windows 路径C:\root\file.exe我们得到

Name: C:rootfile.exe
Parent: null

这是预期的行为吗?如果是,我如何获得相同的 Windows 路径结果?(如果可能,请不要使用正则表达式(

使用 .replace 将 "\" 更改为 "/

String path = "C:\root\file.exe"
path = path.replace("\","/")
File file = new File(path)
println("Name: " + file.name)
println("Parent: " + file.parent)

最新更新