Java中io流的学习(十一)NIO

io流大体分为BIO,AIO,NIO,常用的基本就是BIO,也就是之前文章所介绍的那些,接下来我们来大概了解一下NIO。

传统io是靠字节或字符来传输,nio是靠块传输,也就是一个一个的buffer速度相对于较快。传统io四阻塞型io,nio是非阻塞型io。多适用于进行流畅的网络读写操作。

其中的方法比较多,而且涉及到几个类之间相互继承的方法,具体我就不在这里写出了,在需要使用的时候,可以去参照API使用。

下面通过实例代码来对其进行学习:

①进行Buffers的测试,值得注意的是在需要获取数据的时候,需要使用flip()方法将指针反转,即归零操作

	@Test
	public void t1(){
		//创建缓冲区
		ByteBuffer bb = ByteBuffer.allocate(10);
		//获取此缓冲区的指针位置
		System.out.println("指针的位置:" + bb.position());
		//获取此缓冲区的限制位置
		System.out.println("限制的位置:" + bb.limit());
		//获取此缓冲区的容量
		System.out.println("缓冲区的容量:" + bb.capacity());
		//获取此缓冲区指针到限制之间的元素数
		System.out.println("指针到限制之间的元素数:" + bb.remaining());
		//存储元素
		bb.put("abcd".getBytes());
		System.out.println("------------------------");
		//获取此缓冲区的指针位置
		System.out.println("指针的位置:" + bb.position());
		//获取此缓冲区的限制位置
		System.out.println("限制的位置:" + bb.limit());
		//获取此缓冲区的容量
		System.out.println("缓冲区的容量:" + bb.capacity());
		//获取此缓冲区指针到限制之间的元素数
		System.out.println("指针到限制之间的元素数:" + bb.remaining());
		
		System.out.println("------------------------");
		//获取数据,在获取之前,要先反转指针,让指针归零,否则无法获取
		bb.flip();
		//获取此缓冲区的指针位置
		System.out.println("指针的位置:" + bb.position());
		//获取此缓冲区的限制位置
		System.out.println("限制的位置:" + bb.limit());
		//获取此缓冲区的容量
		System.out.println("缓冲区的容量:" + bb.capacity());
		//获取此缓冲区指针到限制之间的元素数
		System.out.println("指针到限制之间的元素数:" + bb.remaining());
		System.out.println("------------------------");
		for(int i = 0;i < bb.remaining();i++){
			System.out.print((char)bb.get(i));
		}
	}

②进行文件的拷贝,直接将整个文件一次性读入缓冲区,然后直接一次性将文件从缓冲区写出来

	@Test
	public void t2() throws Exception{
		File file = new File("H:\\javaio\\测试.avi");
		FileInputStream fis = new FileInputStream(file);
		FileOutputStream fos = new FileOutputStream("H:\\javaio\\测试NIO1.avi");
		
		//获取通道
		FileChannel rChannel = fis.getChannel();
		FileChannel wChannel = fos.getChannel();
		
		//创建缓冲区
		ByteBuffer bb = ByteBuffer.allocate((int) file.length());
		rChannel.read(bb);
		bb.flip();
		wChannel.write(bb);
		fos.close();
		fis.close();
	}

③进行文件的拷贝,1024个字节的读和1024个字节的写

	@Test
	public void t3() throws Exception{
		File file = new File("H:\\javaio\\测试.avi");
		FileInputStream fis = new FileInputStream(file);
		FileOutputStream fos = new FileOutputStream("H:\\javaio\\测试NIO2.avi");
		
		//获取通道
		FileChannel rChannel = fis.getChannel();
		FileChannel wChannel = fos.getChannel();
		
		//创建缓冲区,大小为1024个字节
		ByteBuffer bb = ByteBuffer.allocate(1024);
		while(rChannel.read(bb) != -1){
			bb.flip();
			wChannel.write(bb);
			bb.clear();
		}
		
		fos.close();
		fis.close();
	}

③进行文件拷贝,使用带有映射关系的MappedByteBuffer进行拷贝

	@Test
	public void t4() throws Exception{
		File file = new File("H:\\javaio\\测试.avi");
		long length = file.length();
		RandomAccessFile rraf = new RandomAccessFile(file, "r");
		RandomAccessFile wraf = new RandomAccessFile("H:\\javaio\\测试NIO3.avi", "rw");
		
		//获取通道
		FileChannel rChannel = rraf.getChannel();
		FileChannel wChannel = wraf.getChannel();
		
		//创建带有映射关系的缓冲区
		MappedByteBuffer rmbb = rChannel.map(MapMode.READ_ONLY, 0, length);
		MappedByteBuffer wmbb = wChannel.map(MapMode.READ_WRITE, 0, length);
		
		for (int i = 0; i < length; i++) {
			byte value = rmbb.get(i);
			wmbb.put(value);
		}
		rraf.close();
		wraf.close();
		
	}

Java中io流的学习(一)File:https://blog.csdn.net/qq_41061437/article/details/81672859

Java中io流的学习(二)FileInputStream和FileOutputStream:https://blog.csdn.net/qq_41061437/article/details/81742175

Java中io流的学习(三)BuffereInputStream和BuffereOutputStream:https://blog.csdn.net/qq_41061437/article/details/81743522

Java中io流的学习(四)InputStreamReader和OutputStreamWriter:https://blog.csdn.net/qq_41061437/article/details/81745300

Java中io流的学习(五)FileReader和FileWriter:https://blog.csdn.net/qq_41061437/article/details/81747105

Java中io流的学习(六)BufferedReader和BufferedWriter:https://blog.csdn.net/qq_41061437/article/details/81747323

Java中io流的学习(七)ObjectInputStream和ObjectOutputStream:https://blog.csdn.net/qq_41061437/article/details/81748461

Java中io流的学习(八)PrintStream和PrintWriter:https://blog.csdn.net/qq_41061437/article/details/81782770

Java中io流的学习(九)RandomAccessFile:https://blog.csdn.net/qq_41061437/article/details/81805351

Java中io流的学习(十)ByteArrayInoutStream和ByteArrayOutputStream:https://blog.csdn.net/qq_41061437/article/details/81806245

Java中io流的学习(十一)NIO:https://blog.csdn.net/qq_41061437/article/details/81809370

Java中io流的学习(总结):https://blog.csdn.net/qq_41061437/article/details/81740680

猜你喜欢

转载自blog.csdn.net/qq_41061437/article/details/81809370