gpt4 book ai didi

Groovy 将路径拆分为名称和父级

转载 作者:行者123 更新时间:2023-12-04 14:00:58 26 4
gpt4 key购买 nike

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

尝试时

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 路径获得相同的结果? (如果可能,请不要使用正则表达式)

最佳答案

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

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

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

关于Groovy 将路径拆分为名称和父级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45015476/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com