java-IO-文件(File)读写操作

  •  常用IO流体系图
  1. InputStream(输入流):用于读取数据
  2. OutputStream(输出流):用于输出数据

  • 字节流:一次读取一个字节
// 测试文件大小:2087KB
// 普通字节流,一次读取一个字节
public static void ioOption() throws IOException {

    // 获取文件 字节输入流,如果文件不存在抛出异常
    FileInputStream fis = new FileInputStream(new File("D:/demo.txt"));
    // 获取文件 字节输出流,如果文件不存在自动创建文件
    FileOutputStream fos = new FileOutputStream(new File("D:/copy.txt"));

    int len;
    // 一次读取一个字节
    // len 表示读取到的字节数,如果没有数据则返回 -1
    while ((len = fis.read())!= -1){
        fos.write(len);
    }

    fis.close();
    fos.close();
}
输出:
耗时(毫秒):7424


// 测试文件大小:2087KB
// 缓冲字节流,一次读取一个字节
public static void ioOption() throws IOException {

    // 获取文件 字节输入流,如果文件不存在抛出异常
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File("D:/demo.txt")));
    // 获取文件 字节输出流,如果文件不存在自动创建文件
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File("D:/copy.txt")));

    int len;
    // 一次读取一个字节
    // len 表示读取到的字节数,如果没有数据则返回 -1
    while ((len = bis.read())!= -1){
        bos.write(len);
    }

    bis.close();
    bos.close();
}
输出:
耗时(毫秒):93
  • 字节流:一次读取一个字节数组
// 测试文件大小:2087KB
// 普通字节流,一次读取一个字节数组
public static void ioOption() throws IOException {

    // 获取文件 字节输入流,如果文件不存在抛出异常
    FileInputStream fis = new FileInputStream(new File("D:/demo.txt"));
    // 获取文件 字节输出流,如果文件不存在自动创建文件
    FileOutputStream fos = new FileOutputStream(new File("D:/copy.txt"));
    // 字节数组
    byte[] bytes = new byte[1024];

    int len;
    // 一次读取一个 bytes 大小的数据
    // len 表示读取到的字节数,如果没有数据则返回 -1
    while ((len = fis.read(bytes))!= -1){
        fos.write(bytes,0,len);
    }

    fis.close();
    fos.close();
}
输出:
耗时(毫秒):17


// 测试文件大小:2087KB
// 缓冲字节流,一次读取一个字节数组
public static void ioOption() throws IOException {

    // 获取文件 字节输入流,如果文件不存在抛出异常
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File("D:/demo.txt")));
    // 获取文件 字节输出流,如果文件不存在自动创建文件
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File("D:/copy.txt")));
    // 字节数组
    byte[] bytes = new byte[1024];

    int len;
    // 一次读取一个 bytes 大小的数据
    // len 表示读取到的字节数,如果没有数据则返回 -1
    while ((len = bis.read(bytes))!= -1){
        bos.write(bytes,0,len);
    }

    bis.close();
    bos.close();
}
输出:
耗时(毫秒):7
  • 字节流:一次性读取所有字节
// 测试文件大小:2087KB
public static void ioOption() throws IOException {

    // 获取文件 字节输入流,如果文件不存在抛出异常
    FileInputStream fis = new FileInputStream(new File("D:/demo.txt"));
    // 获取文件 字节输出流,如果文件不存在自动创建文件
    FileOutputStream fos = new FileOutputStream(new File("D:/copy.txt"));
    // 字节数组
    byte[] bytes = new byte[fis.available()];

    int len = 0;
    // 一次读取一个 bytes 大小的数据
    // len 表示读取到的字节数,如果没有数据则返回 -1
    while ((len = fis.read(bytes))!= -1){
        fos.write(bytes,0,len);
    }

    fis.close();
    fos.close();
}
输出:
耗时(毫秒):6
  • 字符流:一次读取一个字符
// 测试文件大小:2087KB
public static void ioOption() throws IOException {

    // 获取文件 字符输入流,如果文件不存在抛出异常
    BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("D:/demo.txt")),"GBK"));
    // 获取文件 字符输出流,如果文件不存在自动创建文件
    BufferedWriter bw  = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("D:/copy.txt")),"GBK"));

    int cha;
    // 一次读取一个字符,读到换行符会自动换行
    // cha 表示读取到的字符,如果没有数据则返回 -1
    while ((cha = br.read())!= -1){
        bw.write(cha);
    }

    br.close();
    bw.flush();
    bw.close();
}
输出:
耗时(毫秒):110
  • 字符流:一次读取一行
// 测试文件大小:2087KB
public static void ioOption() throws IOException {

    // 获取文件 字符输入流,如果文件不存在抛出异常
    BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("D:/demo.txt")),"GBK"));
    // 获取文件 字符输出流,如果文件不存在自动创建文件
    BufferedWriter bw  = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("D:/copy.txt")),"GBK"));

    String content;
    // 一次读取一行,需要手动换行
    // content 表示读取到的内容,如果没有数据则返回 null
    while ((content = br.readLine())!= null){
        bw.write(content);
        bw.newLine();
    }

    br.close();
    bw.flush();
    bw.close();
}
输出:
耗时(毫秒):102


// 测试文件大小:2087KB
public static void ioOption() throws IOException {

    // 获取文件 字符输入流,如果文件不存在抛出异常
    BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("D:/demo.txt")),"GBK"));
    // 获取文件 字符输出流,如果文件不存在自动创建文件
    PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(new File("D:/copy.txt")),"GBK"));

    String content;
    // 一次读取一行
    // content 表示读取到的内容,如果没有数据则返回 null
    while ((content = br.readLine())!= null){
        pw.println(content);
    }

    br.close();
    pw.flush();
    pw.close();
}
输出:
耗时(毫秒):100
  • 字符流:一次读取一个字符数组
// 测试文件大小:2087KB
public static void ioOption() throws IOException {

    // 获取文件 字符输入流,如果文件不存在抛出异常
    BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("D:/demo.txt")),"GBK"));
    // 获取文件 字符输出流,如果文件不存在自动创建文件
    BufferedWriter bw  = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("D:/copy.txt")),"GBK"));

    // 字符数组
    char[] chars = new char[1024];
    int cha;
    // 一次读取一个字符,读到换行符会自动换行
    // cha 表示读取到的字符,如果没有数据则返回 -1
    while ((cha = br.read(chars))!= -1){
        bw.write(chars,0,cha);
    }

    br.close();
    bw.flush();
    bw.close();
}
输出:
耗时(毫秒):71

总结:

  1. 从复制文件(测试文件为纯文本)来说,感觉字符流表现比较平庸,字节流拥有不俗的表现,不知道是不是我的测试哪里不周全?希望指正

参考:

  1. Java IO流学习总结一:输入输出流    https://www.cnblogs.com/zhaoyanjun/p/6292384.html
  2. JAVA中的基本IO流及高效缓冲流读写文件的4中方法    https://blog.csdn.net/Bear_Rider/article/details/79841494

猜你喜欢

转载自blog.csdn.net/m0_37524661/article/details/87880358