Filedescriptor簡述

簡介

  • 文件描述符類的官方解釋:

Instances of the file descriptor class serve as an opaque handle to the underlying machine-specific structure representing an open file, an open socket, or another source or sink of bytes. The main practical use for a file descriptor is to create a FileInputStream or FileOutputStream to contain it.
Applications should not create their own file descriptors. 文件描述符是對底層資源(文件、網口接口、其餘的一些字節源)的引用,主要用於輸入輸出流。
應用程序不能進行建立文件描述符對象。(公用的構造函數是無用的,只有私有的告訴函數)
bash

  • 文件描述的結構

以下圖:咱們能夠看到文件描述符的與底層文件系統、系統文件管理、進行文件描述符的關係。socket

  1. 進程描述符的文件描述符只是起一個指向做用,文件能夠屢次打開,打開的文件指向統一底層。可是文件描述符是不相同的。
  2. 也能夠用一個文件描述符,建立多個輸入輸出流。基本與上一個相同。
  3. 線程有幾個特殊的文件描述符,0表明標準輸出、1表明標準輸入、2表明錯誤輸出。每一個進程都是如此。

程序演示

程序1:函數

1        FileOutputStream file1=new FileOutputStream("zhao.text");
2        FileOutputStream file2=new FileOutputStream("zhao.text");
3     //   FileOutputStream file3=new FileOutputStream(file2.getFD());
4        file1.write('a');
5        file1.write('a');
6        file2.write('c');
7        System.out.println(file1.getFD().equals(file2.getFD()));
複製代碼

文件內容爲ca 輸出false 緣由:打開兩次相同的文件,從頭開始寫入。 文件描述符對同一個文件,能夠不一樣spa

程序2:.net

文件內容爲cca 輸出true 緣由:相同的文件描述符,至關於打開同一個文件。可是file1與file3的對象不equal。

參考線程

  1. 其餘文獻參考orcale的庫文件說明 二、圖參考https://blog.csdn.net/weixin_34119545/article/details/89038953
相關文章
相關標籤/搜索