复制文件到目标路径

来源:互联网 发布:知盈医学课堂是真的吗? 编辑:程序博客网 时间:2024/05/16 08:32

不多说 直接上代码 你懂得




import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;


import javax.imageio.stream.FileImageInputStream;
import javax.imageio.stream.FileImageOutputStream;


public class CopyImage {

/**
* 拷贝图片至新的文件下
* @param oldImgFile 原文件存放路径
* @param newImgFile 新文件存放路径
*/
private static void copyImage(String oldImgFile,String newImgFile){
FileImageInputStream infile=null;
FileImageOutputStream outfile=null;
try {
File filepath=new File(oldImgFile);
infile=new FileImageInputStream(filepath);
byte[] b=new byte[1024*3];
File newfile=new File(newImgFile);
       if (!newfile.getParentFile().exists()) {
        newfile.getParentFile().mkdirs();
       }
outfile=new FileImageOutputStream(newfile);
int bufLength = 0;
while ((bufLength = infile.read(b)) != -1) {
outfile.write(b, 0, bufLength);
outfile.flush();
}
} catch (FileNotFoundException e) {

} catch (IOException e) {

}finally{
try {
if(infile!=null)
{
infile.close();
}
if(outfile!=null){
 outfile.close();
}
} catch (IOException e1) {
infile=null;
outfile=null;
}
}
}

public static void main(String[] args){
CopyImage c = new CopyImage();
c.copyImage("F:\\suotu\\sLA20121018B15-001.jpg", "F:\\suotu\\copy\\sLA20121018B15-001_s.jpg");

}


}