黑马程序员技术交流社区
标题:
IO 复制图片
[打印本页]
作者:
fmi110
时间:
2015-8-6 10:44
标题:
IO 复制图片
IO
/*
需求:将c盘的1.jpg文件复制到D盘 并命名为2.jpg
分析: 1、源文件不是文本文件 所以用字节流 InputStream读入
2、为提高读写效率,使用缓冲技术,BufferedInputStream()
3、输出使用OutputStream()
*/
import java.io.*;
class CopyImage
{
private BufferedInputStream in;
private BufferedOutputStream out;
CopyImage(BufferedInputStream in,BufferedOutputStream out){
this.in = in;
this.out = out;
}
public static void copy(BufferedInputStream in,BufferedOutputStream out) throws IOException
{
long start = System.currentTimeMillis();
//流返回的是字节
int by;
// int LENGTH = 1024;//字节数组的长度
// byte[] b = new byte[LENGTH];
while((by=in.read())!=-1){
out.write(by);
}
long end = System.currentTimeMillis();
System.out.println("*** Congratulate!!The piture has been Copyed!! ***");
System.out.println("*** It took "+(end -start)+" ms ***");
}
}
class CopyImageDemo
{
public static void main(String[] args) throws IOException
{
BufferedInputStream in = null;
BufferedOutputStream out = null;
try
{
in = new BufferedInputStream(new FileInputStream("c:\\1.jpg"));
out = new BufferedOutputStream(new FileOutputStream("d:\\2.jpg"));
CopyImage.copy(in,out);
}
catch (IOException e)
{
throw new RuntimeException("图片复制失败");
}
finally
{
if(in!=null)
in.close();
if(out!=null)
out.close();
}
System.out.println("Hello World!");
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2