IO流的文件复制

1、IO流的分类

1.根据处理数据类型的不同分为:
   字符流:1)Reader  读取字符流的抽象类   
      常用方法: read() 读取单个字符
                   read(char[] cbuf) 将字符读入数组。
            read(char[] cbuf, int off, int len) 将字符读入数组的某一部分。
            close() 关闭该流并释放与之关联的所有资源。
      2)Writer  写入字符流的抽象类 
      常用方法:write(int c) 写入单个字符。  
           write(String str) 写入字符串
           write(String str, int off, int len) 写入字符串的某一部分。
           write(char[] cbuf) 写入字符数组。
           write(char[] cbuf, int off, int len) 写入字符数组的某一部分。
           flush() 刷新该流的缓冲。
           close() 关闭此流,但要先刷新它。
   字节流:
      1)InputStream    此抽象类是表示字节输入流的所有类的超类
       常用方法:read() 从输入流中读取数据的下一个字节。
            read(byte[] b) 从输入流中读取一定数量的字节,并将其存储在缓冲区数组 b 中。
            read(byte[] b, int off, int len) 将输入流中最多 len 个数据字节读入 byte 数组。
            close() 关闭此输入流并释放与该流关联的所有系统资源。
      2) OutputStream   此抽象类是表示字节输出流的所有类的超类
       常用方法:write(int b) 将指定的字节写入此输出流。
            write(byte[] b) 将 b.length 个字节从指定的 byte 数组写入此输出流。
            write(byte[] b, int off, int len) 将指定 byte 数组中从偏移量 off 开始的 
            flush() 刷新此输出流并强制写出所有缓冲的输出字节。
            close() 关闭此输出流并释放与此流有关的所有系统资源。
2.根据数据流向不同分为:输入流和输出流
3.字符流的由来: 因为数据编码的不同,而有了对字符进行高效操作的流对象。本质其实就是基于字节流读取时,去查了指定的码表。
4.字节流和字符流的区别:
 (1)读写单位不同:字节流以字节(8bit)为单位,字符流以字符为单位,根据码表映射字符,一次可能读多个字节。
 (2)处理对象不同:字节流能处理所有类型的数据(如图片、avi等),而字符流只能处理字符类型的数据。
 (3)字节流在操作的时候本身是不会用到缓冲区的,是文件本身的直接操作的;而字符流在操作的时候下后是会用到缓冲区的,是通过缓冲区来操作文件,我们将在下面验证这一点。
5.结论:优先选用字节流。首先因为硬盘上的所有文件都是以字节的形式进行传输或者保存的,包括图片等内容。但是字符只是在内存中才会形成的,所以在开发中,字节流使用广泛。
6.输入流和输出流
   对输入流只能进行读操作,对输出流只能进行写操作,程序中需要根据待传输数据的不同特性而使用不同的流。

2、IO流的结构图

3、字节流复制文件

 1 package FileInputStream;
 2 
 3 import java.io.FileInputStream;
 4 import java.io.FileOutputStream;
 5 import java.io.IOException;
 6 
 7 // 字节流复制文件
 8 public class Copy字节流复制文件 {
 9     public static void main(String[] args) {
10         // 复制文件的起始时间
11         long s = System.currentTimeMillis();
12         // 定义输入输出流为mull
13         FileInputStream fis = null;
14         FileOutputStream fos = null;
15         // 手动抛出异常
16         try {
17             // 读取文件
18             fis = new FileInputStream("d:\digui.mp4");
19             // 读取完毕复制的文件
20             fos = new FileOutputStream("d:\dd.mp4");
21             // 定义一个字节数组  存到数组b中
22             byte[] b = new byte[1024 * 10];
23             // 起始长度为0
24             int len = 0;
25             // while(){} 循环   一边读取 ,一边写入(复制)文件
26             while ((len = fis.read(b)) != -1) {
27                 fos.write(b, 0, len);
28                 fos.flush();  //  文件刷新
29             }
30         } catch (Exception e) {
31             System.out.println(e);
32             throw new RuntimeException("文件复制失败");  // 手动抛除异常
33             // 最终执行语句
34         } finally {
35             // 复制的文件不为空时  关闭释放资源
36             if (fos != null) {
37                 try {
38                     fos.close();
39                 } catch (IOException e) {
40                     e.printStackTrace();
41                 } finally {
42                     if (fis != null) {
43                         try {
44                             fis.close();
45                         } catch (IOException e) {
46                             System.out.println(e);
47                             throw new RuntimeException("文件复制失败");
48                         }
49                     }
50                 }
51             }
52         }
53         // 复制文件的结束时间   单位:ms 毫秒
54         long e = System.currentTimeMillis();
55         System.out.println(e-s);
56     }
57 }

4、字符流复制文件

 1 package Demo;
 2 
 3 import java.io.FileReader;
 4 import java.io.FileWriter;
 5 import java.io.IOException;
 6 //字符流复制文件     FileReader   read读取
 7 //                   FileWriter   write写入
 8 public class Copy字符流复制文件 {
 9     public static void main(String[] args) {
10         long s = System.currentTimeMillis();
11         FileReader fr = null;   // 字符输入流
12         FileWriter fw = null;   // 字符输出流
13         try {
14             fr = new FileReader("d:\b.txt");
15             fw = new FileWriter("d:\b22.txt");
16             char[] c = new char[1024];  //  字符类char
17             int len = 0;
18             while((len=fr.read(c))!=-1){
19                 fw.write(c,0,len);
20                 fw.flush();
21             }
22         } catch (Exception e) {
23             e.printStackTrace();
24         }finally{
25             if(fw!=null){
26                 try {
27                     fw.close();
28                 } catch (IOException e) {
29                     e.printStackTrace();
30                 }finally{
31                     if(fr!=null){
32                         try {
33                             fr.close();
34                         } catch (IOException e) {
35                             e.printStackTrace();
36                         }
37                     }
38                 }
39             }
40         }
41         long e = System.currentTimeMillis();
42         System.out.println(e-s);
43     }
44 }
原文地址:https://www.cnblogs.com/zhangmenghui/p/10597754.html