Java NIO 读取文件、写入文件、读取写入混合

朴灿烈づ我的快乐病毒、 2021-10-29 07:08 612阅读 0赞

前言

Java NIO(new/inputstream outputstream)使用通道、缓冲来操作流,所以要深刻理解这些概念,尤其是,缓冲中的数据结构(当前位置(position)、限制(limit)、容量(capacity)),这些知识点要通过写程序慢慢体会。

NIO vs 传统IO

NIO是面向缓冲、通道的;传统IO面向流

通道是双向的既可以写、也可以读;传统IO只能是单向的

NIO可以设置为异步;传统IO只能是阻塞,同步的

缓冲区结构图

NIO是面向缓冲区的,缓冲区可以理解为一块内存,有大小。缓冲区有位置、界限、容量几个概念。

capacity:容量,缓冲区的大小

limit:限制,表示最大的可读写的数量

position:当前位置,每当读写,当前位置都会加一

flip和clear方法,内部就操作这三个变量。

Center

缓冲区常用方法

clear:将当前位置设置为0,限制设置为容量,目的是尽最大可能让字节,由通道读取到缓冲中

flip:当前位置置为限制,然后将当前位置置为0,目的是将有数据部分的字节,由缓冲写入到通道中。通常用在读与写之间。

读写文件代码

复制代码

  1. 1 package com.nio;
  2. 2
  3. 3 import java.io.*;
  4. 4 import java.nio.ByteBuffer;
  5. 5 import java.nio.channels.FileChannel;
  6. 6 import java.nio.charset.Charset;
  7. 7
  8. 8 public class TestJavaNio {
  9. 9
  10. 10 public static String pathname = "d://read.txt";
  11. 11 public static String filename = "d://write.txt";
  12. 12
  13. 13 @SuppressWarnings("resource")
  14. 14 public static void main(String[] args) {
  15. 15 readNIO();
  16. 16 writeNIO();
  17. 17 //testReadAndWriteNIO();
  18. 18 }
  19. 19
  20. 20 public static void readNIO() {
  21. 21 FileInputStream fin = null;
  22. 22 try {
  23. 23 fin = new FileInputStream(new File(pathname));
  24. 24 FileChannel channel = fin.getChannel();
  25. 25
  26. 26 int capacity = 1000;// 字节
  27. 27 ByteBuffer bf = ByteBuffer.allocate(capacity);
  28. 28 System.out.println("限制是:" + bf.limit() + ",容量是:" + bf.capacity() + " ,位置是:" + bf.position());
  29. 29 int length = -1;
  30. 30
  31. 31 while ((length = channel.read(bf)) != -1) {
  32. 32
  33. 33 /*
  34. 34 * 注意,读取后,将位置置为0,将limit置为容量, 以备下次读入到字节缓冲中,从0开始存储
  35. 35 */
  36. 36 bf.clear();
  37. 37 byte[] bytes = bf.array();
  38. 38 System.out.println("start..............");
  39. 39
  40. 40 String str = new String(bytes, 0, length);
  41. 41 System.out.println(str);
  42. 42 //System.out.write(bytes, 0, length);
  43. 43
  44. 44 System.out.println("end................");
  45. 45
  46. 46 System.out.println("限制是:" + bf.limit() + "容量是:" + bf.capacity() + "位置是:" + bf.position());
  47. 47
  48. 48 }
  49. 49
  50. 50 channel.close();
  51. 51
  52. 52 } catch (FileNotFoundException e) {
  53. 53 e.printStackTrace();
  54. 54 } catch (IOException e) {
  55. 55 e.printStackTrace();
  56. 56 } finally {
  57. 57 if (fin != null) {
  58. 58 try {
  59. 59 fin.close();
  60. 60 } catch (IOException e) {
  61. 61 e.printStackTrace();
  62. 62 }
  63. 63 }
  64. 64 }
  65. 65 }
  66. 66
  67. 67 public static void writeNIO() {
  68. 68 FileOutputStream fos = null;
  69. 69 try {
  70. 70
  71. 71 fos = new FileOutputStream(new File(filename));
  72. 72 FileChannel channel = fos.getChannel();
  73. 73 ByteBuffer src = Charset.forName("utf8").encode("你好你好你好你好你好");
  74. 74 // 字节缓冲的容量和limit会随着数据长度变化,不是固定不变的
  75. 75 System.out.println("初始化容量和limit:" + src.capacity() + ","
  76. 76 + src.limit());
  77. 77 int length = 0;
  78. 78
  79. 79 while ((length = channel.write(src)) != 0) {
  80. 80 /*
  81. 81 * 注意,这里不需要clear,将缓冲中的数据写入到通道中后 第二次接着上一次的顺序往下读
  82. 82 */
  83. 83 System.out.println("写入长度:" + length);
  84. 84 }
  85. 85
  86. 86 } catch (FileNotFoundException e) {
  87. 87 e.printStackTrace();
  88. 88 } catch (IOException e) {
  89. 89 e.printStackTrace();
  90. 90 } finally {
  91. 91 if (fos != null) {
  92. 92 try {
  93. 93 fos.close();
  94. 94 } catch (IOException e) {
  95. 95 e.printStackTrace();
  96. 96 }
  97. 97 }
  98. 98 }
  99. 99 }
  100. 100
  101. 101 public static void testReadAndWriteNIO() {
  102. 102 FileInputStream fin = null;
  103. 103 FileOutputStream fos = null;
  104. 104 try {
  105. 105 fin = new FileInputStream(new File(pathname));
  106. 106 FileChannel channel = fin.getChannel();
  107. 107
  108. 108 int capacity = 100;// 字节
  109. 109 ByteBuffer bf = ByteBuffer.allocate(capacity);
  110. 110 System.out.println("限制是:" + bf.limit() + "容量是:" + bf.capacity() + "位置是:" + bf.position());
  111. 111 int length = -1;
  112. 112
  113. 113 fos = new FileOutputStream(new File(filename));
  114. 114 FileChannel outchannel = fos.getChannel();
  115. 115
  116. 116
  117. 117 while ((length = channel.read(bf)) != -1) {
  118. 118
  119. 119 //将当前位置置为limit,然后设置当前位置为0,也就是从0到limit这块,都写入到同道中
  120. 120 bf.flip();
  121. 121
  122. 122 int outlength = 0;
  123. 123 while ((outlength = outchannel.write(bf)) != 0) {
  124. 124 System.out.println("读," + length + "写," + outlength);
  125. 125 }
  126. 126
  127. 127 //将当前位置置为0,然后设置limit为容量,也就是从0到limit(容量)这块,
  128. 128 //都可以利用,通道读取的数据存储到
  129. 129 //0到limit这块
  130. 130 bf.clear();
  131. 131
  132. 132 }
  133. 133 } catch (FileNotFoundException e) {
  134. 134 e.printStackTrace();
  135. 135 } catch (IOException e) {
  136. 136 e.printStackTrace();
  137. 137 } finally {
  138. 138 if (fin != null) {
  139. 139 try {
  140. 140 fin.close();
  141. 141 } catch (IOException e) {
  142. 142 e.printStackTrace();
  143. 143 }
  144. 144 }
  145. 145 if (fos != null) {
  146. 146 try {
  147. 147 fos.close();
  148. 148 } catch (IOException e) {
  149. 149 e.printStackTrace();
  150. 150 }
  151. 151 }
  152. 152 }
  153. 153 }
  154. 154
  155. 155 }

复制代码

发表评论

表情:
评论列表 (有 0 条评论,612人围观)

还没有评论,来说两句吧...

相关阅读

    相关 java读取文件写入文件

    Java 中 IO 流 Java 中 IO 流分为几种? 按照流的流向分,可以分为输入流和输出流; 按照操作单元划分,可以划分为字节流和字符流; 按照流的角色划分为节点