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. 字节存放次序



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值