Java基础知识强化之IO流笔记44:IO流练习之 复制图片的 4 种方式案例

1. 复制图片的 4 种方式案例:

分析:

  复制数据,如果我们知道用记事本打开并能够读懂,就用字符流,否则用字节流。

  通过该原理,我们知道我们应该采用字节流。

  而字节流有4种方式,所以做这个题目我们有4种方式。推荐掌握第4种。

数据源

  c:\\a.jpg -- FileInputStream -- BufferedInputStream

目的地

  d:\\b.jpg -- FileOutputStream -- BufferedOutputStream

 

2. 4 种方式代码示例:

 1 package cn.itcast_01;
 2 
 3 import java.io.BufferedInputStream;
 4 import java.io.BufferedOutputStream;
 5 import java.io.File;
 6 import java.io.FileInputStream;
 7 import java.io.FileOutputStream;
 8 import java.io.IOException;
 9 
10 /*
11  * 复制图片
12  * 
13  * 分析:
14  *         复制数据,如果我们知道用记事本打开并能够读懂,就用字符流,否则用字节流。
15  *         通过该原理,我们知道我们应该采用字节流。
16  *         而字节流有4种方式,所以做这个题目我们有4种方式。推荐掌握第4种17  * 
18  * 数据源19  *         c:\\a.jpg -- FileInputStream -- BufferedInputStream
20  * 目的地21  *         d:\\b.jpg -- FileOutputStream -- BufferedOutputStream
22  */
23 public class CopyImageDemo {
24     public static void main(String[] args) throws IOException {
25         // 使用字符串作为路径
26         // String srcString = "c:\\a.jpg";
27         // String destString = "d:\\b.jpg";
28         // 使用File对象做为参数
29         File srcFile = new File("c:\\a.jpg");
30         File destFile = new File("d:\\b.jpg");
31 
32         // method1(srcFile, destFile);
33         // method2(srcFile, destFile);
34         // method3(srcFile, destFile);
35         method4(srcFile, destFile);
36     }
37 
38     // 字节缓冲流一次读写一个字节数组
39     private static void method4(File srcFile, File destFile) throws IOException {
40         BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
41                 srcFile));
42         BufferedOutputStream bos = new BufferedOutputStream(
43                 new FileOutputStream(destFile));
44 
45         byte[] bys = new byte[1024];
46         int len = 0;
47         while ((len = bis.read(bys)) != -1) {
48             bos.write(bys, 0, len);
49         }
50 
51         bos.close();
52         bis.close();
53     }
54 
55     // 字节缓冲流一次读写一个字节
56     private static void method3(File srcFile, File destFile) throws IOException {
57         BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
58                 srcFile));
59         BufferedOutputStream bos = new BufferedOutputStream(
60                 new FileOutputStream(destFile));
61 
62         int by = 0;
63         while ((by = bis.read()) != -1) {
64             bos.write(by);
65         }
66 
67         bos.close();
68         bis.close();
69     }
70 
71     // 基本字节流一次读写一个字节数组
72     private static void method2(File srcFile, File destFile) throws IOException {
73         FileInputStream fis = new FileInputStream(srcFile);
74         FileOutputStream fos = new FileOutputStream(destFile);
75 
76         byte[] bys = new byte[1024];
77         int len = 0;
78         while ((len = fis.read(bys)) != -1) {
79             fos.write(bys, 0, len);
80         }
81 
82         fos.close();
83         fis.close();
84     }
85 
86     // 基本字节流一次读写一个字节
87     private static void method1(File srcFile, File destFile) throws IOException {
88         FileInputStream fis = new FileInputStream(srcFile);
89         FileOutputStream fos = new FileOutputStream(destFile);
90 
91         int by = 0;
92         while ((by = fis.read()) != -1) {
93             fos.write(by);
94         }
95 
96         fos.close();
97         fis.close();
98     }
99 }

 

posted on 2015-10-11 12:59  鸿钧老祖  阅读(200)  评论(0编辑  收藏  举报

导航