一、IO流與系統

IO技術在JDK中算是極其複雜的模組,其複雜的一個關鍵原因就是IO操作和系統核心的關聯性,另外網路程式設計,檔案管理都依賴IO技術,而且都是程式設計的難點,想要整體理解IO流,先從Linux作業系統開始。

Linux空間隔離

Linux使用是區分使用者的,這個是基礎常識,其底層也區分使用者和核心兩個模組:

  • User space:使用者空間
  • Kernel space:核心空間

常識使用者空間的許可權相對核心空間操作許可權弱很多,這就涉及到使用者與核心兩個模組間的互動,此時部署在服務上的應用如果需要請求系統資源,則在互動上更為複雜:

使用者空間本身無法直接向系統釋出排程指令,必須通過核心,對於核心中資料的操作,也是需要先拷貝到使用者空間,這種隔離機制可以有效的保護系統的安全性和穩定性。

引數檢視

可以通過Top命令動態檢視各項資料分析,程序佔用資源的狀況:

  • us:使用者空間佔用CPU的百分比;
  • sy:核心空間佔用CPU的百分比;
  • id:空閒程序佔用CPU的百分比;
  • wa:IO等待佔用CPU的百分比;

wa指標,在大規模檔案任務流程裡是監控的核心項之一。

IO協作流程

此時再看上面圖【1】的流程,當應用端發起IO操作的請求時,請求沿著鏈路上的各個節點流轉,有兩個核心概念:

  • 節點互動模式:同步與非同步;
  • IO資料操作:阻塞與非阻塞;

這裡就是檔案流中常說的:【同步/非同步】IO,【阻塞/非阻塞】IO,下面看細節。

二、IO模型分析

1、同步阻塞

使用者執行緒與核心的互動方式,應用端請求對應一個執行緒處理,整個過程中accept(接收)和read(讀取)方法都會阻塞直至整個動作完成:

在常規CS架構模式中,這是一次IO操作的基本過程,該方式如果在高併發的場景下,客戶端的請求響應會存在嚴重的效能問題,並且佔用過多資源。

2、同步非阻塞

在同步阻塞IO的基礎上進行優化,當前執行緒不會一直等待資料就緒直到完成複製:

線上程請求後會立即返回,並不斷輪詢直至拿到資料,才會停止輪詢,這種模式的缺陷也是顯而易見的,如果資料準備好,在通知執行緒完成後續動作,這樣就可以省掉很多中間互動。

3、非同步通知模式

在非同步模式下,徹底摒棄阻塞機制,過程分段進行互動,這與常規的第三方對接模式很相似,本地服務在請求第三方服務時,如果請求過程耗時很大,會非同步執行,第三方第一次回撥,確認請求可以被執行;第二次回撥則是推送處理結果,這種思想在處理複雜問題時,可以很大程度的提高效能,節省資源:

非同步模式對於效能的提升是巨大的,當然其相應的處理機制也更復雜,程式的迭代和優化是無止境的,在NIO模式中再次對IO流模式進行優化。

三、File檔案類

1、基礎描述

File類作為檔案和目錄路徑名的抽象表示,用來獲取磁碟檔案的相關元資料資訊,例如:檔名稱、大小、修改時間、許可權判斷等。

注意:File並不操作檔案承載的資料內容,檔案內容稱為資料,檔案自身資訊稱為元資料。

public class File01 {
public static void main(String[] args) throws Exception {
// 1、讀取指定檔案
File speFile = new File(IoParam.BASE_PATH+"fileio-03.text") ;
if (!speFile.exists()){
boolean creFlag = speFile.createNewFile() ;
System.out.println("建立:"+speFile.getName()+"; 結果:"+creFlag);
} // 2、讀取指定位置
File dirFile = new File(IoParam.BASE_PATH) ;
// 判斷是否目錄
boolean dirFlag = dirFile.isDirectory() ;
if (dirFlag){
File[] dirFiles = dirFile.listFiles() ;
printFileArr(dirFiles);
} // 3、刪除指定檔案
if (speFile.exists()){
boolean delFlag = speFile.delete() ;
System.out.println("刪除:"+speFile.getName()+"; 結果:"+delFlag);
}
}
private static void printFileArr (File[] fileArr){
if (fileArr != null && fileArr.length>0){
for (File file : fileArr) {
printFileInfo(file) ;
}
}
}
private static void printFileInfo (File file) {
System.out.println("名稱:"+file.getName());
System.out.println("長度:"+file.length());
System.out.println("路徑:"+file.getPath());
System.out.println("檔案判斷:"+file.isFile());
System.out.println("目錄判斷:"+file.isDirectory());
System.out.println("最後修改:"+new Date(file.lastModified()));
System.out.println();
}
}

