title: Java Stream API: Modify a specific line in a file
body: <p>I am reading the content of a <code>readme</code> file as a list of strings</p>
  <p>And I want to modify a specific line of this file (so a specific string of the list).</p>
  <p>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)?</p>
  <p>Other code I found showed how to create a new List and add new strings to this list after performing a <code>replaceAll()</code> or appending certain characters.</p>
  <p>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).</p>
  <p>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 <code>readme</code> file. Just looking for a &quot;stream way&quot; of doing so.</p>
  <p>My actual code:</p>
  <pre class="lang-java prettyprint-override"><code>private static List&lt;String&gt; getReadme() {
    try {
      return stream(readFileToString(new File(&quot;readme.md&quot;)).split(&quot;\n&quot;)).collect(Collectors.toList());
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }
  
  public static void generateReadMe(String day, String year, int part) {
    List&lt;String&gt; readme = getReadme();
  
    // hidden code where I create the line I want to end up modified...
    String line = prefix + fDay + stars + color + packages + fYear + &quot;/days/Day&quot;+ day +&quot;.java)\n&quot;;
  
    List&lt;String&gt; newReadme = readme.stream().map(l -&gt; {
      if (l.contains(fYear) &amp;&amp; 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);
  }
  </code></pre>