package com.sf.day21;

import org.junit.Test;
import java.io.*;

/**
 * 字节流
 *
 */
public class Test01 {

    @Test
    public void t1() throws Exception {
      FileInputStream fileInputStream =  new FileInputStream("D:\\a.txt");
      //读取数据
        int read = fileInputStream.read();
        System.out.println((char) read);
        int read1 = fileInputStream.read();
        System.out.println((char) read1);
        int read2 = fileInputStream.read();
        System.out.println(read2);
        fileInputStream.close();
    }

    @Test
    public void t2() throws IOException {
      FileInputStream fileInputStream =   new FileInputStream("D:\\a.txt");
      int a;
      while ((a = fileInputStream.read())!=-1){
          System.out.print((char) a);
      }
      fileInputStream.close();
    }


    @Test
    public void t3() throws IOException{
        //创建流对象
        FileInputStream fileInputStream = new FileInputStream("D:\\a.txt");
        int len;
        byte[] data = new byte[2];
        while ((len = fileInputStream.read(data)) != -1){
            //有效字符
            System.out.println(new String(data,0,len));
        }
        //关闭资源
        fileInputStream.close();
    }

    /**
     * FileOutputStream、
     * 将数据写到某一个文件中
     */
    @Test
    public void t4() throws IOException {
        FileOutputStream fileOutputStream = new FileOutputStream("sf.txt",true);
        fileOutputStream.write(97);
        fileOutputStream.write(98);
        fileOutputStream.write(99);
        fileOutputStream.write(100);
        fileOutputStream.close();
    }

    @Test
    public void t5() throws IOException {
        FileOutputStream fileOutputStream = new FileOutputStream("sf1.txt",true);
        String str = "loveCoding1";
        byte[] bytes = str.getBytes();
        fileOutputStream.write(bytes,0,bytes.length);
        fileOutputStream.close();
    }

    /**
     * 文件的复制
     * 1.创建input
     * 创建 out
     *
     * 思路：   文件（硬盘）  -- 输入   ----输出 硬盘
     */
    @Test
    public void t6() throws IOException {
        FileInputStream inputStream = new FileInputStream("sf.txt");
        FileOutputStream outputStream = new FileOutputStream("sf-1.txt");
        //读取文件
        byte[] data = new byte[1024];
        int len;
        //有效长度
        while ((len = inputStream.read(data)) != -1){
            outputStream.write(data,0,len);
        }
        inputStream.close();
        outputStream.close();
        System.out.println("文件复制完成");
    }


    /**
     * 缓冲流的效率测试
     * 复制一个大文件
     */
    @Test
    public void t7()  {
        //当前时间
        long startTime = System.currentTimeMillis();
//        FileInputStream fileInputStream = new FileInputStream("D:\\a.txt");
//        BufferedInputStream inputStream = new BufferedInputStream(fileInputStream);
//        String srcPath = "D:\\ruanjian\\WeChat\\WeChat.exe";
//        String destPath = "D:\\ruanjian\\WeChat\\01_WeChat.exe";
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream("D:\\ruanjian\\WeChat\\WeChat.exe");
            fos = new FileOutputStream("D:\\ruanjian\\WeChat\\02_WeChat.exe");
            //进行读写操作  创建一个指定容量的数组byte
            byte[] data = new byte[1000];
            int len;
            while ((len = fis.read(data)) != -1){
                fos.write(data,0,len);
            }
            System.out.println("复制完成");
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                fis.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            try {
                fos.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        long endTime = System.currentTimeMillis();
        System.out.println("当前花费的时间为====="+(endTime-startTime));

    }

    @Test
    public void t8(){
        long startTime = System.currentTimeMillis();
        FileInputStream fis = null;
        FileOutputStream fos = null;
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
//        fis = new FileInputStream("D:\\ruanjian\\WeChat\\WeChat.exe");
//        fos = new FileOutputStream("D:\\ruanjian\\WeChat\\02_WeChat.exe");
            try {
                bis = new BufferedInputStream(new FileInputStream("D:\\ruanjian\\WeChat\\WeChat.exe"));
                bos = new BufferedOutputStream(new FileOutputStream("D:\\ruanjian\\WeChat\\03_WeChat.exe"));
                //读取数据
                byte[] data = new byte[1000];
                int len;
                while ((len = bis.read(data)) != -1){
                    bos.write(data,0,len);
                }
            } catch (FileNotFoundException e) {
                throw new RuntimeException(e);
            } catch (IOException e) {
                throw new RuntimeException(e);
            } finally {
                try {
                    bis.close();
                    bos.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        System.out.println("复制完成");
        long endTime = System.currentTimeMillis();
        System.out.println("缓存流所用的时间==="+(endTime-startTime));
    }

    /**
     * 转换流
     */
    @Test
    public void t9() throws IOException{
        FileReader fileReader = new FileReader("D:\\File_GBK.txt");
        int len;
        while ((len = fileReader.read()) != -1){
            System.out.println((char) len);
        }
        fileReader.close();
    }


    /**
     * 转换流
     */
    @Test
    public void t10() throws IOException {
        //指定文件的路径
        String fileName = "D:\\File_GBK.txt";
        //创建流对象
        InputStreamReader isr = new InputStreamReader(new FileInputStream(new File(fileName)),"UTF-8");
        int charData;
        while ((charData = isr.read()) !=-1){
            System.out.println((char) charData);
        }
        isr.close();
    }

    /**
     * 字符  -- 字节   根据指定的字符集、
     * outputStreamWriter
     */
    @Test
    public void  t11() throws IOException {
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(new File("D:\\File_GBK.txt"),true), "UTF-8");
        osw.write("我是中国人");
        osw.close();
        System.out.println("完成！！！！");
    }


}