上述案例使用了File類中的基本構造和常用方法(讀取、判斷、建立、刪除)等,JDK原始碼在不斷的更新迭代,通過類的構造器、方法、註釋等去判斷類具有的基本功能,是作為開發人員的必備能力。

在File檔案類中缺乏兩個關鍵資訊描述:型別和編碼,如果經常開發檔案模組的需求,就知道這是兩個極其複雜的點,很容易出現問題,下面站在實際開發的角度看看如何處理。

2、檔案業務場景

如圖所示,在常規的檔案流任務中,會涉及【檔案、流、資料】三種基本形式的轉換:

基本過程描述:

  • 原始檔生成,推送檔案中心;
  • 通知業務使用節點獲取檔案;
  • 業務節點進行邏輯處理;

很顯然的一個問題,任何節點都無法適配所有檔案處理策略,比如型別與編碼,面對複雜場景下的問題,規則約束是常用的解決策略,即在約定規則之內的事情才處理。

上面流程中,原始檔節點通知業務節點時的資料主體描述:

public class BizFile {
/**
* 檔案任務批次號
*/
private String taskId ;
/**
* 是否壓縮
*/
private Boolean zipFlag ;
/**
* 檔案地址
*/
private String fileUrl ;
/**
* 檔案型別
*/
private String fileType ;
/**
* 檔案編碼
*/
private String fileCode ;
/**
* 業務關聯:資料庫
*/
private String bizDataBase ;
/**
* 業務關聯:資料表
*/
private String bizTableName ;
}

把整個過程當做一個任務進行封裝,即:任務批次、檔案資訊、業務庫表路由等,當然這些資訊也可以直接標記在檔案命名的策略上,處理的手段類似:

/**
* 基於約定策略讀取資訊
*/
public class File02 {
public static void main(String[] args) {
BizFile bizFile = new BizFile("IN001",Boolean.FALSE, IoParam.BASE_PATH,
"csv","utf8","model","score");
bizFileInfo(bizFile) ;
/*
* 業務性校驗
*/
File file = new File(bizFile.getFileUrl());
if (!file.getName().endsWith(bizFile.getFileType())){
System.out.println(file.getName()+":描述錯誤...");
}
}
private static void bizFileInfo (BizFile bizFile){
logInfo("任務ID",bizFile.getTaskId());
logInfo("是否解壓",bizFile.getZipFlag());
logInfo("檔案地址",bizFile.getFileUrl());
logInfo("檔案型別",bizFile.getFileType());
logInfo("檔案編碼",bizFile.getFileCode());
logInfo("業務庫",bizFile.getBizDataBase());
logInfo("業務表",bizFile.getBizTableName());
}
}

基於主體描述的資訊,也可以轉化到命名規則上:命名策略:編號_壓縮_Excel_編碼_庫_表,這樣一來在業務處理時,不符合約定的檔案直接排除掉,降低檔案異常導致的資料問題。

四、基礎流模式

1、整體概述

IO流向

基本編碼邏輯:原始檔->輸入流->邏輯處理->輸出流->目標檔案

基於不同的角度看,流可以被劃分很多模式:

  • 流動方向:輸入流、輸出流;
  • 流資料型別:位元組流、字元流;

IO流的模式有很多種,相應的API設計也很複雜,通常複雜的API要把握住核心介面與常用的實現類和原理。

基礎API

  • 位元組流:InputStream輸入、OutputStream輸出;資料傳輸的基本單位是位元組;

    • read():輸入流中讀取資料的下一個位元組;
    • read(byte b[]):讀資料快取到位元組陣列;
    • write(int b):指定位元組寫入輸出流;
    • write(byte b[]):陣列位元組寫入輸出流;
  • 字元流:Reader讀取、Writer寫出;資料傳輸的基本單位是字元;

    • read():讀取一個單字元;
    • read(char cbuf[]):讀取到字元陣列;
    • write(int c):寫一個指定字元;
    • write(char cbuf[]):寫一個字元陣列;

緩衝模式

IO流常規讀寫模式,即讀取到資料然後寫出,還有一種緩衝模式,即資料先載入到緩衝陣列,在讀取的時候判斷是否要再次填充緩衝區:

緩衝模式的優點十分明顯,保證讀寫過程的高效率,並且與資料填充過程隔離執行,在BufferedInputStream、BufferedReader類中是對緩衝邏輯的具體實現。

2、位元組流

API關係圖:

位元組流基礎API:

