• 技术文章 >java >java基础

    java IO流效率对比

    小妮浅浅小妮浅浅2021-02-09 13:44:07原创3285

    本教程操作环境:windows7系统、java10版,DELL G3电脑。

    1.缓冲流说明

    缓冲流,也叫高效流,是对4个基本的FileXxx 流的增强,所以也是4个流,按照数据类型分类:

    缓冲流的基本原理,是在创建流对象时,会创建一个内置的默认大小的缓冲区数组,通过缓冲区读写,减少系统IO次数,从而提高读写的效率。

    2.字符流和缓冲字符流的对比

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    39

    40

    41

    42

    43

    44

    45

    46

    47

    48

    49

    50

    51

    52

    53

    54

    55

    56

    57

    58

    59

    60

    61

    62

    63

    64

    65

    66

    67

    68

    69

    70

    71

    72

    73

    74

    75

    76

    77

    78

    79

    80

    81

    82

    83

    public class IOTest {

    public static void main(String[] args) throws IOException {

    // 数据准备

    dataReady();

      

    File data = new File("C:/Mu/data.txt");

    File a = new File("C:/Mu/a.txt");

    File b = new File("C:/Mu/b.txt");

    File c = new File("C:/Mu/c.txt");

      

    long start = System.currentTimeMillis();

    copy(data, a);

    long end = System.currentTimeMillis();

      

    long start2 = System.currentTimeMillis();

    copyChars(data, b);

    long end2 = System.currentTimeMillis();

      

    long start3 = System.currentTimeMillis();

    bufferedCopy(data, c);

    long end3 = System.currentTimeMillis();

      

    System.out.println("普通字节流1耗时:" + (end - start) + " ms,文件大小:" + a.length() / 1024 + " kb");

    System.out.println("普通字节流2耗时:" + (end2 - start2) + " ms,文件大小:" + b.length() / 1024 + " kb");

    System.out.println("缓冲字节流耗时:" + (end3 - start3) + " ms,文件大小:" + c.length() / 1024 + " kb");

    }

      

    // 普通字符流不使用数组

    public static void copy(File in, File out) throws IOException {

    Reader reader = new FileReader(in);

    Writer writer = new FileWriter(out);

      

    int ch = 0;

    while ((ch = reader.read()) != -1) {

    writer.write((char) ch);

    }

    reader.close();

    writer.close();

    }

      

    // 普通字符流使用字符流

    public static void copyChars(File in, File out) throws IOException {

    Reader reader = new FileReader(in);

    Writer writer = new FileWriter(out);

      

    char[] chs = new char[1024];

    while ((reader.read(chs)) != -1) {

    writer.write(chs);

    }

    reader.close();

    writer.close();

    }

      

    // 缓冲字符流

    public static void bufferedCopy(File in, File out) throws IOException {

    BufferedReader br = new BufferedReader(new FileReader(in));

    BufferedWriter bw = new BufferedWriter(new FileWriter(out));

      

    String line = null;

    while ((line = br.readLine()) != null) {

    bw.write(line);

    bw.newLine();

    bw.flush();

    }

      

    // 释放资源

    bw.close();

    br.close();

    }

      

    // 数据准备

    public static void dataReady() throws IOException {

    StringBuilder sb = new StringBuilder();

    for (int i = 0; i < 600000; i++) {

    sb.append("abcdefghijklmnopqrstuvwxyz");

    }

    OutputStream os = new FileOutputStream(new File("C:/Mu/data.txt"));

    os.write(sb.toString().getBytes());

      

    os.close();

    System.out.println("完毕");

    }

    }

    运行结果:

    1

    2

    3

    普通字符流1耗时:1337 ms,文件大小:15234 kb

    普通字符流2耗时:82 ms,文件大小:15235 kb

    缓冲字符流耗时:205 ms,文件大小:15234 kb

    以上是java IO流效率的对比实例,缓冲字符流相对而言的优势并不是那么大,这是因为在使用上我们以缓冲流的方法用的居多,想要学习的可以在课外找寻资料。

    专题推荐:java io流效率
    上一篇:java中File类的使用 下一篇:IO流在java中的实例操作

    相关文章推荐

    • java中IO的分类• java IO流有几种• IO字节流在java中的使用• java IO字符流的用法• java字符流和字节流替换方法• java中File类的使用

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网