Your SlideShare is downloading. ×
Channel Based Io
Channel Based Io
Channel Based Io
Channel Based Io
Channel Based Io
Channel Based Io
Channel Based Io
Channel Based Io
Channel Based Io
Channel Based Io
Channel Based Io
Channel Based Io
Channel Based Io
Channel Based Io
Channel Based Io
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Channel Based Io

1,041

Published on

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

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

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

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

×