`
Donald_Draper
  • 浏览: 950778 次
社区版块
存档分类
最新评论

FileChannel示例

    博客分类:
  • NIO
nio 
阅读更多
前面我们看过socket通道,datagram通道,以管道Pipe,从今天起,我们来看一下file通道,先从一个示例开始:
测试主类1:
package nio.filechannel;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
/**
 * 测试FileChannel
 * @author donald
 * 2017年4月9日
 * 下午4:16:35
 */
public class testFileChannel {
	public static void main(String[] args) throws IOException {
		RandomAccessFile aFile = new RandomAccessFile("E:/nio_data.txt", "rw");
		FileChannel inChannel = aFile.getChannel();
		ByteBuffer buf = ByteBuffer.allocate(1024);
		/*
		 * 1.先屏蔽writeBytes,执行readBytes,从文件中读取数据
		 * 2.再屏蔽readBytes,执行writeBytes,向文件中写数据
		 * 3.最后屏蔽writeBytes,执行readBytes,从文件中读取数据
		 */
//		writeBytes(buf, inChannel);
		readBytes(buf, inChannel);
		inChannel.close();
		aFile.close();
	}
	private static void writeBytes(ByteBuffer buf, FileChannel fileChannel) throws IOException{
	    String newData = "new String to write to file...."+System.currentTimeMillis();
	    buf.put(newData.getBytes("UTF-8"));
	    buf.flip();
	    while(buf.hasRemaining())
	    	fileChannel.write(buf);
	    System.out.println("===已经写完数据到文件");
	    
	}
	private static void readBytes(ByteBuffer buf, FileChannel fileChannel) throws IOException{
		    buf.clear();
			//从file通道读取数据到缓存区,即写入缓冲区
			int bytesRead = fileChannel.read(buf);
			while (bytesRead != -1) {
//				buf.compact();//将未读完的数据移到缓冲的前面,新写入的数据,将会append旧数据的后面
				bytesRead = fileChannel.read(buf);
			}
			//转换缓冲区模式
			buf.flip();// swith the mode write or read
			System.out.println("=====Read byte length:" + buf.limit());
			while (buf.hasRemaining()) {
				System.out.print((char) buf.get());
			}
			System.out.println();
	}
}

按一下三步执行:
1.先屏蔽writeBytes,执行readBytes,从文件中读取数据
2.再屏蔽readBytes,执行writeBytes,向文件中写数据
3.最后屏蔽writeBytes,执行readBytes,从文件中读取数据
相应的控制台输出为:
1.=====Read byte length:0
2.===已经写完数据到文件
3.=====Read byte length:44
new String to write to file....1494407798722

测试主类2:
package nio.filechannel;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
/**
 * 测试通道间传输
 * @author donald
 * 2017年4月9日
 * 下午10:27:16
 */
public class testTransferChannel {
	public static void main(String[] args) throws IOException {
		RandomAccessFile fromFile = new RandomAccessFile("E:/nio_data.txt", "rw");
		FileChannel fromChannel = fromFile.getChannel();
		RandomAccessFile toFile = new RandomAccessFile("E:/nio_data_to.txt", "rw");
		FileChannel toChannel = toFile.getChannel();
		RandomAccessFile to2File = new RandomAccessFile("E:/nio_data_to2.txt", "rw");
		FileChannel to2Channel = to2File.getChannel();
		long position = 0;
		long count = fromChannel.size();
		//将源通道的数据传输的本通道
		toChannel.transferFrom(fromChannel, position, count);
		System.out.println("===将源通道的数据传输的本通道完毕");
		//将本通道数据传输到目的通道
		fromChannel.transferTo(position, count, to2Channel);
		System.out.println("===将本通道数据传输到目的通道完毕");
		fromChannel.close();
		fromFile.close();
		toChannel.close();
		toFile.close();
		to2Channel.close();
		to2File.close();
	}
}

控制输出:
===将源通道的数据传输的本通道完毕
===将本通道数据传输到目的通道完毕

打开文件E:/nio_data_to.txt和E:/nio_data_to2.txt,文件内容如下:
new String to write to file....1494407798722
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics