0

I have a FileChannel object (which I need so I can apply a FileLock to the file), and I want to write to the file it references using a simple OututStream. How do I create an OutputStream that writes to a FileChannel?

This should be in the Java API somewhere, but I can't find it. FileChannel does not have a suitable factory method. The FileOutputStream class does not have a suitable constructor. The Files class (of Java 7) has no suitable methods.

Raedwald
  • 46,613
  • 43
  • 151
  • 237
  • I am answering my own question [as is encouraged](http://blog.stackoverflow.com/2011/07/its-ok-to-ask-and-answer-your-own-questions/). – Raedwald May 12 '14 at 16:41

1 Answers1

0

The Java API provides suitable factory methods in the Channels class. The factory method newOutputStream(WritableByteChannel) can create an OutputStream for a given FileChannel, as a FileChannel implements WritableByteChannel.

Raedwald
  • 46,613
  • 43
  • 151
  • 237