Java NIO 讀取文件、寫入文件、讀取寫入混合


前言

Java NIO(new/inputstream outputstream)使用通道、緩沖來操作流,所以要深刻理解這些概念,尤其是,緩沖中的數據結構(當前位置(position)、限制(limit)、容量(capacity)),這些知識點要通過寫程序慢慢體會。

 

NIO vs  傳統IO

NIO是面向緩沖、通道的;傳統IO面向流

通道是雙向的既可以寫、也可以讀;傳統IO只能是單向的

NIO可以設置為異步;傳統IO只能是阻塞,同步的

 

 

緩沖區結構圖

NIO是面向緩沖區的,緩沖區可以理解為一塊內存,有大小。緩沖區有位置、界限、容量幾個概念。

 

capacity:容量,緩沖區的大小

limit:限制,表示最大的可讀寫的數量

position:當前位置,每當讀寫,當前位置都會加一

 

flip和clear方法,內部就操作這三個變量。

 

 

 

緩沖區常用方法

clear:將當前位置設置為0,限制設置為容量,目的是盡最大可能讓字節,由通道讀取到緩沖中

flip:當前位置置為限制,然后將當前位置置為0,目的是將有數據部分的字節,由緩沖寫入到通道中。通常用在讀與寫之間。

 

讀寫文件代碼

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM