title: Writing Multiple CSV Files From Excel using Java and Jexcelapi
body: <p>I'm trying to create a program that writes all the data from excel to multiple csv files. </p>
  
  <p>Currently, my program creates <em>n</em> number of files, and only the last row of excel is being written on the last csv file.</p>
  
  <h3>Programming Background Observation:</h3>
  
  <p>It seems like the file is writing. But for each written row, it somehow creates the same file 250 times. It erases the row that was written last. </p>
  
  <p>I may have some problems using the <strong>OutputStream</strong> or <strong>BufferedWriter</strong>. I just can't figure out what it is.</p>
  
  <p><strong>writeRow() method:</strong> </p>
  
  <pre><code>public static void writeRow(BufferedWriter bw, Cell[] row) throws IOException { 
      if (row.length &gt; 0) {
          bw.write(row[0].getContents());
          for (int j = 1; j &lt; row.length; j++) {
              bw.write(',');
              bw.write(row[j].getContents());
          }
      }
  }
  </code></pre>
  
  <p><strong>setFile() method:</strong></p>
  
  <pre><code>public static BufferedWriter setFile (int i) throws IOException {
      i=i/250;
      File f = new File(dir + "file-" + (i+1) + ".csv");
      // If i has changed, create a new file. Else, append to current file.
      String encoding = "UTF8";
      OutputStream os = null;
      OutputStreamWriter osw = null;
      BufferedWriter bw = null;
      Boolean append = null;
      try {
          // If i has changed, create a new file, else append.
          if (i%250==0) {
              append = new Boolean("TRUE");
              os = new FileOutputStream(f,append);
          } else {
              append = new Boolean("FALSE");
              os = new FileOutputStream(f, append);
          }
          osw = new OutputStreamWriter(os, encoding);
          bw = new BufferedWriter(osw);
  
      } finally {
          os.close();
          osw.close();
      }
      return bw;
  }
  </code></pre>
  
  <p><strong>Here are the exceptions.</strong> </p>
  
  <pre><code>Exception in thread "main" java.io.IOException: Stream closed
  at sun.nio.cs.StreamEncoder.ensureOpen(Unknown Source)
  at sun.nio.cs.StreamEncoder.write(Unknown Source)
  at java.io.OutputStreamWriter.write(Unknown Source)
  at java.io.BufferedWriter.flushBuffer(Unknown Source)
  at java.io.BufferedWriter.flush(Unknown Source)
  at Main.main(Main.java:46)
  </code></pre>
  
  <p>Below are some of the links to the code. </p>
  
  <p>What am I doing wrong?</p>
  
  <p>Here is the svn trunk:</p>
  
  <p><a href="https://franz-opensource.googlecode.com/svn/trunk/ExcelToCsv" rel="nofollow">https://franz-opensource.googlecode.com/svn/trunk/ExcelToCsv</a> - </p>
  
  <ul>
  <li><a href="http://code.google.com/p/franz-opensource/source/browse/trunk/ExcelToCsv/src/Main.java" rel="nofollow">Click here for the Java Main code</a> like the one above.</li>
  <li><a href="http://code.google.com/p/franz-opensource/downloads/detail?name=inventory.xls&amp;can=2&amp;q=" rel="nofollow">Click here to download the sample excel file</a></li>
  <li><a href="http://sourceforge.net/projects/jexcelapi/files/jexcelapi/2.6.12/jexcelapi_2_6_12.zip/download" rel="nofollow">Click here for the jexcelapi download</a></li>
  </ul>