我目前正在尝试编写一个程序,该程序可以扫描文本文档并用另一个短语替换指定的单词/字符串/任何内容,特别是使用 Scanner 和 Printwriter 类。不幸的是,我在寻找正确的使用方法以及如何实现它们时遇到了一些麻烦。这是我的代码:
class Redaction {
public static void main(String[] args) throws FileNotFoundException {
Scanner input = new Scanner(System.in);
System.out
.println("Please enter the filename of the sensitive information");
String f = input.next();
System.out.println("Please input what text you want 'lost'");
String o = input.next();
System.out
.println("Please input what you want the new, improved filename to be called");
String n = input.next();
File sensitiveDocument = new File(f);
if (!sensitiveDocument.exists()) {
System.out.println("File does not exist.");
System.exit(0);
}
Scanner in = new Scanner(sensitiveDocument);
in.useDelimiter("[^A-Za-z]+");
PrintWriter out = new PrintWriter(n);
while (in.hasNext()) {
if (in.hasNext(o)) {
// ...
}
}
in.close();
out.close();
}
}
在这一点上我很迷茫。任何帮助将非常感激。