1. 程式人生 > >Java NIO 讀取文件、寫入文件、讀取寫入混合

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

ID text forname tac 之間 fin top put HR

前言

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://test.txt"; 11 12 @SuppressWarnings("resource") 13 public static void main(String[] args) { 14 readNIO(); 15 //writeNIO(); 16 //testReadAndWriteNIO();
17 } 18 19 public static void readNIO() { 20 //String pathname = "C:\\Users\\adew\\Desktop\\jd-gui.cfg"; 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 String filename = "out.txt"; 69 FileOutputStream fos = null; 70 try { 71 72 fos = new FileOutputStream(new File(filename)); 73 FileChannel channel = fos.getChannel(); 74 ByteBuffer src = Charset.forName("utf8").encode("你好你好你好你好你好"); 75 // 字節緩沖的容量和limit會隨著數據長度變化,不是固定不變的 76 System.out.println("初始化容量和limit:" + src.capacity() + "," 77 + src.limit()); 78 int length = 0; 79 80 while ((length = channel.write(src)) != 0) { 81 /* 82 * 註意,這裏不需要clear,將緩沖中的數據寫入到通道中後 第二次接著上一次的順序往下讀 83 */ 84 System.out.println("寫入長度:" + length); 85 } 86 87 } catch (FileNotFoundException e) { 88 e.printStackTrace(); 89 } catch (IOException e) { 90 e.printStackTrace(); 91 } finally { 92 if (fos != null) { 93 try { 94 fos.close(); 95 } catch (IOException e) { 96 e.printStackTrace(); 97 } 98 } 99 } 100 } 101 102 public static void testReadAndWriteNIO() { 103 String pathname = "C:\\Users\\adew\\Desktop\\test.txt"; 104 FileInputStream fin = null; 105 106 String filename = "test-out.txt"; 107 FileOutputStream fos = null; 108 try { 109 fin = new FileInputStream(new File(pathname)); 110 FileChannel channel = fin.getChannel(); 111 112 int capacity = 100;// 字節 113 ByteBuffer bf = ByteBuffer.allocate(capacity); 114 System.out.println("限制是:" + bf.limit() + "容量是:" + bf.capacity() + "位置是:" + bf.position()); 115 int length = -1; 116 117 fos = new FileOutputStream(new File(filename)); 118 FileChannel outchannel = fos.getChannel(); 119 120 121 while ((length = channel.read(bf)) != -1) { 122 123 //將當前位置置為limit,然後設置當前位置為0,也就是從0到limit這塊,都寫入到同道中 124 bf.flip(); 125 126 int outlength = 0; 127 while ((outlength = outchannel.write(bf)) != 0) { 128 System.out.println("讀," + length + "寫," + outlength); 129 } 130 131 //將當前位置置為0,然後設置limit為容量,也就是從0到limit(容量)這塊, 132 //都可以利用,通道讀取的數據存儲到 133 //0到limit這塊 134 bf.clear(); 135 136 } 137 } catch (FileNotFoundException e) { 138 e.printStackTrace(); 139 } catch (IOException e) { 140 e.printStackTrace(); 141 } finally { 142 if (fin != null) { 143 try { 144 fin.close(); 145 } catch (IOException e) { 146 e.printStackTrace(); 147 } 148 } 149 if (fos != null) { 150 try { 151 fos.close(); 152 } catch (IOException e) { 153 e.printStackTrace(); 154 } 155 } 156 } 157 } 158 159 }

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