Java中如何用输入的新的字符串替换一个旧的字符串,将改变后的文本保存到一个新的文件中。
参考下
String oldStr = "test";
Scanner scanner = new Scanner(System.in);
oldStr = scanner.nextLine();
File file = new File("D:/tmp", "word.txt");
try {
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream output = new FileOutputStream(file);
output.write(oldStr.getBytes());
output.close();
} catch (IOException e) {
e.printStackTrace();
}
字符串有replace方法,直接替换,
然后用io写入文件