/*
复制一个图片
思路:
1,用字节读取流对象和图片关联
2,用字节写入流对象创建一个图片文件,用于存储获取到的图片数据
3,通过循环读写,完成数据的存储
4,关闭资源
*/
import java.io.*;
class CopyPic
{
public static void main(String[] args)
{
FileOutputStream fos = null;
FileInputStream fis = null;
try
{
fos = new FileOutputStream("c:\\2.jpg");
fis = new FileInputStream("c:\\1.jpg");
byte[] buf = new byte[1024];
int len = 0;
while((len=fis.read())!=-1)
{
fos.write(buf,0,len);
}
}
catch (IOException e)
{
throw new RuntimeException("复制文件失败");
}
finally
{
try
{
if(fos!=null)
fos.close();
}
catch (IOException e)
{
throw new RuntimeException("写入关闭失败");
}
try
{
if(fis!=null)
fos.close();
}
catch (IOException e)
{
throw new RuntimeException("读取关闭失败");
}
}
}
}
上边的代码可以实现复制,但是复制出来的图片打不开,怎么回事?还有就是原来的图片只有20.1k ,为什么复制出的有2.36M ?试了几次都这样
|
|