Java--NIO学习(一)

版权声明:be the one ~you will be the one~~ https://blog.csdn.net/Hqxcsdn/article/details/88421815

1.buffer

缓冲区 负责nio中的数据的存取 ,底层实现为数组

数据类型不同 有不同的类型的 buffer (boolean没有)
baytebuffer
CharBuffer
IntBuffer

使用方式都几乎一样
1.使用 allocate 创建缓冲区
2.存取数据方法
put() 将数据存入缓冲区
get() 获取缓冲区中的数据
3.缓冲区的四个核心属性
capacity:缓冲区 最大存储容量 不可改变
limit:缓冲区中数据 可操作 的大小
position:当前正在操作的数据的位置

position<= limit<= capacity
mark:表示 记录当前的位置 ,可以使用reset()恢复到mark 的位置
4. 读取数据 切换方法 filp
flip() 切换的到读取缓冲区数据的模式
此时 position 变为 0, limit 变为 之前的position ,capacity不变

直接缓冲区—非直接缓冲区
非直接缓冲区 – 通过allocate () 方法分配 缓冲区,建立在jvm中
直接缓冲区 – 通过 allocateDirect 在物理内存中开辟一个缓冲区

public static void txt() {
		 String a ="my all";
		//为其分配一个指定大小的 缓冲区
		ByteBuffer buffer =ByteBuffer.allocate(1024);
		//写入数据 
		buffer.put(a.getBytes());
		//flip() 切换的到读取缓冲区数据的模式
		//此时 position 变为 0, limit 变为 之前的position  ,capacity不变
		System.out.println(buffer.position());
		System.out.println(buffer.limit());
		buffer.flip();
		System.out.println(buffer.position());
		System.out.println(buffer.limit());
		byte[] by =new byte[6];
		
		//执行get方法 position 向后移动
		buffer.get(by);
		System.out.println(new String(by,0,by.length));
		System.out.println(buffer.position());
		System.out.println(buffer.limit());	
		//可重复读数据 
		buffer.rewind();
		System.out.println(buffer.position());
		System.out.println(buffer.limit());	
		
		//清空缓冲区,但是数据依然存在处于被遗忘状态
		buffer.clear();
		System.out.println(buffer.position());
		System.out.println(buffer.limit());	
		
		
		
	}
	public  static void txt2() {
		   String a ="my all";
			//为其分配一个指定大小的 缓冲区
			ByteBuffer buffer =ByteBuffer.allocate(1024);
			//写入数据 
			buffer.put(a.getBytes());
			//flip() 切换的到读取缓冲区数据的模式
			//此时 position 变为 0, limit 变为 之前的position  ,capacity不变
			System.out.println(buffer.position());
	}

2.channel

通道 channel

  • 与传统的流相似,在nio中负责缓冲区中数据的传送,channel本身不存储数据,要和buffer 配合使用

  • 主要实现类: FileChannel ||SocketChannel ServerSocketChannel DatagramChannel||

  • 获取通道的方法:

    • 1.getchannel();

      • FileInputStream/FileOutputStream
      • RandomAccessFile
    • 2.nio 2 :针对各个通道提供了 静态方法 open()

class testchannel{
	//文件的复制
	public void test1() throws FileNotFoundException {
		FileInputStream infile =  new FileInputStream("d:/a.txt");
		FileOutputStream outfile = new FileOutputStream("d:/b.txt");
		
		//获取通道
		FileChannel inchannel = infile.getChannel();
		FileChannel outchannel = outfile.getChannel();
		
		//建立缓冲区
		ByteBuffer buffer = ByteBuffer.allocate(1024);
		
		//将通道中的数据传入到缓冲区中
		try {
			while(inchannel.read(buffer)!=-1 ){
				buffer.flip();//切换buffer模式
				
				//将数据写入通道
				outchannel.write(buffer);
				
				buffer.clear();
				
				
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			inchannel.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			outchannel.close();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			infile.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			outfile.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	
	
}

在这里插入图片描述

扫描二维码关注公众号,回复: 5522029 查看本文章

猜你喜欢

转载自blog.csdn.net/Hqxcsdn/article/details/88421815
今日推荐