title: Java 8: Supplier working with files throws "stream has already been operated upon or closed"
body: <p>Working with <code>JUnit</code> and <code>Stream</code> I have the following error message:</p>
  
  <pre><code>java.lang.IllegalStateException: stream has already been operated upon or closed
  </code></pre>
  
  <p>I did a research, and is clear is <strong>not</strong> possible reuse a stream</p>
  
  <p><strong>But</strong> according with this post:</p>
  
  <ul>
  <li><a href="https://www.mkyong.com/java8/java-stream-has-already-been-operated-upon-or-closed" rel="nofollow noreferrer">Java – Stream has already been operated upon or closed</a></li>
  </ul>
  
  <p>working with <code>Supplier</code> is possible work around with this problem.</p>
  
  <p>So my current code is the following:</p>
  
  <pre><code>    try (Stream&lt;String&gt; stream =  Files.lines(Paths.get(fileName)) ) {
  
          Supplier&lt;Stream&lt;String&gt;&gt; supplier = () -&gt; stream;
  
          logger.info("A");
          logger.info("ABC {}", supplier.get().findFirst().get());
          logger.info("B");
          logger.info("XYZ {}", supplier.get().skip(1050).findFirst().get());
          logger.info("C");
  
          assertThat(supplier.get().count(), is(1051));
  
      }
      catch (IOException e) {
          logger.error("{}", e.getMessage());
      }
  </code></pre>
  
  <p>How you can see I use the <code>supplier.get()</code> to work with the <code>Stream</code> (<em>it according with the tutorial</em>), but the <code>@Test</code> prints until <strong>B</strong>, therefore the <code>@Test</code> fails in <code>supplier.get().skip(1050).findFirst().get()</code> and it is still generating the same error message. </p>
  
  <p>The unique difference between my code and the tutorial, the mime works through a File and the tutorial works around an array.</p>
  
  <p>Something special to edit to work without any problem?</p>
  
  <p><strong>Alpha</strong></p>
  
  <p>I did the following edition (according with the snippet code of Eugene)</p>
  
  <pre><code>   try (Stream&lt;String&gt; stream =  Files.lines(Paths.get(fileName)) ) {
  
          Supplier&lt;Stream&lt;String&gt;&gt; supplier = () -&gt; stream.collect(Collectors.toList()).stream();
  
          logger.info("A");
          logger.info("ABC {}", supplier.get().findFirst().get());
          logger.info("B");
          logger.info("XYZ {}", supplier.get().skip(1050).findFirst().get());
          logger.info("C");
  
          assertThat(supplier.get().count(), is(1051));
  
      }
  </code></pre>
  
  <p>Same error message.</p>