Learn about the NIO module in Java

Source: Internet
Author: User

A lot of online information, but to write code, my age of people have a little memory.

Reference URL:

http://blog.csdn.net/wuxianglong/article/details/6612282

 PackageCom.cg.io;ImportJava.io.*;ImportJava.nio.*;Importjava.nio.channels.*; Public classTestintbuffer {Static Private Final byteMessage[] = {83, 111, 109, 101, 32, 98, 98}; Static PrivateString fileName = "D:\\hehehe\\test.txt";  Public Static voidMain (string[] args)throwsException {fileoutputstream fout=NewFileOutputStream (fileName); FileChannel FC=Fout.getchannel (); Bytebuffer Bytebuffer= Bytebuffer.allocate (1024); Output ("Initialize", Bytebuffer);  for(inti=0; i<message.length; ++i) {bytebuffer.put (message[i]);    } bytebuffer.flip (); Output ("Call Flip ()", Bytebuffer);    Fc.write (Bytebuffer); Output ("Call Write ()", Bytebuffer);    Fout.close ();    Bytebuffer.clear (); Output ("Call Clear ()", Bytebuffer); FileInputStream Fin=NewFileInputStream (fileName); FC=Fin.getchannel ();    Fc.read (Bytebuffer); Output ("Call Read ()", Bytebuffer);    Bytebuffer.flip (); Output ("Call Flip ()", Bytebuffer);  while(bytebuffer.remaining () > 0) {      byteb =Bytebuffer.get (); System.out.print ( (Char) (b)); } Output ("Call Getchannel ()", Bytebuffer);    Bytebuffer.clear (); Output ("Call Clear ()", Bytebuffer);    Fin.close (); Intbuffer Intbuffer= Intbuffer.allocate (8);  for(inti=0; I < intbuffer.capacity (); ++i) {intj = 2 * (i + 1);    Intbuffer.put (j);    } intbuffer.flip ();  while(Intbuffer.hasremaining ()) {intj =Intbuffer.get (); System.out.print (J+ " "); }  }   Public Static voidoutput (String step, buffer buffer) {System.out.println (step+ " : "); System.out.println ("Capacity:" + buffer.capacity () + ","); System.out.println ("Position:" + buffer.position () + ","); System.out.println ("Limit:" + buffer.limit () + ",");  System.out.println (); }}

Learn about the NIO module in Java

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.