[Java Study Notes] Learning filechannel

Source: Internet
Author: User

Author: gnuhpc
Source: http://www.cnblogs.com/gnuhpc/

Various channels are defined. These channels indicate connections to entities (such as files and sockets) that can perform I/O operations; defines the Selector Used for multiplexing and non-blocking I/O operations.

Write files

Import java. Io. fileoutputstream;
Import java. Io. ioexception;
Import java. NiO. bytebuffer;
Import java. NiO. charbuffer;
Import java. NiO. channels. filechannel;
Public class nioblockout {
Public static void main (string Arg []) {
Try {
Fileoutputstream Fos = new fileoutputstream ("nioblock ");
Filechannel channel = FOS. getchannel ();
Bytebuffer = bytebuffer. Allocate (20 );
Charbuffer = bytebuffer. ascharbuffer ();
Charbuffer. Put ("abcdefghi ");
Channel. Position (20 );
Channel. Write (bytebuffer );
Channel. Close ();
} Catch (ioexception e ){
System. Out. println (E );
}
}
}

File Buffer

Import java. Io. ioexception;
Import java. Io. randomaccessfile;
Import java. NiO. channels. filechannel;
Import java. NiO. channels. filechannel. mapmode;
Import java. NiO. mappedbytebuffer;
Import java. NiO. charbuffer;
Public class niomap {
Public static void main (string Arg []) {
Try {
Randomaccessfile RAF = new randomaccessfile ("nioblock", "RW ");
Filechannel channel = Raf. getchannel ();
Mappedbytebuffer bytebuffer = channel. Map (
Filechannel. mapmode. read_write,
20, 18); // position and size
Charbuffer = bytebuffer. ascharbuffer ();
Bytebuffer. Load ();
Char CH = charbuffer. Get (3 );
System. Out. println ("character:" + CH );
Charbuffer. Put (3, 'x ');
Channel. Close ();
} Catch (ioexception e ){
System. Out. println (E );
}
}
}

 

Author: gnuhpc
Source: http://www.cnblogs.com/gnuhpc/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.