how can i replace specified strings in text files by java?

In general you can do it in 3 steps:

  1. Read file and store it in String
  2. Change the String as you need (your “username,password…” modification)
  3. Write the String to a file

You can search for instruction of every step at Stackoverflow.

Here is a possible solution working directly on the Stream:

public static void main(String[] args) throws IOException {

    String inputFile = "C:\\Users\\geheim\\Desktop\\lines.txt";
    String outputFile = "C:\\Users\\geheim\\Desktop\\lines_new.txt";

    try (Stream<String> stream = Files.lines(Paths.get(inputFile));
            FileOutputStream fop = new FileOutputStream(new File(outputFile))) { -> line += " manipulate line as required\n").forEach(line -> {
            try {
            } catch (IOException e) {

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top