Java進階 | IO流核心模塊與基本原理


一、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基礎

數據分析】【技術導圖】【 職場


免責聲明!

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



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