使用FileInputStream 和 FileOutputStream 进行copy流 和 使用FileReader 和 FileWriter 进行 copy流案例:

使用FileInputStream 和 FileOutputStream 进行copy流案例:

package com.javaSe.Copy;


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;


/*
使用FileInputStream + FileOutputStream完成文件的拷贝
拷贝的过程应该是一边读,一边写。
使用以上的字节流拷贝文件的时候,文件类型随意写,万能的。什么样的文件都能拷贝
*/
public class CopyTest01 {
    public static void main(String[] args) {
        FileInputStream fis = null;
        FileOutputStream fos = null;
    
        try {
            // 创建一个输入流对象
            fis = new FileInputStream("F:\\略略略\\1\\137349.mp4");
            
            // 创建一个输出流对象
            fos = new FileOutputStream("H:\\137349.mp4");
            
            // 最核心的:一边读一边写
            byte[] bytes = new byte[1024 * 1024]; // 1MB(一次最多拷贝1MB)
            int readCount = 0;
            while ((readCount = fis.read()) != -1){
                fos.write(bytes,0,readCount);
            }
            
            fos.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 分开try 不要一起try
            // 一起try的时候,其中一个出现异常,可能会影响到另一个流的关闭。
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
    
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

 

 
使用FileReader 和 FileWriter进行 copy流案例:
package com.javaSe.Copy;


import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;


/*
使用FileReader 和 FileWriter 进行拷贝的话,只能拷贝普通文本文件。
*/
public class CopyTest02 {
    public static void main(String[] args) {
        FileReader fr = null;
        FileWriter fw = null;
    
        try {
            //
            fr = new FileReader("Stream\\src\\com\\javaSe\\Copy\\CopyTest02.java");
            //
            fw = new FileWriter("CopyTest02.java");
            
            // 一边读、 一边写
            char[] chars = new char[1024 * 1024];// 1MB
            int readCount = 0;
            while ((readCount = fr.read(chars)) != -1){
                fw.write(chars,0,readCount);
            }
            
            // 刷新
            fw.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fr != null) {
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
    
            if (fw != null) {
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    
    }
}

 

posted @ 2020-08-09 20:11  xlwu丶lz  阅读(332)  评论(0编辑  收藏  举报