您的位置:首页 > 其它

通过文件流复制图片

2009-11-13 19:19 302 查看
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class ImageCopier {

public static void main(String args[]) throws Exception{

//源图片路径(包括图片名)
String srcUrl = "D:/My Documents/temp/p1.jpg";

//目标图片路径
String tarUrl = "D:/My Documents/temp/p2.jpg";
ioImage(srcUrl, tarUrl);
}

/**
*
* @param srcUrl 源图片路径
* @return 目标图片路径
*/
public static void ioImage(String srcUrl,String tarUrl){

try {

//生成读取图片到内存的输入流
FileInputStream finput = new FileInputStream(new File(srcUrl));

//生成从内存将图片输出到磁盘的输出流
FileOutputStream foutput = new FileOutputStream(new File(tarUrl));
BufferedOutputStream bos = new BufferedOutputStream(foutput);

int b ;
while(true) {
if(finput.available()<1024){
//
while((b=finput.read())!=-1){

bos.write(b);
}
break;
}else{

b=finput.read();
bos.write(b);
}
} finput.close();
bos.close();
foutput.close();

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

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