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!");
- }
- }
复制代码
|
|