title: Best way to write String to file using java nio
body: <p>I need to write(append) huge string to flat file using java nio. The encoding is ISO-8859-1. </p>
  
  <p>Currently we are writing as shown below. Is there any <strong>better</strong> way to do the same ?</p>
  
  <pre><code>public void writeToFile(Long limit) throws IOException{
       String fileName = "/xyz/test.txt";
       File file = new File(fileName);        
       FileOutputStream fileOutputStream = new FileOutputStream(file, true);  
       FileChannel fileChannel = fileOutputStream.getChannel();
       ByteBuffer byteBuffer = null;
       String messageToWrite = null;
       for(int i=1; i&lt;limit; i++){
           //messageToWrite = get String Data From database
           byteBuffer = ByteBuffer.wrap(messageToWrite.getBytes(Charset.forName("ISO-8859-1")));
           fileChannel.write(byteBuffer);         
       }
       fileChannel.close();
  }
  </code></pre>
  
  <p>EDIT: Tried both options. Following are the results.</p>
  
  <pre><code>@Test
  public void testWritingStringToFile() {
      DiagnosticLogControlManagerImpl diagnosticLogControlManagerImpl = new DiagnosticLogControlManagerImpl();
      try {
          File file = diagnosticLogControlManagerImpl.createFile();
          long startTime = System.currentTimeMillis();
          writeToFileNIOWay(file);
          //writeToFileIOWay(file);
          long endTime = System.currentTimeMillis();
          System.out.println("Total Time is  " + (endTime - startTime));
      } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  }
  
  /**
   *
   * @param limit
   *            Long
   * @throws IOException
   *             IOException
   */
  public void writeToFileNIOWay(File file) throws IOException {
      FileOutputStream fileOutputStream = new FileOutputStream(file, true);
      FileChannel fileChannel = fileOutputStream.getChannel();
      ByteBuffer byteBuffer = null;
      String messageToWrite = null;
      for (int i = 1; i &lt; 1000000; i++) {
          messageToWrite = "This is a test üüüüüüööööö";
          byteBuffer = ByteBuffer.wrap(messageToWrite.getBytes(Charset
              .forName("ISO-8859-1")));
          fileChannel.write(byteBuffer);
      }
  }
  
  /**
   *
   * @param limit
   *            Long
   * @throws IOException
   *             IOException
   */
  public void writeToFileIOWay(File file) throws IOException {
      FileOutputStream fileOutputStream = new FileOutputStream(file, true);
      BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(
          fileOutputStream, 128 * 100);
      String messageToWrite = null;
      for (int i = 1; i &lt; 1000000; i++) {
          messageToWrite = "This is a test üüüüüüööööö";
          bufferedOutputStream.write(messageToWrite.getBytes(Charset
              .forName("ISO-8859-1")));
      }
      bufferedOutputStream.flush();
      fileOutputStream.close();
  }
  
  private File createFile() throws IOException {
      File file = new File(FILE_PATH + "test_sixth_one.txt");
      file.createNewFile();
      return file;
  }
  </code></pre>
  
  <p><strong>Using ByteBuffer and Channel: took 4402 ms</strong></p>
  
  <p><strong>Using buffered Writer : Took 563 ms</strong></p>