文件路径问题:/ -> \

Waq*_*qas 1 java string file path

            // Dividend Limit check or increase the Dividend
        if (dival == 10) {
            writer.println("Divident has reached it Limit !");
            i++;
            // update the file name
            String upath = "channel_" + i;
            System.out.println(path);
            // find channel_1 and replace with the updated path
            if (path.contains("channel_1")) {
                path = "D:/File Compression/Data/low_freq/low_freq/house_1/"
                        + upath + ".dat";
            } else {
                JOptionPane.showMessageDialog(null, "Invalid File Choosen");
                System.exit(0);
            }

            dival = 10;

        } else {
            dival = dival + 10;
            writer.println("Dividen:" + dival);
        }
Run Code Online (Sandbox Code Playgroud)

这些行采用递归方法。第一次给出正确的路径:

D:/File Compression/Data/low_freq/low_freq/house_1/channel_2.dat
Run Code Online (Sandbox Code Playgroud)

但是在第二次调用时,它将正斜杠翻转为反斜杠:

D:\File Compression\Data\low_freq\low_freq\house_1\channel_1.dat
Run Code Online (Sandbox Code Playgroud)

如果我不使用条件,它工作正常。

if(path.contains("channel_"))
Run Code Online (Sandbox Code Playgroud)

Mar*_*aux 5

那是因为File.seperatorWindows 中的\. 每次你让你的路径字符串通过一个java.io.File它会替换它们。所以要解决这个问题,要么不要使用 File 作为辅助工具,要么用正斜杠替换反斜杠。

因此,发生的情况是您的pathString 使用了反斜杠。您检索该字符串形式 ajava.io.File将在 Windows 上自动使用反斜杠。如果路径包含“channel_1”,则使用带有正斜杠的硬编码字符串覆盖整个字符串。