简体   繁体   中英

Java Stream API: Modify a specific line in a file

I am reading the content of a readme file as a list of strings

And I want to modify a specific line of this file (so a specific string of the list).

I managed to achieve it, but is there an elegant way to do this with (only) java stream operations (as I am currently learning the java Stream API)?

Other code I found showed how to create a new List and add new strings to this list after performing a replaceAll() or appending certain characters.

My case is a bit different, and I'd like to change only a certain line I am finding in the file (and more specifically certain characters of it, even though I am fine with re-writing the whole line). For now, I just re-write all lines one by one in a new list, except for one line that I generate and write instead (so the line I wanted to modify).

I'm open to any way of doing so, I can change the i/o types, re-write the whole file/line or just change a part of it... as long as I end up with a modified readme file. Just looking for a "stream way" of doing so.

My actual code:

private static List<String> getReadme() {
  try {
    return stream(readFileToString(new File("readme.md")).split("\n")).collect(Collectors.toList());
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
}

public static void generateReadMe(String day, String year, int part) {
  List<String> readme = getReadme();

  // hidden code where I create the line I want to end up modified...
  String line = prefix + fDay + stars + color + packages + fYear + "/days/Day"+ day +".java)\n";

  List<String> newReadme = readme.stream().map(l -> {
    if (l.contains(fYear) && l.contains(fDay)) {
      // add the modified line (aka replacing the line)
      return line;
    } else {
      // or add the non modified line if not the one we're looking for
      return l;
    }
  }).toList();

  newReadme.forEach(System.out::println);
}

You wouldn't need Apache commons, I'd suggest using Files.lines of java.nio.file.Files , also you can have the implementation one single method like below

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Example {

    public static void generateReadMe(String day, String year, int part) {
        List<String> newReadme = null;

        try (Stream<String> lines = Files.lines(Paths.get("readme.md"))) {
             newReadme = lines.map(l -> l.contains(fYear) && l.contains(fDay) ? replaceLine() : l).collect(Collectors.toList())
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        newReadme.forEach(System.out::println);
    }

    private static String replaceLine() {
        //Your implementation
        return prefix + fDay + stars + color + packages + fYear + "/days/Day" + day + ".java)\n";
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM