IO流--文件复制

复制输入简单测试:
 1 package file;
 2 import java.io.File;
 3 import java.io.FileReader;
 4 import java.io.FileWriter;
 5 import java.io.IOException;
 6 import java.io.Reader;
 7 import java.io.Writer;
 8 /**
 9  * 完成文件的复制  (输出  )
10  * 使用字符流
11  * @author superdrew
12  * 使用字节流复制文件没有问题,显示中文有问题
13  * 字节流  InputStream  OutputStream  
14  * 字符流  Reader         Writer
15  *     
16  *  总结:1.字节流每次读取 1 个字节  字符流每次读取 1个字符?
17  *          英文是一个一个字节  中文是两个字节(GBK)  utf-8 是三个字节
18  *  
19  *  有缺点:中转站是一个字符,使用 字符 数组    
20  */
21 public class TestFileReader {
22     public static void main(String[] args) throws IOException {
23         //创建字符流   输入流
24         Reader fr = new FileReader(new File("d:/a.jpeg"));
25         //输出流
26         Writer fw = new FileWriter(new File("d:/a.jpeg"));
27         //先使用输入流读取 再使用输出流 写入文件
28         //中转站  字符?
29         int len = fr.read();
30         while(len!=-1){
31             fw.write(len);    //向文件中写内容
32             System.out.print((char)len);
33             len = fr.read();  //继续读取内容
34         }
35         fr.close(); //关闭输入流read
36         fw.close();  //关闭输出流write
37     }
38 }
TestFileRead.java
复制输出简单测试:
 1 package file;
 2 import java.io.File;
 3 import java.io.FileNotFoundException;
 4 import java.io.FileReader;
 5 import java.io.FileWriter;
 6 import java.io.IOException;
 7 import java.io.Reader;
 8 import java.io.Writer;
 9 /**
10  * 完成文件的复制  (输出  )
11  * 使用字符流
12  * @author superdrew
13  * 使用字节流复制文件没有问题,显示中文有问题
14  * 字节流  InputStream  OutputStream  
15  * 字符流  Reader         Writer
16  *  
17  *  有缺点:1.中转站是一个字符,使用 字符 数组    
18  *       2.没有异常处理
19  *  
20  *  总结:使用字符流 代码并没有简化。只是可以正确显示中文了。
21  *      到底使用字节流还是字符流 
22  *      FileReader------>InputStreamReader------>Reader
23  *      字符流底层还是字节流
24  *      字符流只能处理文本文件 
25  *      复制文件,建议使用字节流  如果要输出中文 使用字符流
26  */
27 public class TestFileReader2 {
28     public static void main(String[] args)   {
29         Reader fr = null;
30         Writer fw = null;
31         try{
32             //创建字符流   输入流
33             fr = new FileReader(new File("d:/a.txt"));
34             //输出流
35             fw = new FileWriter(new File("d:/a3.txt"));
36             //先使用输入流读取 再使用输出流 写入文件
37             //中转站  字符?
38             char [] chbuf = new char[1024];
39             int len = fr.read(chbuf);
40             while(len!=-1){
41                 //
42                 fw.write(chbuf, 0, len);
43                 //输出本次读取的内容
44                 System.out.print(new String(chbuf,0,len));
45                 //再度
46                 len = fr.read(chbuf);
47             }
48         }catch(IOException e){
49             e.printStackTrace();
50         }finally{
51             try {
52                 if(fr!=null){
53                     fr.close();
54                 }
55             } catch (IOException e1) {
56                 e1.printStackTrace();
57             }
58             try {
59                 if(fw!=null){
60                     fw.close();
61                 }
62             } catch (IOException e) {
63                 e.printStackTrace();
64             }
65         }
66     }
67 }
TestFileReader2.java
使用字节流文件复制:
 1 package filecopy;
 2 
 3 import java.io.File;
 4 import java.io.FileInputStream;
 5 import java.io.FileNotFoundException;
 6 import java.io.FileOutputStream;
 7 import java.io.IOException;
 8 import java.io.InputStream;
 9 import java.io.OutputStream;
