您的位置:首页 > 其它

复制文件到目标路径

2012-11-07 17:21 281 查看
不多说 直接上代码 你懂得

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");

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