【问题标题】:Groovy split path into name and parentGroovy 将路径拆分为名称和父级
【发布时间】:2017-12-14 09:22:15
【问题描述】:

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

尝试时

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:\root\file.exe
Parent: null

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

【问题讨论】:

标签: groovy path


【解决方案1】:

使用 .replace 将“\”更改为“/”

String path = "C:\\root\\file.exe"
path = path.replace("\\","/")
File file = new File(path)

println("Name: " + file.name)
println("Parent: " + file.parent)

【讨论】:

    猜你喜欢
    • 2020-08-22
    • 2012-05-17
    • 2015-12-20
    • 2021-10-11
    • 2021-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-15
    相关资源
    最近更新 更多