// 取source文件夹的绝对路径,取到为 E:\eclipse\workspace\tryBufferedReader\source
String sourceFilePath = new File("source").getAbsolutePath();
System.out.println(sourceFilePath);
String find = "E:\\eclipse";
String targetDir = "E:\\target";
sourceFilePath.replaceFirst(find, targetDir);
System.out.println(sourceFilePath); 我想把取到的绝对路径E:\eclipse\workspace\tryBufferedReader\source的前面部分替换掉E:\eclipse\workspace\tryBufferedReader\source --->E:\target\workspace\tryBufferedReader\source但是我这样写代码,没有发生任何的替换动作,有谁知道应该怎么写吗?
sourceFilePath = sourceFilePath.replaceFirst(find, targetDir);我觉得replaceFirst这个函数根本没有发生作用,通过我定义的那个find似乎找不到匹配的字符串。我想是我的find变量定义错了,关于那个\应该要做一些处理的吧
String sourceFilePath = "E:\\eclipse\\123";
System.out.println(sourceFilePath);
String find = "E:\\\\eclipse";
String targetDir = "E:\\\\target";
sourceFilePath = sourceFilePath.replaceFirst(find, targetDir);
System.out.println(sourceFilePath);
sourceFilePath的值不应该看做正则,所以是匹配两个\\,用正则就变成了4个斜杠。
我这样理解对吧。
谢谢
最后想到一个办法。虽然很愚笨,但是只能先暂时解决这个问题了。
先用split("//// ")将路径分开,然后再拼凑起来。
很笨吧?