title: Java FileInputStream out of memory issue
body: <p>I am trying to read an excel file and then write to csv file using xssf .I am getting out of memory error(Heap space). i see that fileinputstream is good for memory management ,but still i see the issue</p>
  
  <pre><code>package xlsxtocsv;
  import java.io.File;
  import java.io.FileInputStream;
  import java.io.FileNotFoundException;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.util.Locale;
  import org.apache.poi.ss.usermodel.DataFormatter;
  import org.apache.poi.xssf.usermodel.XSSFRow;
  import org.apache.poi.xssf.usermodel.XSSFSheet;
  import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  
  public class xlsxtocsv
  {
  
      private static final String NEW_LINE_CHARACTER="\r\n";
  
      /**
       * Write the string into a text file 
       * @param csvFileName
       * @param csvData
       * @throws Exception
       */
      private static void writeCSV(String csvFileName,String csvData) throws Exception{
          FileOutputStream writer = new FileOutputStream(csvFileName);
          writer.write(csvData.getBytes());
          writer.close();
          System.out.println("Sucessfully written data to "+csvFileName);
      }
  
  
      public static void excelXToCSVfile(String excelFileName,String csvFileName,String Field_Delimiter,int Sheet_Number) {
           checkValidFile(excelFileName);
           XSSFWorkbook myWorkBook;
          try {
              myWorkBook = new XSSFWorkbook(new FileInputStream(excelFileName));
  
              XSSFSheet mySheet = myWorkBook.getSheetAt(Sheet_Number);
               String csvData="";
               DataFormatter formatter = new DataFormatter(Locale.US);
               checkValidFile(excelFileName);
  
               int rows = mySheet.getPhysicalNumberOfRows();
               String prefix="\"";
  
               for (int eachRow = 0;eachRow&lt;rows;eachRow++) {
                   XSSFRow myRow = (XSSFRow) mySheet.getRow(eachRow);
                       for ( int i=0;i&lt;myRow.getLastCellNum();i++){
                           if(i==0)
                           {
  
                               csvData += prefix+formatter.formatCellValue(myRow.getCell(i))+prefix;
  
                           }
                           else
                           {
                               csvData += Field_Delimiter+prefix+formatter.formatCellValue(myRow.getCell(i))+prefix;
  
                           }
  
  
                       }
                       csvData+=NEW_LINE_CHARACTER;
               }
  
  
  
  
                   try {
                      writeCSV(csvFileName, csvData);
                  } catch (Exception e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
  
          } catch (FileNotFoundException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
  
  
  
           }
  
  
      /**
       * get Cell value from XLSX file column
       * @param myCell
       * @return
       * @throws Exception
       */
  
          private static void checkValidFile(String fileName){
          boolean valid=true;
          try{
              File f = new File(fileName);
              if ( !f.exists() || f.isDirectory() ){
                  valid=false;
              }
          }catch(Exception e){
              valid=false;
          }
          if ( !valid){
              System.out.println("File doesn't exist: " + fileName);
              System.exit(0);
          }
      }
      public static void main(String[] args) throws Exception
      {
          String inp_file_name="";
          String Output_file_name="";
          String delimiter=",";
  
  
          //inp_file_name=args[0];    
          //Output_file_name=args[1]; 
  
      enter code here
          //delimiter=args[2];
  
  
          inp_file_name="C:/Users/xxx/Desktop/cloudera_shared/test_data.xlsx";
          Output_file_name="C:/Users/xxx/Desktop/cloudera_shared/test_data.csv";
          delimiter="|";
  
  
  
      if(args.length==4 &amp;&amp; (args[3].equals("") == false))
      {
  
          int Sheet_Number=Integer.parseInt(args[3]); 
          excelXToCSVfile(inp_file_name,Output_file_name,delimiter,Sheet_Number);
  
  
      }
      else
      {
  
          excelXToCSVfile(inp_file_name,Output_file_name,delimiter,0);
      }
  
      }
  
  }
  </code></pre>