title: How to use java.nio.channels.FileChannel to write a byte[] to a file - Basics
body: <p>I do not have experience using Java channels.  I would like to write a byte array to a file.  Currently, I have the following code:</p>
  
  <pre><code>String outFileString = DEFAULT_DECODED_FILE; // Valid file pathname
  FileSystem fs = FileSystems.getDefault();
  Path fp = fs.getPath(outFileString);
  
  FileChannel outChannel = FileChannel.open(fp, EnumSet.of(StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE));
  
  // Please note: result.getRawBytes() returns a byte[]
  ByteBuffer buffer = ByteBuffer.allocate(result.getRawBytes().length);
  buffer.put(result.getRawBytes());
  
  outChannel.write(buffer); // File successfully created/truncated, but no data
  </code></pre>
  
  <p>With this code, the output file is created, and truncated if it exists.  Also, in the IntelliJ debugger, I can see that <code>buffer</code> contains data.  Also, the line <code>outChannel.write()</code> is successfully called without throwing an exception.  However, after the program exits, the data does not appear in the output file.</p>
  
  <p>Can somebody (a) tell me if the FileChannel API is an acceptable choice for writing a byte array to a file, and (b) if so, how should the above code be modified to get it to work?</p>