В настоящее время я пытаюсь написать программу, которая может сканировать текстовый документ и заменять указанное слово/строку/любую другую фразой, в частности, используя классы Scanner и PrintWriter. К сожалению, у меня есть немного проблем с поиском правильных методов использования и как именно их реализовать. Вот мой код:Поиск и замена слов в текстовом файле с помощью классов 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();
}
}
Я довольно потерян в этом пункте. Любая помощь приветствуется.
Но если '(in.hasNext (o))' в нем нет кода. Что вас смущает? –
Посмотрите мой ответ здесь: Надеюсь, это поможет!
http://stackoverflow.com/questions/7779265/find-and-replace-a-word-in-several-text-files-with-java/19246348#19246348
– Nikhil