Thursday, May 12, 2016

Write String in to CSV (Comma Separated Values) File in Java







Code Snippet :

       public static void writeStringAsCSV(String resultsetContentds,String filePath){
      FileOutputStream fileoutputStream = null;
         File fileInstance;
         try {
           fileInstance = new File(filePath);
              fileoutputStream = new FileOutputStream(fileInstance);
              if (!fileInstance.exists()) {
                fileInstance.createNewFile();
              }
              byte[] contentInBytes = resultsetContentds.getBytes();
              fileoutputStream.write(contentInBytes);
              fileoutputStream.flush();
              fileoutputStream.close();
         } catch (IOException exceptionTrace) {
           exceptionTrace.printStackTrace();
         } finally {
           try {
                if (fileoutputStream != null) {
                  fileoutputStream.close();
                }
              } catch (IOException exceptionTrace) {
                exceptionTrace.printStackTrace();
              }
         }

       }

No comments:

Post a Comment