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(); }
}
}
}
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(); }
}
}
}