gpt4 book ai didi

java - Window/linux 路径组件分离

转载 作者:太空宇宙 更新时间:2023-11-04 11:24:00 25 4
gpt4 key购买 nike

我正在我的 linux 环境中编写一个路径分隔符,它是为了解析一个文件路径,它可以是 linux 和 windows 风格,我以前不知道。目前我正在使用 File.separator 进行拆分,但这只适用于我所在的平台......

我想知道是否有办法使用诸如 Path 对象之类的东西来处理这个问题。

[...]
Node pathElement = nodeList.item(nodePos);
String path = pathElement.getTextContent();
String[] pathElements = path.split(File.separator);
String contentFileName = pathElements[pathElements.length - 1];
String parentFolderPath = xmlFiles[u].getParentFile().getAbsolutePath();
pathElement.setTextContent(parentFolderPath + File.separator + contentFileName);
[...]

编辑:我需要能够打破 linux 的 windows 样式路径,反之亦然。我也曾尝试使用 Paths.getPath(...) 但未成功

查看上述代码的示例输出:

/input/ZIP_0bd78143-f6cd-473f-84f7-d0da09dc42aa/7634/c:\SharedFiles\7634\1

它使用 windows 路径作为文件名,因为它无法破解它。如果可能的话,我尽量避免花哨的逻辑来检测路径类型。

最佳答案

因此,根据您的编辑,您可能需要检测给定 URI 处的文件系统,可能需要查看以下内容:

http://docs.oracle.com/javase/7/docs/api/java/nio/file/FileSystems.html

关于java - Window/linux 路径组件分离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16063729/

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