How to use NIO to write InputStream to File?

Tapas Bose picture Tapas Bose · May 16, 2013 · Viewed 28.7k times · Source

I am using following way to write InputStream to File:

private void writeToFile(InputStream stream) throws IOException {
    String filePath = "C:\\Test.jpg";
    FileChannel outChannel = new FileOutputStream(filePath).getChannel();       
    ReadableByteChannel inChannel = Channels.newChannel(stream);
    ByteBuffer buffer = ByteBuffer.allocate(1024);
    
    while(true) {
        if(inChannel.read(buffer) == -1) {
            break;
        }
        
        buffer.flip();
        outChannel.write(buffer);
        buffer.clear();
    }
    
    inChannel.close();
    outChannel.close();
}

I was wondering if this is the right way to use NIO. I have read a method FileChannel.transferFrom, which takes three parameter:

  1. ReadableByteChannel src
  2. long position
  3. long count

In my case I only have src, I don't have the position and count, is there any way I can use this method to create the file?

Also for Image is there any better way to create image only from InputStream and NIO?

Any information would be very useful to me. There are similar questions here, in SO, but I cannot find any particular solution which suites my case.

Answer

Evgeniy Dorofeev picture Evgeniy Dorofeev · May 16, 2013

I would use Files.copy

Files.copy(is, Paths.get(filePath));

as for your version

  1. ByteBuffer.allocateDirect is faster - Java will make a best effort to perform native I/O operations directly upon it.

  2. Closing is unreliable, if first fails second will never execute. Use try-with-resources instead, Channels are AutoCloseable too.