/*
复制一个图片
思路:
1,用字节流读取对象和图片关联
2,用字节写入流对象创建一个图片文件,用于存储获取到的图片数据。
3,通过循环读写,完成数据的存储。
4,关闭资源。
*/
import java.io.*;
class Copypic
{
public static void main(String[] args)
{
FileOutputStream fos = null;
FileInputStream fis = null;
try
{
fis = new FileInputStream("C:\\1.jpg");
fos = new FileOutputStream("C:\\2.jpg");
byte[] buf = new byte[1024];
int len = 0;
while ((len=fis.read(buf))!=-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)
fis.close();
}
catch (IOException e)
{
throw new RuntimeException("关闭读取失败");
}
}
}
}
复制一个图片
思路:
1,用字节流读取对象和图片关联
2,用字节写入流对象创建一个图片文件,用于存储获取到的图片数据。
3,通过循环读写,完成数据的存储。
4,关闭资源。
*/
import java.io.*;
class Copypic
{
public static void main(String[] args)
{
FileOutputStream fos = null;
FileInputStream fis = null;
try
{
fis = new FileInputStream("C:\\1.jpg");
fos = new FileOutputStream("C:\\2.jpg");
byte[] buf = new byte[1024];
int len = 0;
while ((len=fis.read(buf))!=-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)
fis.close();
}
catch (IOException e)
{
throw new RuntimeException("关闭读取失败");
}
}
}
}
2530

被折叠的 条评论
为什么被折叠?



