IO-拷贝文件测试类----暂存

来源:互联网 发布:ansys fluent软件下载 编辑:程序博客网 时间:2024/05/22 01:37
package com.wzh.test;


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;


public class TestCopyFile {


/**
* @param args
*/
public static void main(String[] args) {
String src="d:/srcpicture/wazi.jpg";
String dst="d:/dstpicture/dst.jpg";
TestCopyFile.copyFile(src, dst);
}

private static void copyFile(String src,String dst)
{
BufferedInputStream bis=null;
BufferedOutputStream bos=null;
byte buf[]=new byte[1024];
try{
bis=new BufferedInputStream(new FileInputStream(src));
bos=new BufferedOutputStream(new FileOutputStream(dst));
int len=0;
while( true ){
len=bis.read(buf);
if(len<=0) break;
bos.write(buf,0,len);
}
//缓冲区只有满时才会将数据输出到输出流,用flush()将未满的缓冲区中数据强制输出
bos.flush();
}catch(Exception e){
e.printStackTrace();
}finally{
if(bis != null) try{ bis.close(); }catch(Exception e){ e.printStackTrace(); }
if(bos!= null) try{ bos.close(); }catch(Exception e){ e.printStackTrace(); }
}
}


}
0 0
原创粉丝点击