一、流的分类

  • 数据类型
    • 字节流:传输类型为字节
    • 字符流:传输类型为字符
  • 流向
    • 输入流:读数据
    • 输出流:写数据

      为什么buffer比直接输入输出流快? 因为直接写每次调用都会触发系统调用,就会导致内核态切换,而缓冲它默认为8k,也就是每8k才会调用一次,速度就会快很多。

二、IO流类图

image-20201116141422457.png

三、文件读取

  1. public class FileBIO {
  2. /**
  3. * 字节流读取文件,需要关闭IO流
  4. */
  5. @Test
  6. public void readByte() {
  7. File file = new File("e:\\test.log");
  8. FileInputStream inputStream = null;
  9. try {
  10. inputStream = new FileInputStream(file);
  11. int len;
  12. List<Byte> list = new ArrayList<>();
  13. while ((len = inputStream.read()) != -1) {
  14. list.add((byte) len);
  15. }
  16. byte[] bytes = new byte[list.size()];
  17. for (int i = 0; i < list.size(); i++) {
  18. bytes[i] = list.get(i);
  19. }
  20. System.out.println(new String(bytes, StandardCharsets.UTF_8));
  21. } catch (Exception e) {
  22. e.printStackTrace();
  23. } finally {
  24. if (inputStream != null) {
  25. try {
  26. inputStream.close();
  27. } catch (IOException e) {
  28. e.printStackTrace();
  29. }
  30. }
  31. }
  32. }
  33. /**
  34. * 字节流读取文件,优雅方式,不用手动关闭流
  35. */
  36. @Test
  37. public void readByteElegance() {
  38. File file = new File("e:\\610.log");
  39. try (FileInputStream inputStream = new FileInputStream(file)) {
  40. long begin = System.currentTimeMillis();
  41. int len;
  42. List<Byte> list = new ArrayList<>();
  43. while ((len = inputStream.read()) != -1) {
  44. list.add((byte) len);
  45. }
  46. byte[] bytes = new byte[list.size()];
  47. for (int i = 0; i < list.size(); i++) {
  48. bytes[i] = list.get(i);
  49. }
  50. System.out.println(new String(bytes, StandardCharsets.UTF_8));
  51. System.out.println(System.currentTimeMillis() - begin);
  52. } catch (Exception e) {
  53. e.printStackTrace();
  54. }
  55. }
  56. /**
  57. * 字节流读取文件,缓冲区读取字节流
  58. */
  59. @Test
  60. public void readByteBuffer() {
  61. File file = new File("e:\\610.log");
  62. try (FileInputStream inputStream = new FileInputStream(file)) {
  63. long begin = System.currentTimeMillis();
  64. BufferedInputStream bis = new BufferedInputStream(inputStream);
  65. int len;
  66. List<Byte> list = new ArrayList<>();
  67. while ((len = bis.read()) != -1) {
  68. list.add((byte) len);
  69. }
  70. byte[] bytes = new byte[list.size()];
  71. for (int i = 0; i < list.size(); i++) {
  72. bytes[i] = list.get(i);
  73. }
  74. System.out.println(new String(bytes, StandardCharsets.UTF_8));
  75. System.out.println(System.currentTimeMillis() - begin);
  76. } catch (Exception e) {
  77. e.printStackTrace();
  78. }
  79. }
  80. /**
  81. * 字节流读取文件,缓冲区读取字节流,以数组的方式读取
  82. */
  83. @Test
  84. public void readByteBufferArray() {
  85. File file = new File("e:\\610.log");
  86. try (FileInputStream inputStream = new FileInputStream(file)) {
  87. long begin = System.currentTimeMillis();
  88. BufferedInputStream bis = new BufferedInputStream(inputStream);
  89. int len;
  90. byte[] byt = new byte[1024];
  91. List<Byte> list = new ArrayList<>();
  92. while ((len = bis.read(byt)) != -1) {
  93. for (int i = 0; i < len; i++) {
  94. list.add(byt[i]);
  95. }
  96. }
  97. byte[] bytes = new byte[list.size()];
  98. for (int i = 0; i < list.size(); i++) {
  99. bytes[i] = list.get(i);
  100. }
  101. System.out.println(new String(bytes, StandardCharsets.UTF_8));
  102. System.out.println(System.currentTimeMillis() - begin);
  103. } catch (Exception e) {
  104. e.printStackTrace();
  105. }
  106. }
  107. /**
  108. * 字符读取文件
  109. */
  110. @Test
  111. public void readChar() {
  112. File file = new File("e:\\610.log");
  113. try (FileInputStream inputStream = new FileInputStream(file)) {
  114. long begin = System.currentTimeMillis();
  115. InputStreamReader isr = new InputStreamReader(inputStream,
  116. StandardCharsets.UTF_8);
  117. int len = 0;
  118. StringBuilder builder = new StringBuilder();
  119. while ((len = isr.read()) != -1) {
  120. builder.append((char) len);
  121. }
  122. System.out.println(builder.toString());
  123. System.out.println(System.currentTimeMillis() - begin);
  124. } catch (Exception e) {
  125. e.printStackTrace();
  126. }
  127. }
  128. /**
  129. * 字符读取文件,通过缓冲流读取
  130. */
  131. @Test
  132. public void readCharBuffer() {
  133. File file = new File("e:\\test.log");
  134. try (FileInputStream inputStream = new FileInputStream(file)) {
  135. long begin = System.currentTimeMillis();
  136. BufferedReader br = new BufferedReader(
  137. new InputStreamReader(inputStream,
  138. StandardCharsets.UTF_8));
  139. String len;
  140. StringBuilder builder = new StringBuilder();
  141. while ((len = br.readLine()) != null) {
  142. //一行行读取的时候换行符就没有了,需要自己添加
  143. builder.append(len).append("\n");
  144. }
  145. builder.deleteCharAt(builder.length() - 1);
  146. System.out.println(builder.toString());
  147. System.out.println(System.currentTimeMillis() - begin);
  148. } catch (Exception e) {
  149. e.printStackTrace();
  150. }
  151. }
  152. }