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