D
D
Dimastuy022021-05-06 11:46:53
Java
Dimastuy02, 2021-05-06 11:46:53

How to overwrite the .replace line?

Hello everyone, I don’t understand what is the reason)
The essence of the task is to find a line in the file with the same value as the variable and note this line.
The line is located but not overwritten in the file, maybe someone knows how to fix it)

Here is the code:

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class main {
    public static void main(String[] args) {
        String str = "qwerty";

        try (BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\***"))) {
            String line = br.readLine();

            while (line != null) {
                if (line.equals(str)) {
                    line = line.replace("qwerty", "str1");
                    System.out.println("Replaced " + line);

                }
                line = br.readLine();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Romses Panagiotis, 2021-05-06
@romesses

I see reading from a file. Where is the entry to another file?
Why is the file not closed?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question