【问题标题】:reconstructing a file path in java在java中重建文件路径
【发布时间】:2013-07-25 23:46:56
【问题描述】:

我有这样的文件路径位置:

Properties readProp = \\192.168.41.84\dev\config\dev\config.properties

我该如何操作它,所以我删除了 config.properties 的部分 并将其替换为 test\config.properties

所以新的属性位置将是:

Properties readProp = \\192.168.41.84\dev\config\dev\test\newconfig.properties

?

感谢您的时间和精力

【问题讨论】:

  • 你能创建SSCCE吗?

标签: java file properties filepath


【解决方案1】:

确保在构建字符串时转义所有反斜杠。

String path = "\\\\192.168.41.84\\dev\\config\\dev\\config.properties";
System.out.println(path);
int lastBackSlash = path.lastIndexOf("\\");
//+1 to include lastBackSlash
String newPath = path.substring(0, lastBackSlash + 1) + "test" + path.substring(lastBackSlash);
System.out.println(newPath);

打印

\\192.168.41.84\dev\config\dev\config.properties
\\192.168.41.84\dev\config\dev\test\config.properties

这样的文章也值得一读。将路径视为字符串可能很危险。

http://twistedoakstudios.com/blog/Post4872_dont-treat-paths-like-strings

但是,如果您小心,知道您的字符串函数的行为方式(或查找它们),并且您不会出现 1 个错误...然后像字符串一样处理路径应该是无痛的。但是您无法保证路径是有效的......而路径构建器库会为您提供保证。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-04
    • 1970-01-01
    • 1970-01-01
    • 2018-01-20
    • 2015-05-14
    • 2015-06-11
    相关资源
    最近更新 更多