title: Converting List into InputStream without creating an intermediate file
body: <p>I'm using a function which will get the input as List and className in which I convert the List as a delimiter file(Intermediate) then convert the file as InputStream as a return value.</p>
  
  <p>Here creating an intermediate file and deleting is a tedious process as this will increase the time and performance.</p>
  
  <p>So is there is another way in which we can directly convert the data from <code>List&lt;Class&gt;</code> into a delimiter with the headers (maybe we can Store it in <code>List&lt;Object&gt;</code>) then without creating an intermediate file we can directly convert it into InputStream.</p>
  
  <pre><code>&lt;T&gt; InputStream createInputStream(String csvFileName, List&lt;T&gt; listObject, Class className, char csvPreference) {
  
          ICsvBeanWriter beanWriter = null;
          File file=new File(csvFileName);
          InputStream inputStream=null;
          try {
              CsvPreference DELIMITER = new CsvPreference.Builder('"', csvPreference, "\n").build();
  
              beanWriter = new CsvBeanWriter(new FileWriter(file), DELIMITER);
              List&lt;String&gt; fieldList = Arrays.stream(className.getDeclaredFields()).map(Field::getName).collect(Collectors.toList());
              CellProcessor[] processors = new CellProcessor[fieldList.size()];
              for (int i = 0; i &lt; fieldList.size(); i++) {
                  processors[i] = new Optional();
              }
              String[] header = new String[fieldList.size()];
              fieldList.toArray(header);
              beanWriter.writeHeader(header);
  
              for (T obj : listObject) {
                  beanWriter.write(obj, header, processors);
              }
  
          } catch (IOException ex) {
              System.err.println("Error writing the CSV file: " + ex);
          } finally {
              if (beanWriter != null) {
                  try {
                      beanWriter.close();
                      inputStream=new FileInputStream(file);
                      file.delete();
                  } catch (IOException ex) {
                      System.err.println("Error closing the writer: " + ex);
                  }
              }
          }
          return inputStream;
      }
  </code></pre>
  
  <h2>Update 1:</h2>
  
  <p><strong>Users Class</strong></p>
  
  <pre><code>public class Users {
  
      String id;
  
      String name;
  
      String gender;
  
  }
  </code></pre>
  
  <p><strong>Adding elements in List</strong></p>
  
  <pre><code>List&lt;Users&gt; userList=new ArrayList&lt;Users&gt;();
  userList.add(new Users("1","AA","Male"));
  userList.add(new Users("2","BB","Male"));
  userList.add(new Users("3","CC","Female"));
  </code></pre>
  
  <p><strong>Here how I'm currently calling</strong></p>
  
  <pre><code>InputStream inputStream=createInputStream("demo.csv",userList,Users.Class,'|');
  </code></pre>
  
  <p>Here the Intermediate file(demo.csv) will be like,</p>
  
  <pre><code>id|name|gender
  1|AA|Male
  2|BB|Male
  3|CC|Female
  </code></pre>
  
  <p>From this file, I'll convert into inputstream</p>
  
  <p>Thanks in Advance,<br> Jay</p>