NIO(helloworld,字符編碼,字節存放順序)

直接上代碼:

package cn.yjq.interview;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.util.SortedMap;
import java.util.Map.Entry;

import org.junit.Test;

public class GetChannels {
	
	public static final int BSIZE = 1024;
	
	public static void main(String[] args) {
		try {
			//3種方式獲取chanel
			FileChannel fc = new FileOutputStream("data.txt").getChannel();
			fc.write(ByteBuffer.wrap("hello channel!".getBytes()));
			fc.close();
			
			fc = new RandomAccessFile("data.txt", "rw").getChannel();
			fc.position(fc.size());
			fc.write(ByteBuffer.wrap("more datas!".getBytes()));
			fc.close();
			
			fc = new FileInputStream("data.txt").getChannel();
			//數據緩存區
			ByteBuffer buffer = ByteBuffer.allocate(BSIZE);
			fc.read(buffer);
			buffer.flip();
			while(buffer.hasRemaining()) {
				System.out.print((char)buffer.get());
			}
			fc.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@Test
	public void printCharSets() {
		//所有默認的字符編碼
		SortedMap<String, Charset> map =Charset.availableCharsets();
		for(Entry<String, Charset> e : map.entrySet()) {
			System.out.println(e.getKey() + " : " + e.getValue().name());
		}
	}
	
}

2. 視圖緩存器

package cn.yjq.interview;

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.DoubleBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.LongBuffer;
import java.nio.ShortBuffer;

import org.junit.Test;

public class ViewBuffers {
	
	@Test
	public void test1() {
		ByteBuffer bb = ByteBuffer.wrap(new byte[]{0, 0, 0, 0, 0, 0, 0, 'a'});
		System.out.print("Byte Buffer : ");
		while(bb.hasRemaining()) {
			System.out.print(bb.position() + "->" + bb.get() + " ");
		}
		System.out.println();
		
		System.out.print("Char Buffer : ");
		CharBuffer cb =((ByteBuffer)bb.rewind()).asCharBuffer();
		while(cb.hasRemaining()) {
			System.out.print(cb.position() + "->" + cb.get() + " ");
		}
		System.out.println();
		
		System.out.print("Short Buffer : ");
		ShortBuffer sb = ((ByteBuffer)bb.rewind()).asShortBuffer();
		while(sb.hasRemaining()) {
			System.out.print(sb.position() + "->" + sb.get() + " ");
		}
		System.out.println();
		
		System.out.print("Int Buffer : ");
		IntBuffer ib = ((ByteBuffer)bb.rewind()).asIntBuffer();
		while(ib.hasRemaining()) {
			System.out.print(ib.position() + "->" + ib.get() + " ");
		}
		System.out.println();
		
		System.out.print("Float Buffer : ");
		FloatBuffer fb = ((ByteBuffer)bb.rewind()).asFloatBuffer();
		while(fb.hasRemaining()) {
			System.out.print(fb.position() + "->" + fb.get() + " ");
		}
		System.out.println();
		
		System.out.print("Long Buffer : ");
		LongBuffer lb = ((ByteBuffer)bb.rewind()).asLongBuffer();
		while(lb.hasRemaining()) {
			System.out.print(lb.position() + "->" + lb.get() + " ");
		}
		System.out.println();
		
		System.out.print("Double Buffer : ");
		DoubleBuffer db = ((ByteBuffer)bb.rewind()).asDoubleBuffer();
		while(db.hasRemaining()) {
			System.out.print(db.position() + "->" + db.get() + " ");
		}
		System.out.println();
	}
}
//output
Byte Buffer : 0->0 1->0 2->0 3->0 4->0 5->0 6->0 7->97 
Char Buffer : 0->  1->  2->  3->97
Short Buffer : 0->0 1->0 2->0 3->97 
Int Buffer : 0->0 1->97 
Float Buffer : 0->0.0 1->1.36E-43 
Long Buffer : 0->97 
Double Buffer : 0->4.8E-322


3. 字節存放次序



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章