title: Effective Way to Write RxJava Emissions to Text File?
body: <p>What is the most effective way to write an <code>Observable&lt;T&gt;</code> of items into a text file, where a line is written for each emission? I am using a <code>try-with-resources</code> setup below with a <code>CountDownLatch</code>, but it definitely feels like an anti-pattern due to the blocking. There is also a strong possibility of interruptions and early unsubscriptions throwing errors. </p>
  
  <pre><code> private void saveToCSV(String url) {
  
          CountDownLatch latch = new CountDownLatch(1);
  
          File outputFile = new File(url);
  
          try(BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile))) {
  
              writer.write(ReportItem.getHeaders() + "\r\n");
  
              reportItems.forDate(dt)
                      .map(ReportItem::toCSVLine).map(s -&gt; s.concat("\r\n"))
                      .subscribe(Checked.a1(writer::write), Throwable::printStackTrace, latch::countDown);
  
              latch.await(); 
  
          } catch (Exception e) {
              e.printStackTrace();
          }
  }
  </code></pre>