Java IO流 三

处理文本

最好来说:
在这里插入图片描述

实现图片复制

package demo10;

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

public class IOTest {
    public static void main(String[] args) throws IOException {
        File src = new File("1.png");
        File dest = new File("2.png");
        
        FileInputStream fis = new FileInputStream(src);
        FileOutputStream fos = new FileOutputStream(dest);

        byte[] buffer = new byte[5];
        int len;
        while ((len = fis.read(buffer))!=-1){
            fos.write(buffer,0,len);
        }

        fis.close();
        fos.close();
    }
}

在这里插入图片描述
在这里插入图片描述

实现了图片复制。

指定路径下文件的复制

package demo10;

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

public class IOTest {
    public static void main(String[] args) throws IOException {
        long start = System.currentTimeMillis();
        copyFile("1.mp4","2.mp4");
        long end = System.currentTimeMillis();
        System.out.println((end - start)/1000.0);
    }
    public static void copyFile(String srcPath,String destPath) throws IOException {
        File src = new File(srcPath);
        File dest = new File(destPath);

        FileInputStream fis = new FileInputStream(src);
        FileOutputStream fos = new FileOutputStream(dest);

        byte[] buffer = new byte[5];
        int len;
        while ((len = fis.read(buffer))!=-1){
            fos.write(buffer,0,len);
        }

        fis.close();
        fos.close();
    }
}

7.635

复制一个21s的mp4文件花了7.635s。

将buffer数组改为100大小
在这里插入图片描述

0.607

缓冲流字节型实现非文本文件复制

作用:提高流的读取和写入的速度。
注意到缓冲流是处理流,也就是要作用到节点流,所以要先来一个节点流。

package demo10;

import java.io.*;

public class BufferTest {
    public static void main(String[] args) throws IOException {
        //1.造文件
        File src = new File("1.png");
        File dest = new File("2.png");
        //2.造流
        //2.1造节点流
        FileInputStream fis = new FileInputStream(src);
        FileOutputStream fos = new FileOutputStream(dest);
        //2.2造处理流也就是缓冲流
        BufferedInputStream bis = new BufferedInputStream(fis);
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        //3.复制细节:读取和写入
        byte[] b = new byte[1024];
        int len;
        while ((len = bis.read(b))!=-1){
            bos.write(b,0,len);
        }
        //4.资源关闭 先关外面的也就是处理流,再关里面的也就是节点流
        //其实关闭外层流,内层流自动就关了,所以没必要再写。
        bis.close();
        bos.close();
        /*fis.close();
        fos.close();*/

    }
}


缓冲流与节点流速度对比

package demo10;

import java.io.*;

public class BufferTest {
    public static void main(String[] args) throws IOException {
        long start = System.currentTimeMillis();
        //1.造文件
        File src = new File("1.mp4");
        File dest = new File("2.mp4");
        //2.造流
        //2.1造节点流
        FileInputStream fis = new FileInputStream(src);
        FileOutputStream fos = new FileOutputStream(dest);
        //2.2造处理流也就是缓冲流
        BufferedInputStream bis = new BufferedInputStream(fis);
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        //3.复制细节:读取和写入
        byte[] b = new byte[1024];
        int len;
        while ((len = bis.read(b))!=-1){
            bos.write(b,0,len);
        }
        //4.资源关闭 先关外面的也就是处理流,再关里面的也就是节点流
        //其实关闭外层流,内层流自动就关了,所以没必要再写。
        bis.close();
        bos.close();
        long end = System.currentTimeMillis();
        System.out.println(end - start);
        /*fis.close();
        fos.close();*/

    }
}

24

而之前的节点流是0.607s也就是607ms,是将近30倍,这里我们用的都是1024大小的byte数组。

提高读写速度的原因:内部提供了一个缓冲区。

缓冲流对文本文件的复制

package demo10;

import java.io.*;

public class BufferTest {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(new File("hello.txt")));
        BufferedWriter bw = new BufferedWriter(new FileWriter(new File("hi.txt")));

        char[] c = new char[1024];
        int len;
        while ((len = br.read(c)) != -1){
            bw.write(c,0,len);
        }
        br.close();
        bw.close();

    }
}

上述方式仍然使用char[]数组
方式二:使用String和BufferedReader里的readLine()方法。
在这里插入图片描述

package demo10;

import java.io.*;

public class BufferTest {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(new File("hello.txt")));
        BufferedWriter bw = new BufferedWriter(new FileWriter(new File("hi.txt")));

        String data;
        while ((data = br.readLine()) != null){
            bw.write(data);//data中不包含换行符
        }
        br.close();
        bw.close();

    }
}

在这里插入图片描述

在这里插入图片描述

所以这里要加一个换行符
在这里插入图片描述
或者调用newLine()方法
在这里插入图片描述

总结

在这里插入图片描述
此外,处理流中还有flush方法,调用它可以把缓冲区的内容写出去,只不过缓冲流自动调用它,所以在这里不用管

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值