码迷,mamicode.com
首页 > 其他好文 > 详细

使用Channel进行本地文件读写的简单实例2

时间:2020-05-12 17:19:21      阅读:103      评论:0      收藏:0      [点我收藏+]

标签:ring   cep   文件读写   操作   直接   oid   getch   rgs   inpu   

使用一个缓冲区进行读写操作:

 public static void main(String[] args) throws IOException {
FileInputStream fileInputStream = new FileInputStream("1.txt");
FileChannel channel01 = fileInputStream.getChannel();

FileOutputStream fileOutputStream=new FileOutputStream("2.txt");
FileChannel channel02 = fileOutputStream.getChannel();

ByteBuffer byteBuffer = ByteBuffer.allocate(1024);

//循环读取
while(true){
//清理缓冲区 非常重要
byteBuffer.clear();
int read= channel01.read(byteBuffer);
if(read==-1){
break;
}
byteBuffer.flip();
channel02.write(byteBuffer);
}
fileInputStream.close();
fileOutputStream.close();
}
直接通过channel进行读写
public static void main(String[] args) throws IOException {
FileInputStream fileInputStream = new FileInputStream("d:\\eva.jpg");
FileOutputStream fileOutputStream = new FileOutputStream("d:\\eva2.jpg");
FileChannel channel01 = fileInputStream.getChannel();
FileChannel channel02 = fileOutputStream.getChannel();
channel02.transferFrom(channel01,0,channel01.size());
channel01.close();
channel02.close();
fileInputStream.close();
fileOutputStream.close();

}

使用Channel进行本地文件读写的简单实例2

标签:ring   cep   文件读写   操作   直接   oid   getch   rgs   inpu   

原文地址:https://www.cnblogs.com/mc-74120/p/12876895.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!