简介
- 文件描述符类的官方解释:
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.
文件描述符是对底层资源(文件、网口接口、其他的一些字节源)的引用,主要用于输入输出流。
应用程序不能进行创建文件描述符对象。(公用的构造函数是无用的,只有私有的告诉函数)
- 文件描述的结构
如下图:我们可以看到文件描述符的与底层文件系统、系统文件管理、进行文件描述符的关系。
- 进程描述符的文件描述符只是起一个指向作用,文件可以多次打开,打开的文件指向统一底层。但是文件描述符是不相同的。
- 也可以用一个文件描述符,创建多个输入输出流。基本与上一个相同。
- 线程有几个特殊的文件描述符,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 原因:打开两次相同的文件,从头开始写入。 文件描述符对同一个文件,可以不同
程序2:
文件内容为cca 输出true 原因:相同的文件描述符,相当于打开同一个文件。但是file1与file3的对象不equal。参考
- 其他文献参考orcale的库文件说明 2、图参考https://blog.csdn.net/weixin_34119545/article/details/89038953