读书人

怎么使用FileChannel

发布时间: 2012-10-07 17:28:51 作者: rapoo

怎样使用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();

?

?

?

?

读书人网 >编程

热点推荐