0
我正在嘗試讀取文件內容,然後更改一些內部文本,然後複製到新位置。複製字符串文件並更改一些文本
在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());
}
你可以發佈一個示例輸入文件,結果應該是什麼? – Tunaki