`

怎样使用FileChannel

 
阅读更多

怎样使用FileChannel

 

FileChannel定义了两个用于文件传输的方法:

1. transferTo(int position, long count, WritableByteChannel dst)

    该方法是把文件从position位置开始向dst通道传送count个字节。

 

2. transferFrom(ReadableByteChannel src, long position, long count)

    该方法是将count个字节从通道src传送到文件中,position是为文件开始写入的位置。

 

从FileInputStream中获得的 FileChannel对象 只支持transferTo,而从FileOutputStream中获得的 FileChannel对象 只支持tansferFrom()方法。

 

FileChannel sfc =  new  FileInputStream(“D://temp//from.txt”).getChannel();

FileChannel tfc =  new  FileOutputStream(“D://temp//to.txt”).getChannel();

sfc.transferTo( 0 , sfc.size(), tfc);

sfc.close();

tfc.close();

 

 

 

 

分享到:
评论
1 楼 sunfuyun 2012-09-24  
  

相关推荐

Global site tag (gtag.js) - Google Analytics