title: Modify file using Files.lines
body: <p>I'd like to read in a file and replace some text with new text.  It would be simple using asm and int 21h but I want to use the new java 8 streams.</p>
  
  <pre><code>    Files.write(outf.toPath(), 
          (Iterable&lt;String&gt;)Files.lines(inf)::iterator,
          CREATE, WRITE, TRUNCATE_EXISTING);
  </code></pre>
  
  <p>Somewhere in there I'd like a <code>lines.replace("/*replace me*/","new Code()\n");</code>.  The new lines are because I want to test inserting a block of code somewhere.</p>
  
  <p>Here's a play example, that doesn't work how I want it to, but compiles.  I just need a way to intercept the lines from the iterator, and replace certain phrases with code blocks.</p>
  
  <pre><code>import java.io.IOException;
  import java.nio.file.Files;
  import java.nio.file.Paths;
  import static java.nio.file.StandardOpenOption.*;
  import java.util.Arrays;
  import java.util.stream.Stream;
  
  public class FileStreamTest {
  
      public static void main(String[] args) {
          String[] ss = new String[]{"hi","pls","help","me"};
          Stream&lt;String&gt; stream = Arrays.stream(ss);
  
          try {
              Files.write(Paths.get("tmp.txt"),
                      (Iterable&lt;String&gt;)stream::iterator,
                      CREATE, WRITE, TRUNCATE_EXISTING);
          } catch (IOException ex) {}
  
  //// I'd like to hook this next part into Files.write part./////
          //reset stream
          stream = Arrays.stream(ss);
          Iterable&lt;String&gt; it = stream::iterator;
          //I'd like to replace some text before writing to the file
          for (String s : it){
              System.out.println(s.replace("me", "my\nreal\nname"));
          }
      }
  
  }
  </code></pre>
  
  <p>edit: I've gotten this far and it works.  I was trying with filter and maybe it isn't really necessary.</p>
  
  <pre><code>        Files.write(Paths.get("tmp.txt"),
                   (Iterable&lt;String&gt;)(stream.map((s) -&gt; {
                      return s.replace("me", "my\nreal\nname");
                  }))::iterator,
                  CREATE, WRITE, TRUNCATE_EXISTING);
  </code></pre>