利用数据流实现复制文件

package com.mashensoft;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.Writer;
import java.util.Scanner;

/**
 * 读取文本文件,读取图片,读取视频
 * 
 * @author PeicongHe
 *
 */
public class Homework {
    /**
     * 实现复制文件 运用FileInputStream,FileOutputStream对文件进行读取写入 只能一个一个字符复制(速度慢)
     */
    public static void copyFile(String sourceName, String dest) {
        // 1:读取一个文件
        // 2:写入一个文件
        try {
            FileInputStream fis = new FileInputStream(sourceName);// 源文件地址
            FileOutputStream fos = new FileOutputStream(dest);// 目的文件地址
            int a = 0;
            while ((a = fis.read()) != -1) {
                fos.write(a);
            }
            fos.flush();
            fos.close();
            fis.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * test2
     * 实现复制文件(提速) 运用FileInputStream,FileOutputStream对文件进行读取写入 引用数组byte[] 缺点:
     * 当使用数组的时候,如果文件里的字符的长度不是数组的倍数的时候,拿到的数据会重复
     */
    public static void test2() {
        byte[] myArray = new byte[3];
        try {
            InputStream fis = new FileInputStream("src/a.txx");
            while (fis.read(myArray) != -1) {
                // 如果达到文件尾,跳出循环
                for (int i = 0; i < myArray.length; i++) {
                    System.out.print((char) myArray[i]);
                }
            }
            fis.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * test3
     * 实现复制文件(提速)(改进) 运用FileInputStream,FileOutputStream对文件进行读取写入 引用数组byte[] 缺点:
     * 当文件的内容是中的时候会出现乱码
     */
    public static void test3() {
        byte[] myArray = new byte[3];
        int len;// 长度
        try {
            FileInputStream is = new FileInputStream("src/a.txt");
            while ((len = is.read(myArray)) != -1) {
                // 定义一个新的数组,防止有多余的数据
                for (int i = 0; i < len; i++) {
                    System.out.print((char) myArray[i]);
                }
            }
            is.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * test4
     * 实现复制文件 (速度快) 运用FileInputStream,FileOutputStream对文件进行读取写入 只能一个一个字符复制(速度快)
     * write
     */
    public static void test4() {
        byte[] myArray = new byte[32];
        int len;
        try {
            InputStream is = new FileInputStream("src/BangBang.flac");
            FileOutputStream os = new FileOutputStream("src/BangBangx.flac");
            // 如果到达文件尾,无法满足while的条件,不会执行while语句的内容
            while ((len = is.read(myArray)) != -1) {
                // 定义一个新的数组,防止有多余的数据
                byte descArray[] = new byte[len];
                for (int i = 0; i < len; i++) {
                    descArray[i] = myArray[i];
                }
                os.write(descArray);
            }
            is.close();
            os.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * test5
     * 实现复制文件 (速度快)(jdk)
     */
    public static void test4x(String sourceName, String targetName) {
        byte myArray[] = new byte[1024];
        int len;
        try {
            InputStream fis = new FileInputStream(sourceName);
            FileOutputStream fos = new FileOutputStream(targetName);
            while ((len = fis.read(myArray)) != -1) {
                /**
                 * os.write JDK系统是已经直接封装好的,可以直接用,速度会更快一些 os.write(数字长度,起始,末尾)
                 */
                fos.write(myArray, 0, len);
            }
            fis.close();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void test4xx() {
        try {
            FileInputStream fis = new FileInputStream("src/a.txt");
            FileOutputStream fos = new FileOutputStream("src/axx.txt");
            int len;
            byte[] myArray = new byte[1024];
            while ((len = fis.read(myArray)) != -1) {
                fos.write(myArray, 0, len);
            }
            fis.close();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("执行成功");

    }

    /**
     * test6
     * 使用缓冲区,提高速率
     * 用到FileInputStream,FileOutputStream,BufferedInputStream,BufferedOutputStream
     * 用缓冲区的好处
     * 没有缓存区,那么每read一次,就会发送一次IO操作;
有缓存区,第一次read时,会一下读取x个字节放入缓存区,然后后续的read都会从缓存中读取,当read到缓存区末尾时,会再次读取x个字节放入缓存区。
很明显,第二种方式,会减少IO操作,效率更高,缺点就是,内存占用的多。
     */
    public static void test6() {
        InputStream is;
        try {
            is = new FileInputStream("src/BangBang.flac");
            FileOutputStream os = new FileOutputStream("src/BangBangxx.flac");
            BufferedInputStream bis = new BufferedInputStream(is);
            BufferedOutputStream bos = new BufferedOutputStream(os);
            byte myArrat[] = new byte[1024];
            int len;
            while ((len = bis.read(myArrat)) != -1) {
                bos.write(myArrat, 0, len);
            }
            bos.flush();
            bis.close();
            bos.close();
            os.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * test8
     * 当我们没有文件的时候,如何使用流
     * 这里用到的是字节数组输出流和字节数组输入流
     * ByteArrayOutputStream类是在创建它的实例时,程序内部创建一个byte型别数组的缓冲区,然后利用ByteArrayOutputStream和ByteArrayInputStream的实例向数组中写入或读出byte型数据。在网络传输中我们往往要传输很多变量,我们可以利用ByteArrayOutputStream把所有的变量收集到一起,然后一次性把数据发送出去。
     */
    public static void test8() {
        String content = "hepeicongshidashuaige";
        ByteArrayInputStream is = new ByteArrayInputStream(content.getBytes());
        int a = 0;
        byte[] myArray = new byte[3];
        try {
            while ((a = is.read(myArray)) != -1) {
                for(int i=0;i<a;i++){
                System.out.print((char)myArray[i]);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * test9
     * 问题:当有中文的时候,可能会出现乱码
     */
    public static void test9() {
        String content = "你";
        byte myArray[] = content.getBytes();
        for(int i = 0;i<myArray.length;i++){
        System.out.println((char)myArray[i]);
        }
    }

    /**
     * test10
     * 问题:当有中文的时候,可能出现乱码,当有了流的时候
     * 出现乱码的原因是:字符的是字节是2个长度,而read(byte[])以一个字节扫描。将字符的两个字节拆分开了,所以出现乱码
     */
    public static void test10() {
        String content = "你";
        ByteArrayInputStream is = new ByteArrayInputStream(content.getBytes());
        byte a = 0;
        while ((a = (byte) is.read()) != -1) {
            System.out.print((char) a);
        }
    }

    /**
     * test11
     * 解决方法:利用数组Array[2],和ByteArrayInputeStream,ByteArrayOutputStream,将字符的数据存储在一起(以二的倍数存储)最后一起输出就可以避免出现乱码
     */
    public static void test11() {
        String content = "你";
        ByteArrayInputStream bis = new ByteArrayInputStream(content.getBytes());
        byte a[] = new byte[2];
        try {
            bis.read(a);
            System.out.println(new String(a));
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    /**
     * text12
     * 同理输出一串中文
     */
    public static void test12() {
        String content = "杨晓怡";
        ByteArrayInputStream bis = new ByteArrayInputStream(content.getBytes());
        byte a[] = new byte[2];
        int len;
        try {
            while((len=bis.read(a))!=-1){
                for(int i=0;i<len-1;i++){
                System.out.println(new String(a));
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void test12x() {
        try {
            FileInputStream is = new FileInputStream("src/a.txt");
            byte myArray[] = new byte[2];
            int a = 0;
            try {
                while ((a = is.read(myArray)) != -1) {
                    System.out.println(new String(myArray));
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

    }

    /**
     * test13
     * 引用字符流解决中文问题 Reader 抽象类,InputStreamReader实现类
     * 只能输出一个中文(一个字符)
     */
    public static void test13() {
        try {
            Reader reader = new InputStreamReader(new FileInputStream("src/a.txt"));
            int a = reader.read();
                    System.out.print((char)a);

            reader.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * test14
     * 引用字符流解决中文的问题 Reader抽象类,InputStreamReader实现类 读取到的数组中,一次可以读取多个字符
     *  
     */
    public static void test14() {
        Reader reader;
        try {
            reader = new InputStreamReader(new FileInputStream("src/a.txt"));
            char myArray[] = new char[3];
            int a = 0;
            while ((a = reader.read(myArray)) != -1) {
                System.out.println(new String(myArray));
            }
            reader.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * test15
     * 用字符流写入数据到文件里
     * 这里用到Writer,OutputStreamWriter,FileOutputStream
     */
    public static void test15() {
        try {
            Writer writer = new OutputStreamWriter(new FileOutputStream("src/a.txt", true));
            //指向操作对象
            //true表示可以在文件的原有内用上写入内容
            writer.write("jiajia");
            writer.flush();
            writer.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("写入成功");

    }

    /**
     * test16
     * 复制文件
     * 利用Reader,Writer,InputStreamReader,OutputStreamReader
     */
    public static void test15x() {
        try {
            Reader reader = new InputStreamReader(new FileInputStream("src/a.txt"));
            Writer writer = new OutputStreamWriter(new FileOutputStream("src/a15x.txt"));
            char myArray[] = new char[3];
            int a = 0;//长度
            while ((a = reader.read(myArray)) != -1) {
                System.out.println(new String(myArray));
                writer.write(myArray);
            }
            writer.flush();
            reader.close();
            writer.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("复制成功");
    }
    public static void test15xx(){
        try {
            Reader reader = new InputStreamReader(new FileInputStream("src/a.txt"));
            Writer writer = new OutputStreamWriter(new FileOutputStream("src/axxx.txt"));
            char[] myArray=new char[32];
            int len;
            while((len=reader.read(myArray))!=-1){
                System.out.println(new String(myArray));
                writer.write(myArray);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * 利用缓冲,使写入字符流更高效
     * 用到BufferedWriter
     * BufferedWriter(new OutputStreamWriter(new FileOutputStream(where)))
     */
    public static void test17() {
        try {
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("src/d.txt")));
            bw.write("hello,何沛聪");
            bw.flush();
            bw.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    /**
     * test18
     * 为了能够写入数据的时候更加方便,我们要引入printWriter
     * 用到PrintWriter
     */
    public static void test18() {
        try {
            PrintWriter pw = new PrintWriter("src/e.txt");
            pw.print("回车\n");
            pw.print("新行\r");
            pw.print(18);
            pw.flush();
            pw.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        System.out.println("执行成功");
    }
    /**
     * 读取文件的时候更加简单 scanner
     */
    public static void test19() {
        Scanner sc;
        try {
            sc = new Scanner(new FileInputStream("src/a15x.txt"));
            while (sc.hasNextLine()) {
                System.out.println(sc.nextLine());
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 复制 利用print - sanner
     */
    public static void test19x() {
        try {
            Scanner sc=new Scanner(new FileInputStream("src/a.txt"));
            PrintWriter pw = new PrintWriter("src/acp.txt");
            while(sc.hasNextLine()){
//              System.out.println(sc.nextLine());
                pw.print(sc.nextLine());
            }
            sc.close();
            pw.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        System.out.println("执行成功");
    }
    /**
     * 复制 利用BufferedWriter - Scanner
     */
    public static void test19xx(){
        try {
            Scanner sc=new Scanner(new FileInputStream("src/a.txt"));
            BufferedWriter bfw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("src/acpx.txt")));
            while(sc.hasNextLine()){
                bfw.write(sc.nextLine());
            }
            //将缓存区的数据强制写入到目标文件
            bfw.flush();
            bfw.close();
            sc.close();
            System.out.println("执行成功");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }
    public static void main(String[] args) {
        long a = System.currentTimeMillis();
        // copyFile("a.txt","b.txt");
        // test4();
        // test4x("BangBang.flac","BangBangtx.flac");
        test19xx();
        System.out.println("\r<br>执行耗时 : " + (System.currentTimeMillis() - a) / 1000f + " 秒 ");
    }

}

猜你喜欢

转载自blog.csdn.net/PeicongHe/article/details/54917882