关于Channel学习

java的新I/O流,有点意思,看看相关的类,其实就这几个就能解决解了。对于Buffer与ByteBuffer比较重要,看看来自JDK的说法http://happyprince.iteye.com/blog/1692137,对于ByteChallnel的FileChannel都是来自ReadByteChannel与WriteByteChannel的方法。ByteChannel中没有什么方法的。



具体一点把JDK弄过来查一查,如下(FileChannel的方法):

 例子(两个文件的复制): 

		String fname1 = "src/data.txt", fname2 = "src/data2.txt";
		File f1 = new File(fname1), f2 = new File(fname2);
		FileChannel in = new FileInputStream(f1).getChannel();
		FileChannel out = new FileOutputStream(f2).getChannel();
		ByteBuffer b = ByteBuffer.allocate(1024);
		while (in.read(b) != -1) {
			b.flip();
			out.write(b);
			b.clear();
		}
		in.close() ;
		out.close() ;

 另一个例子也是文件的复制:

  FileChannel inn = new FileInputStream("src/data.txt").getChannel(), 
              outt = new FileOutputStream("src/data1.txt").getChannel();
  inn.transferTo(0, inn.size(), outt);
   inn.close() ;
   outt.close() ;

 两个都可实现文件的复制功能。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值