public class IoByte01 {
public static void main(String[] args) throws Exception {
// 原始檔 目標檔案
File source = new File(IoParam.BASE_PATH+"fileio-01.png") ;
File target = new File(IoParam.BASE_PATH+"copy-"+source.getName()) ;
// 輸入流 輸出流
InputStream inStream = new FileInputStream(source) ;
OutputStream outStream = new FileOutputStream(target) ;
// 讀入 寫出
byte[] byteArr = new byte[1024];
int readSign ;
while ((readSign=inStream.read(byteArr)) != -1){
outStream.write(byteArr);
}
// 關閉輸入、輸出流
outStream.close();
inStream.close();
}
}

位元組流緩衝API:

public class IoByte02 {
public static void main(String[] args) throws Exception {
// 原始檔 目標檔案
File source = new File(IoParam.BASE_PATH+"fileio-02.png") ;
File target = new File(IoParam.BASE_PATH+"backup-"+source.getName()) ;
// 緩衝:輸入流 輸出流
InputStream bufInStream = new BufferedInputStream(new FileInputStream(source));
OutputStream bufOutStream = new BufferedOutputStream(new FileOutputStream(target));
// 讀入 寫出
int readSign ;
while ((readSign=bufInStream.read()) != -1){
bufOutStream.write(readSign);
}
// 關閉輸入、輸出流
bufOutStream.close();
bufInStream.close();
}
}

位元組流應用場景:資料是檔案本身,例如圖片,視訊,音訊等。

3、字元流

API關係圖:

字元流基礎API:

public class IoChar01 {
public static void main(String[] args) throws Exception {
// 讀文字 寫文字
File readerFile = new File(IoParam.BASE_PATH+"io-text.txt") ;
File writerFile = new File(IoParam.BASE_PATH+"copy-"+readerFile.getName()) ;
// 字元輸入輸出流
Reader reader = new FileReader(readerFile) ;
Writer writer = new FileWriter(writerFile) ;
// 字元讀入和寫出
int readSign ;
while ((readSign = reader.read()) != -1){
writer.write(readSign);
}
writer.flush();
// 關閉流
writer.close();
reader.close();
}
}

字元流緩衝API:

public class IoChar02 {
public static void main(String[] args) throws Exception {
// 讀文字 寫文字
File readerFile = new File(IoParam.BASE_PATH+"io-text.txt") ;
File writerFile = new File(IoParam.BASE_PATH+"line-"+readerFile.getName()) ;
// 緩衝字元輸入輸出流
BufferedReader bufReader = new BufferedReader(new FileReader(readerFile)) ;
BufferedWriter bufWriter = new BufferedWriter(new FileWriter(writerFile)) ;
// 字元讀入和寫出
String line;
while ((line = bufReader.readLine()) != null){
bufWriter.write(line);
bufWriter.newLine();
}
bufWriter.flush();
// 關閉流
bufWriter.close();
bufReader.close();
}
}

字元流應用場景:檔案作為資料的載體,例如Excel、CSV、TXT等。

4、編碼解碼

  • 編碼:字元轉換為位元組;
  • 解碼:位元組轉換為字元;
public class EnDeCode {
public static void main(String[] args) throws Exception {
String var = "IO流" ;
// 編碼
byte[] enVar = var.getBytes(StandardCharsets.UTF_8) ;
for (byte encode:enVar){
System.out.println(encode);
}
// 解碼
String deVar = new String(enVar,StandardCharsets.UTF_8) ;
System.out.println(deVar);
// 亂碼
String messyVar = new String(enVar,StandardCharsets.ISO_8859_1) ;
System.out.println(messyVar);
}
}

亂碼出現的根本原因,就是在編碼與解碼的兩個階段使用的編碼型別不同。

5、序列化

  • 序列化:物件轉換為流的過程;
  • 反序列化:流轉換為物件的過程;
public class SerEntity implements Serializable {
private Integer id ;
private String name ;
}
public class Seriali01 {
public static void main(String[] args) throws Exception {
// 序列化物件
OutputStream outStream = new FileOutputStream("SerEntity.txt") ;
ObjectOutputStream objOutStream = new ObjectOutputStream(outStream);
objOutStream.writeObject(new SerEntity(1,"Cicada"));
objOutStream.close();
// 反序列化物件
InputStream inStream = new FileInputStream("SerEntity.txt");
ObjectInputStream objInStream = new ObjectInputStream(inStream) ;
SerEntity serEntity = (SerEntity) objInStream.readObject();
System.out.println(serEntity);
inStream.close();
}
}

注意:引用型別的成員物件也必須是可被序列化的,否則會丟擲NotSerializableException異常。

五、NIO模式

1、基礎概念