10 
11 /**
12  * 使用字节流完成文件的复制
13  *     FileInputStream + FileOutputStream
14  * @author superdrew
15  *         复制比较大的文件 效率很慢  因为是一个一个字节的读取 再写
16  *         改进:使用字节的数组 
17  * 
18  *         结论:1.使用字节数组做中转站效率高,
19  *             2.使用字节流可以操作任何类型的文件(文本文件和二进制文件)
20  *         没有进行异常处理
21  *         
22  */
23 public class TestFileCopy {
24     public static void main(String[] args)  {
25         //File file = new File("");
26         //创建输入流和输出流 
27         InputStream in = null;
28         OutputStream os = null;
29         try{
30             in = new FileInputStream("C:/JDK_API_1_6_zh_CN.CHM");
31             os = new FileOutputStream("C:/JDK_API_1_6_zh_CN2.CHM");
32             //使用输入流和输出流       创建中转站(使用一个字节)
33             /*int n = 0;
34             n = in.read();//读取
35             while(n!=-1){
36                 os.write(n);  //输出                
37                 n = in.read();  //继续读取文件
38             }*/
39             
40             //创建中转站 使用字节数组
41             byte [] buf = new byte[1024];
42             int n = in.read(buf);//n是本次读取的字节数
43             while(n!=-1){
44                 os.write(buf, 0, n);   //读了就写
45                 n = in.read(buf);  //再次读取
46             }
47         }catch(IOException e){
48             e.printStackTrace();
49         }finally{
50             //关闭流
51             try {
52                 if(in!=null){in.close();}    //如果 in.close()抛异常,os.close()还执行?
53             } catch (IOException e) {
54                 e.printStackTrace();
55             }
56             try{
57                 if(os!=null){os.close();}
58             }catch(IOException e){
59                 e.printStackTrace();
60             }
61         }
62     }
63 }
filecopy.java
FileInputstream字节流:
 1 package fileinputstream;
 2 import java.io.File;
 3 import java.io.FileInputStream;
 4 import java.io.FileNotFoundException;
 5 import java.io.IOException;
 6 import java.io.InputStream;
 7 /**
 8  * 功能:复制文件 前一半 (读取文件) 读取并输出
 9  * @author superdrew
10  * 
11  *     总结:1.FileInputStream
12  *             输入流  字节流  节点流
13  *         2.如何创建输入流对象
14  *             InputStream is = new FileInputStream(file);
15  *             InputStream is = new FileInputStream("d:/sxt.txt");
16  *        3.如何用输入流读文件
17  *            n = is.read();借助while循环实现
18  *        4.如何关闭输入流
19  *            is.close(); 不close行不行?
20  *            不使用一定要关闭,和外存文件产生连接
21  *        5.缺点:1.不是不能显示中文,没有正确的显示中文
22  *              2.中转站太小了,只有一个字节,影响效率
23  *        
24  *        ANSI:本地编码   大陆采用的是GBK 
25  *            GB2312    GBK     GB18030   英文占1个字节,汉字2个字节
26  *        UTF-8:    英文占1一个字节   中文占3个字节
27  *        Unicode:每个字符占两个字节    
28  */
29 public class TestFileInputStream {
30     public static void main(String[] args) throws IOException {
31         //创建文件 创建输入流
32         File file = new File("C:/JDK_API_1_6_zh_CN.CHM");
33         InputStream is = new FileInputStream(file);
34         //使用输入流读取 并输出
35         //相当于一个 中转站(水杯)
36         int n = 0;
37         //先读取一个字节
38         n = is.read();
39         //如果因为已经到达文件末尾而没有更多的数据,则返回 -1。 
40         while(n!=-1){
41             //输出 读取的内容
42             //System.out.println(n);
43             //System.out.print((char)n);
44             //输出后继续读取
45             n = is.read();
46         }
47         //关闭输入流
48         is.close();
49     }
50 }
fileinputstream.java
FileInputStream字节流输出2:
 1 package fileinputstream;
 2 import java.io.File;
 3 import java.io.FileInputStream;
 4 import java.io.IOException;
 5 import java.io.InputStream;
 6 /**
 7  * 功能:复制文件 前一半 (读取文件) 读取并输出
 8  * @author superdrew
 9  *        缺点:1.不是不能显示中文,没有正确的显示中文
10  *              2.中转站太小了,只有一个字节,影响效率
11  *        解决:中转站采用  字节数组  中文显示问题并没有真正的解决,中文可以通过字符流解决
12  *        
13  *        总结: byte [] buf = new byte [1024];
14  *            1.int n = is.read(buf); 读数据到buf(字节数组)  n 本次读取的字节数
15  *            2.字节数组和String之间的转换
16  *                字节数组-->String     new String(buf,0,n) 
17  *                String-->字节数组     str.getBytes() 
18  *             str.getBytes("utf-8");   使用指定的字符集将此 String 编码为 byte 序列
19  *        
20  *        缺点:没有进行异常处理
21  */
22 public class TestFileInputStream2 {
23     public static void main(String[] args) throws IOException {
24         String str ="0987654321";
25         byte [] b = str.getBytes();
26         //创建文件 创建输入流
27         File file = new File("E:/JDK_API_1_6_zh_CN.CHM");
28         InputStream is = new FileInputStream(file);
29         //使用输入流读取 并输出
30         //相当于一个 中转站(水桶)
31         int n =0;
32         //中转站是用一个 byte数组
33         byte [] buf = new byte [1024];
34         //先读取buf.length个字节
35         n = is.read(buf);
36         //如果因为已经到达文件末尾而没有更多的数据,则返回 -1。 
37         while(n!=-1){
38             //输出后继续读取
39             n = is.read(buf);
40         }
41         is.close();
42     }
43 }
TestFileInputStream2.java

