• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Channel Based Io
 

Channel Based Io

on

  • 1,371 views

 

Statistics

Views

Total Views
1,371
Views on SlideShare
1,371
Embed Views
0

Actions

Likes
4
Downloads
0
Comments
0

0 Embeds 0

No embeds

Accessibility

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Channel Based Io Channel Based Io Presentation Transcript

    • Channel-Based I/O Supported by : java.nio java.nio.channels From jdk1.4+
    • 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.
    • Buffers java.nio.Buffer MappedByteBuffer DoubleBuffer FloatBuffer LongBuffer ByteBuffer ShortBuffer IntBuffer java.lang.Object CharBuffer
    • Channel Hierarchy
    • FileChannel java.nio.channels.FileChannel java.nio.channels.spi.AbstractInterruptibleChannel GatheringByteChannel ByteChannel ScatteringByteChannel java.lang.Object
    • 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
    • GatheringByteChannel & ScatteringByteChannel
      • ScatteringByteChannel
      • long read (ByteBuffer[], int, int)
      • long read (ByteBuffer)
      • GatheringByteChannel
      • long write(ByteBuffer[], int, int)
      • long write(ByteBuffer)
      GatheringByteChannel ScatteringByteChannel ReadableByteChannel WritableByteChannel
    • 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() ); }
    • 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() ); }
    •  
    •  
    •  
    •  
    •  
    •