java - getPath() 不返回斜杠? ( window )

标签 java

我正在制作一个涉及编辑文件并用路径替换占位符的程序。

代码如下:

List<String> lines = Files.readAllLines(new File(new File(basepath, "Game"), "launcher_profiles.json").toPath());
        int index = -1;
        for (String s : lines){
            index++;
            if (s.contains("PROGRAM/GAMEDIRPATH")) break;
        }

        String k = lines.get(index);
        k = k.replaceAll("PROGRAM/GAMEDIRPATH", new File(basepath, "Game").getPath());
        lines.set(index, k);
        clearFile(new File(new File(basepath, "Game"), "launcher_profiles.json"));
        Files.write(new File(new File(basepath, "Game"), "launcher_profiles.json").toPath(), lines, StandardOpenOption.CREATE);

基本路径“游戏”文件路径没有问题,因为在我程序的所有其他地方,它都包含斜杠。仅在这部分替换不包含斜杠的占位符。

例如,而不是 C:/Users\name\Documents\program\Game,返回UsersnameDocumentsprogramGame。

正如我之前所说,在所有其他地方,它返回正确的路径名(带斜线)。 在 Mac 上,所有斜杠都在那里,即使在这一部分也是如此。

有人知道解决办法吗?谢谢。

最佳答案

来自 String.replaceAll 方法的文档:

Note that backslashes (\) and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string; see Matcher.replaceAll. Use Matcher.quoteReplacement to suppress the special meaning of these characters, if desired.

关于java - getPath() 不返回斜杠? ( window ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44810727/

相关文章:

java - 严重: Allocate exception for servlet myServletName

java - 将字符串(各种长单词)对齐到列 -\t 在 html 中不起作用

java - 删除两个字符之间的子字符串

java - 为什么 ListView 没有显示在 fragment 中?

java - IBM Websphere 7,支持 JAX-WS Service 2.2,这可能吗?

java - while循环大括号{}代码错误

java - 无法在填充的矩形上渲染图像

java - Protocol Buffer : raw binary data to json

java - 如何将一个 Spring Boot 应用程序作为依赖项包含在另一个应用程序中

java - 错误: Could not find method android() for arguments