NIO即(NonBlockingIO),面向資料塊的處理機制,同步非阻塞模型,服務端的單個執行緒可以處理多個客戶端請求,對IO流的處理速度有極高的提升,三大核心元件:

  • Buffer(緩衝區):底層維護陣列儲存資料;
  • Channel(通道):支援讀寫雙向操作;
  • Selector(選擇器):提供Channel多註冊和輪詢能力;

API使用案例

public class IoNew01 {
public static void main(String[] args) throws Exception {
// 原始檔 目標檔案
File source = new File(IoParam.BASE_PATH+"fileio-02.png") ;
File target = new File(IoParam.BASE_PATH+"channel-"+source.getName()) ; // 輸入位元組流通道
FileInputStream inStream = new FileInputStream(source);
FileChannel inChannel = inStream.getChannel(); // 輸出位元組流通道
FileOutputStream outStream = new FileOutputStream(target);
FileChannel outChannel = outStream.getChannel(); // 直接通道複製
// outChannel.transferFrom(inChannel, 0, inChannel.size()); // 緩衝區讀寫機制
ByteBuffer buffer = ByteBuffer.allocateDirect(1024);
while (true) {
// 讀取通道中資料到緩衝區
int in = inChannel.read(buffer);
if (in == -1) {
break;
}
// 讀寫切換
buffer.flip();
// 寫出緩衝區資料
outChannel.write(buffer);
// 清空緩衝區
buffer.clear();
}
outChannel.close();
inChannel.close();
}
}

上述案例只是NIO最基礎的檔案複製能力,在網路通訊中,NIO模式的發揮空間十分寬廣。

2、網路通訊

服務端的單執行緒可以處理多個客戶端請求,通過輪詢多路複用器檢視是否有IO請求,這樣一來,服務端的併發能力得到極大的提升,並且顯著降低了資源的消耗。

API案例:服務端模擬

public class SecServer {
public static void main(String[] args) {
try {
//啟動服務開啟監聽
ServerSocketChannel socketChannel = ServerSocketChannel.open();
socketChannel.socket().bind(new InetSocketAddress("127.0.0.1", 8089));
// 設定非阻塞,接受客戶端
socketChannel.configureBlocking(false);
// 開啟多路複用器
Selector selector = Selector.open();
// 服務端Socket註冊到多路複用器,指定興趣事件
socketChannel.register(selector, SelectionKey.OP_ACCEPT);
// 多路複用器輪詢
ByteBuffer buffer = ByteBuffer.allocateDirect(1024);
while (selector.select() > 0){
Set<SelectionKey> selectionKeys = selector.selectedKeys();
Iterator<SelectionKey> selectionKeyIter = selectionKeys.iterator();
while (selectionKeyIter.hasNext()){
SelectionKey selectionKey = selectionKeyIter.next() ;
selectionKeyIter.remove();
if(selectionKey.isAcceptable()) {
// 接受新的連線
SocketChannel client = socketChannel.accept();
// 設定讀非阻塞
client.configureBlocking(false);
// 註冊到多路複用器
client.register(selector, SelectionKey.OP_READ);
} else if (selectionKey.isReadable()) {
// 通道可讀
SocketChannel client = (SocketChannel) selectionKey.channel();
int len = client.read(buffer);
if (len > 0){
buffer.flip();
byte[] readArr = new byte[buffer.limit()];
buffer.get(readArr);
System.out.println(client.socket().getPort() + "埠資料:" + new String(readArr));
buffer.clear();
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
} }
}

API案例:客戶端模擬

public class SecClient {
public static void main(String[] args) {
try {
// 連線服務端
SocketChannel socketChannel = SocketChannel.open();
socketChannel.connect(new InetSocketAddress("127.0.0.1", 8089));
ByteBuffer writeBuffer = ByteBuffer.allocate(1024);
String conVar = "[hello-8089]";
writeBuffer.put(conVar.getBytes());
writeBuffer.flip();
// 每隔5S傳送一次資料
while (true) {
Thread.sleep(5000);
writeBuffer.rewind();
socketChannel.write(writeBuffer);
writeBuffer.clear();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

SelectionKey繫結Selector和Chanel之間的關聯,並且可以獲取就緒狀態下的Channel集合。

IO流同系列文章:

| IO流概述 | MinIO中介軟體 | FastDFS中介軟體 | Xml和CSV檔案 | Excel和PDF檔案 | 檔案上傳邏輯 |

六、原始碼地址

GitHub·地址
https://github.com/cicadasmile/java-base-parent
GitEE·地址
https://gitee.com/cicadasmile/java-base-parent

閱讀標籤

Java基礎】【設計模式】【結構與演算法】【Linux系統】【資料庫

分散式架構】【微服務】【大資料元件】【SpringBoot進階】【Spring&Boot基礎

資料分析】【技術導圖】【 職場