原创  使用FileChannel复制文件 收藏

文件通道定义了两个方法,可以进行直接的文件传输:
int transferTo(int position,long count,WritableByteChannel dst);
这个函数是把文件从position位置开始向dst通道传送count个字节。
int transferFrom(ReadableByteChannel src,long position,long count);
将count个字节从通道src传送到文件中,position是为文件开始写入的位置。
从FileInputStream中获得的通道只支持transferTo,而从FileOutputStream中获得的通道只支持tansferFrom()方法

  1. FileChannel sfc = new FileInputStream(“D:\\suqiang\\from.txt”).getChannel();
  2. FileChannel tfc = new FileOutputStream(“D:\\suqiang\\to.txt”).getChannel();
  3. sfc.transferTo(0, sfc.size(), tfc);
  4. sfc.close();
  5. tfc.close();

发表于 @ 2008年09月24日 17:46:00 | 评论( loading... ) | 编辑| 举报| 收藏

旧一篇:根据URL从服务器端复制文件到本地java代码 | 新一篇:使用apache.commons.fileupload 进行文件上传

  • 发表评论
  • 评论内容:
  •  
Copyright © china8848
Powered by CSDN Blog