Java 之NIO(二) - Channel

简介:

晒酷学院qq群:979438600
本节主要内容包括通过Channel(通道)对文件进行读写操作,通道类似于之前的输入/输出流,程序不会直接操作通道,通常都是将数据读入到缓冲区中,然后再从缓冲区中读写数据,与传统的输入输出流相比,通道是双向操作的,既可以完成输入,也可以完成输出。
另外,本节还会讲解一下内存映射,内存映射指的是将文件映射到内存中,这样就可以使用系统调用函数进行内存读写,采用这样的方式读取文件是速度最快的。
下面通过代码示例,来描述FileChannel进行文件读写以及FileChannel提供的map方法进行内存映射的使用步骤,为了简化文字篇幅,以下代码我会做详细的注释,相信大家会一目了然。

文件写操作:

package com.ray.nio.demo;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
/**
 * FileChannel是Channel的子类,可以进行文件的读写操作,使用FileChannel可以通过FileInputStream 和 FileOutputStream获得。
 * 本示例演示了使用FileChannel写入操作,首先将内容放入缓冲区中,然后使用输出通道将数据输出到文本文件中。
 * @author xuleilei
 *
 */
public class ChannelWrite {
	
	public static void main(String[] args) {
		String outputMsg = "这是一段测试代码!abc123!@#";
		
		FileOutputStream fos = null;	//文件输出流
		FileChannel fileChannel = null;	//读写文件的通道
		try {
			fos = new FileOutputStream(new File("test.txt"));
			fileChannel = fos.getChannel();	//通过文件输出流获取输出通道
//			开辟字节缓冲区,并指定缓冲区大小
			ByteBuffer buf = ByteBuffer.allocate(outputMsg.getBytes().length);
			buf.put(outputMsg.getBytes());	//将数据存放在字节缓冲区中
			buf.flip();	//重设缓冲区
			
//			通过写通道,将缓冲区中的数据输出到目标文件中
			fileChannel.write(buf);
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				fileChannel.close();
				fos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

文件读写操作:

package com.ray.nio.demo;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
/**
 * FileChannel进行文件边读边写操作。
 * @author xuleilei
 *
 */
public class ChannelReadWrite {
	
	public static void main(String[] args) {
		
		FileInputStream fis = null;		//文件输入流
		FileOutputStream fos = null;	//文件输出流
		FileChannel fin = null;		//文件输入通道
		FileChannel fout = null;	//文件输出通道
		try {
			fis = new FileInputStream(new File("in.txt"));
			fos = new FileOutputStream(new File("out.txt"));
//			注意,通过输入,输出文件流获取对应的通道
			fin = fis.getChannel();
			fout = fos.getChannel();
			
			ByteBuffer buf = ByteBuffer.allocate((int) new File("in.txt").length());
			
			while(fin.read(buf) != -1){
				buf.flip();
				fout.write(buf);
				buf.clear();
			}
			
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				fin.close();
				fout.close();
				fis.close();
				fos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

内存映射:
在使用FileChannel的map方法进行映射时,FileChannel$MapMode类提供了三种映射模式:

/*** Mode for a read-only mapping.	 */
public static final MapMode READ_ONLY = new MapMode("READ_ONLY");
/*** Mode for a read/write mapping. */
public static final MapMode READ_WRITE  = new MapMode("READ_WRITE");

/*** Mode for a private (copy-on-write) mapping. */
public static final MapMode PRIVATE  = new MapMode("PRIVATE");

package com.ray.nio.demo;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;
/**
 * 内存映射
 * @author xuleilei
 *
 */
public class ChannelMemMap {
	public static void main(String[] args) {
		FileInputStream fis = null;
		FileChannel fc = null;
		try {
			File file = new File("in.txt");
			fis = new FileInputStream(file);
			fc = fis.getChannel();
//			通过FileChannel将文件映射到内存中,并设置映射模式为只读
			MappedByteBuffer mbb = fc.map(MapMode.READ_ONLY, 0, file.length());
			byte[] data = new byte[(int) file.length()];
			for(int i=0;mbb.hasRemaining();i++){
				data[i] = mbb.get();
			}
			System.out.println(new String(data));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				fc.close();
				fis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值