ByteBuffer編碼和解碼設定
阿新 • • 發佈:2019-01-25
FileChannel fileChannel2=new FileInputStream("D:\\abr.txt").getChannel(); Charset charset=Charset.forName("utf-8"); ByteBuffer byteBuffer=ByteBuffer.allocate(BIZE); while (fileChannel2.read(byteBuffer)!=-1){ charset.decode(byteBuffer); byteBuffer.flip(); CharBuffer charBuffer=charset.decode(byteBuffer);System.out.println(charBuffer.toString()); System.out.println("------------"); byteBuffer.clear(); }
1.解碼:從通道中讀取資料儲存在ByteBuffer中,呼叫charset.decode(bytebuffer)進行對應格式的解碼,並得到CharBuffer
2.編碼:和解碼互逆,把呼叫chareset.encode(CharBuffer),返回出特定編碼後的ByteBuffer,放入通道
3.
Charset charset=Charset.forName("utf-8");
獲取Charset物件