`
美丽的小岛
  • 浏览: 299170 次
  • 性别: Icon_minigender_1
  • 来自: 大连
社区版块
存档分类
最新评论

关于Channel学习

    博客分类:
  • java
 
阅读更多

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() ;

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

  • 大小: 27.1 KB
  • 大小: 247.1 KB
1
2
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics