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

一时失言乱红尘 2021-09-14 02:30 1080阅读 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,目的是将有数据部分的字节,由缓冲写入到通道中。通常用在读与写之间。

读写文件代码

[html] view plain copy

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

发表评论

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

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

相关阅读

    相关 java读取文件写入文件

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