利用缓冲区来进行复制

1.字节流

二进制文件只能用字节流来复制

   @Test
    public void Filetest() throws IOException {
        File file = new File("C:\\Users\\injuryzy\\Desktop\\token.txt");
        File file1 = new File("C:\\Users\\injuryzy\\Desktop\\新建文本12.txt");

        FileReader fileReader = new FileReader(file);
        FileWriter fileWriter = new FileWriter(file1);
        
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
        String a;
        while ((a=bufferedReader.readLine())!=null){
            bufferedWriter.write(a);

        }
        bufferedWriter.flush();
        bufferedWriter.close();
        bufferedReader.close();
    }

字符流

字符流可以转非2进制的文件如(text,css,html)

  @Test
    public void Filetest3() throws IOException {
        File file = new File("C:\\Users\\injuryzy\\Desktop\\正则表达式30分钟入门.pdf");
        File file1 = new File("C:\\Users\\injuryzy\\Desktop\\新建文本1.pdf");
        FileInputStream fileInputStream = new FileInputStream(file);
        FileOutputStream fileOutputStream = new FileOutputStream(file1);
        //创家一个缓存输入的地方
        BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
        byte[] bytes = new byte[8];
        int len;
        while ((len=bufferedInputStream.read(bytes))!=-1){

            bufferedOutputStream.write(bytes);
        }

        bufferedOutputStream.close();
        bufferedInputStream.close();
    }

相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页