FileInputStream3.java

 1 package fileinputstream;
 2 import java.io.File;
 3 import java.io.FileInputStream;
 4 import java.io.IOException;
 5 import java.io.InputStream;
 6 /**
 7  * 功能:复制文件 前一半 (读取文件) 读取并输出
 8  * @author superdrew
 9  *        缺点:没有进行异常处理
10  */
11 public class TestFileInputStream3 {
12     public static void main(String[] args) {
13         //创建文件 创建输入流
14         InputStream is = null;
15         try{
16             File file = new File("E:/JDK_API_1_6_zh_CN.CHM");
17             is = new FileInputStream(file);
18             //使用输入流读取 并输出
19             //相当于一个 中转站(水桶)
20             int n =0;
21 //中转站是用一个 byte数组
22             byte [] buf = new byte [1024];
23 //先读取buf.length个字节
24             n = is.read(buf);
25 //如果因为已经到达文件末尾而没有更多的数据,则返回 -1。 
26             while(n!=-1){
27                 n = is.read(buf);
28             }
29         }catch(IOException e){
30             e.printStackTrace();
31         }finally{   //关闭输入流
32             try {
33                 if(is!=null){is.close();}
34             } catch (IOException e) {
35                 e.printStackTrace();
36             }
37         }
38     }
39 }
fileinputstream3.java
FileOutputStream字节流:
将制定程序的内容写入到文件中,覆盖源文件中的内容。
package fileoutputstream;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * 复制文件的后一半(写内容到文件)  
 * 输入 input   输出out
 * @author superdrew
 *
 *    总结:1.如何创建输出流
 *                              new FileOutputStream(file)
 *            OutputStream os = new FileOutputStream(file,true);
 *                              new FileOutputStream("D:/sxt2.txt",true);
 *        2.输出的方法
 *            write(n);  os.write(buf);buf字节数组
 *        3.使用字节输出流,如果 File不存在,会自动创建
 *
 *        问题:中转站是一个字节 太小
 *            可以把字符串转成字节数组 直接输出写入到文件 不需要循环
 *        异常处理    
 *            
 */
public class TestOutPutStream {
    public static void main(String[] args)  {
        //指定内容
        String content="superdrew--drew--good--mark--helloworld!";
        OutputStream os = null;
        try{
            //指定目录
            File file =new File("C:/test.txt");
            //覆盖   追加内容??? 
            //追加内容 在 new FileOutputStream要true关键字 
            os = new FileOutputStream(file,true);
            //输出
            int n =0;
            //把内容转成字节数组
            byte [] buf =content.getBytes();
            /*for(int i =0;i<buf.length;i++){
                n = buf[i];
                //System.out.println((char)n);
                os.write(n);
            }*/
            os.write(buf);
        }catch(IOException e){
            e.printStackTrace();
        }finally{  //无论如何都得关闭输出流
            try {
                if(os!=null){os.close();}
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
原文地址:https://www.cnblogs.com/superdrew/p/8086600.html