1
Я пытаюсь создать программу, которая удаляет текст из файла. Текст для удаления и путь к файлу предоставляются в виде аргументов командной строки. Все идет хорошо, но когда я открываю файл после завершения работы программы, все пусто. Что я делаю не так?Файл выводит все содержимое
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class Remove {
public static void main(String[] args) throws IOException{
if(args.length != 2) {
System.out.println("usage : java Remove stringToRemove filePath");
System.exit(1);
}
String stringToReplace = args[0];
String path = args[1];
File file = new File(path);
if(!file.exists()) {
System.out.println("No such file exists!");
System.exit(2);
}
Scanner input = new Scanner(file);
PrintWriter output = new PrintWriter(file);
while(input.hasNext()) {
String currentLine = input.nextLine();
currentLine = currentLine.replaceAll(stringToReplace, "");
output.println(currentLine);
}
input.close();
output.close();
System.out.println("Operation Successful");
}
}
Ахан! Спасибо ... – Saud
Да ............ – Saud