我正在尝试读取文件内容,然后更改一些内部文本,然后复制到新位置。
在java 1.7下运行此代码,代码创建文件但无法用newName替换内部内容。
if (file.isFile()) {
Charset charset = StandardCharsets.UTF_8;
String content = new String(Files.readAllBytes(file.toPath()), charset);
content.replaceAll("(?i)" + oldName, newName);
String newFileName = file.getAbsolutePath().replace(oldName, newName);
File newFile = new File(newFileName);
newFile.getParentFile().mkdirs();
newFile.createNewFile();
Files.write(newFile.toPath(), content.getBytes());
}
最佳答案
字符串 content
不会被 replaceAll
函数更改。您必须将其返回值保存为新字符串并使用它。
关于java - 复制字符串文件并更改一些文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33451262/