Can someone help me to resolve my issue?
I have problem that BufferedWriter does not write to text file.
Program compiles without errors.
int number_of_line_to_delete_in_file = 0,number_of_all_lines = 0;
System.out.println("Trimmer - Logów pocztowych");
System.out.println("Rafał Biel v0.5");
try {
File file_reader = new File("src/log.txt");
System.out.println("Lokalizacja pliku LOG: " + file_reader.getAbsolutePath());
File file_saver = new File("src/logTemp.txt");
BufferedReader reader = new BufferedReader(new FileReader(file_reader));
BufferedWriter writer = new BufferedWriter(new FileWriter(file_saver));
LineNumberReader lnr = new LineNumberReader(reader);
String line_to_delete = "aaa";
String checked_line,line_counter;
while ((line_counter = lnr.readLine()) != null){
if(line_counter.equals(line_to_delete)) {
number_of_line_to_delete_in_file++; // sprawdzenie ilośći linii do usunięcia
}
number_of_all_lines++;// sprawdzenie ilośći wszystkich linii w pliku
}
System.out.println("W pliku znajduje się " + number_of_all_lines + " lini, w tym do usunięcia " + number_of_line_to_delete_in_file + " linii.");
while((checked_line = reader.readLine()) != null) {
String trimmed_line = checked_line.trim();
if(trimmed_line.equals(line_to_delete)) continue;
writer.write("TEST");
} // składnia odpowiadająca za usunięcie napisu z pliku.
writer.close();//zamknięcie zapisu
reader.close();//zamkniecia odczytu
boolean successful = file_saver.renameTo(file_reader);
System.out.println("Pomyślnie zakończyłem pracę " + successful);
}catch (Exception e){
System.err.println("Błąd: " + e.getMessage());
}
File is read, but when I try to open the file saved in Temp the file is empty
Problem is that you are reading the whole file in first while loop. Your file pointer has already reached at EOF. Now you are again trying to read that file in next while loop. Since pointer is already at EOF, it will return null and your code will never go inside second while loop.
Below is dirty way to validate my comments. I am reopening your log.txt file to reset the pointer. It is not efficient but points to the mistake.