【无标题】

package com.luo.koashi;

import java.io.*;
import java.util.Iterator;

/**
 * @Author: luosiyu
 * @Description: io流:字符流
 * @Version: 1.0
 */

public class test2 {
//    private protected default public
//    implements
//    super
//    RuntimeException
//    String  StringBuffer  StringBuilder
//    throw throws
//    str.length();
//    Collection
//    Reader Writer
//    BufferedInputStream  BufferedOutputStream
//    InputStreamReader  OutputStreamWriter
//    start 启动新线程 sleep() 休眠
//    Iterator hashNext() next()
//    Comparable排序接口
    public static void main(String[] args) throws Exception {
//        copyFile();
//        copyByfileReader();
//        copy123();
        copy1234();
    }
    //第一种
    public static void copyFile() throws Exception{
        InputStream in = new FileInputStream("E:/test.txt");
        OutputStream out = new FileOutputStream("E:/copy.txt");
        int len;
        byte[] buff = new byte[2048];
        long startTime = System.currentTimeMillis();
        while((len = in.read(buff)) != -1){
            System.out.println((char)len);
            out.write(buff,0,len);
        }
        long endTime = System.currentTimeMillis();
        System.out.println("所用时间:" + (endTime - startTime));
        in.close();
        out.close();
    }
    //第二种
    public static void copyByfileReader() {
        FileReader fr = null;
        FileWriter fw = null;
        try {
            fr = new FileReader("E:/test.txt");
            fw = new FileWriter("E:/copy.txt");
            int len;
            while((len = fr.read()) != -1){
                System.out.print((char)len);
                fw.write(len);
            }
        } catch (Exception 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();
                }
            }

        }

    }


    public static void copy123(){
        // 在外部保存为了finally中可以访问到
        InputStream inp = null;
        OutputStream oup = null;
        try {
            inp = new FileInputStream("E:/test.txt");
            oup = new FileOutputStream("E:/copy.txt");
            byte[] arr = inp.readAllBytes();
            oup.write(arr);
            System.out.println("复制成功!");
        } catch (IOException e) {
            e.printStackTrace();
        } finally { // finally中释放资源
            try {
                if (inp != null) { // 非空校验
                    inp.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (oup != null) { // 非空校验
                    oup.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

//jdk7
//在括号中放置资源对象, 并且是只能放置资源对象, 用完会自动关闭;自动调用资源对象的close方法关闭资源(即使出现异常也会做关闭的操作)
    public static void copy1234(){
            try(InputStream inp = new FileInputStream("E:/test.txt");
                OutputStream oup = new FileOutputStream("E:/copy.txt")) {
                byte[] arr = inp.readAllBytes();
                oup.write(arr);
                System.out.println("复制成功!");
            } catch (IOException e) {
                e.printStackTrace();
            }

    }
}
package com.luo.koashi;

import java.io.*;

/**
 * @Author: luosiyu
 * @Description: io流:字符流
 * @Version: 1.0
 */
public class test2 {
    public static void main(String[] args) throws Exception {
//        copyFile();
//        copyByfileReader();
//        copy123();
        copy1234();
    }
    //第一种
    public static void copyFile() throws Exception{
        InputStream in = new FileInputStream("E:/test.txt");
        OutputStream out = new FileOutputStream("E:/copy.txt");
        int len;
        byte[] buff = new byte[2048];
        long startTime = System.currentTimeMillis();
        while((len = in.read(buff)) != -1){
            System.out.println((char)len);
            out.write(buff,0,len);
        }
        long endTime = System.currentTimeMillis();
        System.out.println("所用时间:" + (endTime - startTime));
        in.close();
        out.close();
    }
    //第二种
    public static void copyByfileReader() {
        FileReader fr = null;
        FileWriter fw = null;
        try {
            fr = new FileReader("E:/test.txt");
            fw = new FileWriter("E:/copy.txt");
            int len;
            while((len = fr.read()) != -1){
                System.out.print((char)len);
                fw.write(len);
            }
        } catch (Exception 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();
                }
            }

        }

    }


    public static void copy123(){
        // 在外部保存为了finally中可以访问到
        InputStream inp = null;
        OutputStream oup = null;
        try {
            inp = new FileInputStream("E:/test.txt");
            oup = new FileOutputStream("E:/copy.txt");
            byte[] arr = inp.readAllBytes();
            oup.write(arr);
            System.out.println("复制成功!");
        } catch (IOException e) {
            e.printStackTrace();
        } finally { // finally中释放资源
            try {
                if (inp != null) { // 非空校验
                    inp.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (oup != null) { // 非空校验
                    oup.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

//jdk7
//在括号中放置资源对象, 并且是只能放置资源对象, 用完会自动关闭;自动调用资源对象的close方法关闭资源(即使出现异常也会做关闭的操作)
    public static void copy1234(){
            try(InputStream inp = new FileInputStream("E:/test.txt");
                OutputStream oup = new FileOutputStream("E:/copy.txt")) {
                byte[] arr = inp.readAllBytes();
                oup.write(arr);
                System.out.println("复制成功!");
            } catch (IOException e) {
                e.printStackTrace();
            }

    }
}