Channel-Based I/O Supported by : java.nio java.nio.channels From jdk1.4+
File I/O using NIO <ul><li>Objects required : 3 </li></ul><ul><li>A file stream object </li></ul><ul><li>One or more buffe...
Buffers java.nio.Buffer MappedByteBuffer DoubleBuffer FloatBuffer LongBuffer ByteBuffer ShortBuffer IntBuffer java.lang.Ob...
Channel Hierarchy
FileChannel java.nio.channels.FileChannel java.nio.channels.spi.AbstractInterruptibleChannel GatheringByteChannel ByteChan...
java.nio.channels.ByteChannel <ul><li>Channel </li></ul><ul><li>boolean isOpen( ) </li></ul><ul><li>void close( ) throws I...
GatheringByteChannel & ScatteringByteChannel <ul><li>ScatteringByteChannel </li></ul><ul><li>long read (ByteBuffer[], int,...
Reading a File <ul><li>try{ </li></ul><ul><li>FileInputStream fis = new FileInputStream(&quot;story.txt&quot;); </li></ul>...
Writing to a File <ul><li>try{ </li></ul><ul><li>FileOutputStream fos = new FileOutputStream(&quot;story.txt&quot;); </li>...
 
 
 
 
 
 
Upcoming SlideShare
Loading in...5
×

Channel Based Io

1,068

Published on

Published in: Technology, News & Politics
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,068
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

Channel Based Io

  1. 1. Channel-Based I/O Supported by : java.nio java.nio.channels From jdk1.4+
  2. 2. File I/O using NIO <ul><li>Objects required : 3 </li></ul><ul><li>A file stream object </li></ul><ul><li>One or more buffer objects </li></ul><ul><li>A channel object </li></ul><ul><li>Buffers are used to put the data to be written to a file or from which we can get the data that was put into. </li></ul><ul><li>A channel object provides the connection to the file and does the reading or writing of the data using one or more buffer objects. </li></ul>
  3. 3. Buffers java.nio.Buffer MappedByteBuffer DoubleBuffer FloatBuffer LongBuffer ByteBuffer ShortBuffer IntBuffer java.lang.Object CharBuffer
  4. 4. Channel Hierarchy
  5. 5. FileChannel java.nio.channels.FileChannel java.nio.channels.spi.AbstractInterruptibleChannel GatheringByteChannel ByteChannel ScatteringByteChannel java.lang.Object
  6. 6. java.nio.channels.ByteChannel <ul><li>Channel </li></ul><ul><li>boolean isOpen( ) </li></ul><ul><li>void close( ) throws IOException </li></ul><ul><li>WritableByteChannel </li></ul><ul><li>int write(java.nio.ByteBuffer) </li></ul><ul><li>ReadableByteChannel </li></ul><ul><li>int read(java.nio.ByteBuffer) </li></ul><ul><li>ByteChannel : A tag interface </li></ul>ByteChannel WritableByteChannel ReadableByteChannel Channel
  7. 7. GatheringByteChannel & ScatteringByteChannel <ul><li>ScatteringByteChannel </li></ul><ul><li>long read (ByteBuffer[], int, int) </li></ul><ul><li>long read (ByteBuffer) </li></ul><ul><li>GatheringByteChannel </li></ul><ul><li>long write(ByteBuffer[], int, int) </li></ul><ul><li>long write(ByteBuffer) </li></ul>GatheringByteChannel ScatteringByteChannel ReadableByteChannel WritableByteChannel
  8. 8. Reading a File <ul><li>try{ </li></ul><ul><li>FileInputStream fis = new FileInputStream(&quot;story.txt&quot;); </li></ul><ul><li>FileChannel fc = fis.getChannel(); </li></ul><ul><li>ByteBuffer buffer = ByteBuffer.allocate(1024); </li></ul><ul><li>fc.read(buffer); </li></ul><ul><li>buffer.rewind(); </li></ul><ul><li>for(int i=0; i< fc.size() ; i++) </li></ul><ul><li>System.out.print( (char)buffer.get() ); </li></ul><ul><li>fc.close(); </li></ul><ul><li>fis.close(); </li></ul><ul><li>} </li></ul><ul><li>catch(Exception e) </li></ul><ul><li>{ System.out.print( e.getMessage() ); } </li></ul>
  9. 9. Writing to a File <ul><li>try{ </li></ul><ul><li>FileOutputStream fos = new FileOutputStream(&quot;story.txt&quot;); </li></ul><ul><li>FileChannel fc = fos.getChannel(); </li></ul><ul><li>ByteBuffer buffer = ByteBuffer.allocate(1024); </li></ul><ul><li>buffer.put(&quot;Sindbad : - The great Sailor&quot;.getBytes()); </li></ul><ul><li>buffer.rewind(); </li></ul><ul><li>fc.write(buffer); </li></ul><ul><li>fc.close(); </li></ul><ul><li>fos.close(); </li></ul><ul><li>} </li></ul><ul><li>catch(Exception e) </li></ul><ul><li>{ System.out.print( e.getMessage() ); } </li></ul>

×