Note that there are some explanatory texts on larger screens.

plurals
  1. POCasting a SeekableByteChannel to a FileChannel
    text
    copied!<p>Directly from <a href="http://docs.oracle.com/javase/tutorial/essential/io/rafs.html" rel="nofollow">this</a> oracle java tutorial: </p> <blockquote> <p>The following code snippet opens a file for both reading and writing by using one of the newByteChannel methods. The SeekableByteChannel that is returned is cast to a FileChannel.</p> </blockquote> <p>This is the snippet they are talking about present in the same link I above mentioned.</p> <pre><code>String s = "I was here!\n"; byte data[] = s.getBytes(); ByteBuffer out = ByteBuffer.wrap(data); ByteBuffer copy = ByteBuffer.allocate(12); try (FileChannel fc = (FileChannel.open(file, READ, WRITE))) { // Read the first 12 // bytes of the file. int nread; do { nread = fc.read(copy); } while (nread != -1 &amp;&amp; copy.hasRemaining()); // Write "I was here!" at the beginning of the file. fc.position(0); while (out.hasRemaining()) fc.write(out); out.rewind(); // Move to the end of the file. Copy the first 12 bytes to // the end of the file. Then write "I was here!" again. long length = fc.size(); fc.position(length-1); copy.flip(); while (copy.hasRemaining()) fc.write(copy); while (out.hasRemaining()) fc.write(out); } catch (IOException x) { System.out.println("I/O Exception: " + x); } </code></pre> <p>So basically they are talking about a Files.newByteChannel() method which returns a SeekableByteChannel object which in turn gets cast to a FileChannel. Well, I do not see this process. Is it hidden/running on the background/ or anyother sourcery magic kind of thing? Thanks in advance.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload