认知IO流之 — FileDescriptor

简介: FileDescriptor 顾名思义是文件描述符,FileDescriptor 可以被用来表示开放文件、开放套接字等。比如用 FileDescriptor 表示文件来说: 当 FileDescriptor 表示文件时,我们可以通俗的将 FileDescriptor 看成是该文件。但是,我们不能直接通过 FileDescriptor 对该文件进行操作。

FileDescriptor 是什么

FileDescriptor 顾名思义是文件描述符,FileDescriptor 可以被用来表示开放文件、开放套接字等。比如用 FileDescriptor 表示文件来说: 当 FileDescriptor 表示文件时,我们可以通俗的将 FileDescriptor 看成是该文件。但是,我们不能直接通过 FileDescriptor 对该文件进行操作。

若需要通过 FileDescriptor 对该文件进行操作,则需要新创建 FileDescriptor 对应的 FileOutputStream或者是 FileInputStream,再对文件进行操作,应用程序不应该创建他们自己的文件描述符

下面让我们用两个例子来演示一下 FileDescriptor 分别与 FileInputStream 和 FileOutputStream 的使用

publicclass FileDescriptorExample {
    public static void main(String[] args) {
        try (FileInputStream fileInputStream = new FileInputStream("/Users/mr.l/Desktop/test")) {
            // 返回 FileDescriptor 对象代表着文件系统中的真实文件的链接。
            FileDescriptor fd = fileInputStream.getFD();
            System.out.println("File descriptor of the file /Users/mr.l/Desktop/test.txt : "
                    + fd.hashCode());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try (FileOutputStream fileOutputStream = new FileOutputStream("/Users/mr.l/Desktop/test2")) {
            FileDescriptor fd = fileOutputStream.getFD();
            System.out.println("File descriptor of the file /Users/mr.l/Desktop/test2.txt : " + fd.hashCode());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

FileDescriptor 结构

FileDescriptor 有三种输出方式:in、out、error,分别代表

publicstaticfinal FileDescriptor in = new FileDescriptor(0);

一个标准输入流的句柄。通常情况下,这个文件描述符不会直接使用,而是通过称为System.in的输入流。

publicstaticfinal FileDescriptor out = new FileDescriptor(1);

一个标准的输出流句柄。通过 System.out 来使用

publicstaticfinal FileDescriptor err = new FileDescriptor(2);

一个标准的错误流句柄。通过 System.err 来使用

那么如何创建这三种输出流呢?在 FileInputStreamFileOutputStream 中使用其对应的构造方法来创建。

public FileInputStream(FileDescriptor fdObj) {...}

文件描述符的创建

我们可以通过如下的方式来创建文件描述符

FileInputStream fileInputStream = new FileInputStream(FileDescriptor.in);

fileInputStream.read();
fileInputStream.close();

这段代码创建了一个标准输入流,让你可以从控制台输入信息,它的作用等同于System.in

类似的,outerror 都是文件输出流,你可以按照下面这种方式创建

FileOutputStream out = new FileOutputStream(FileDescriptor.out);
out.write('A');
out.close();

它们用于向控制台输出消息,out 的作用等于 System.out,err 的作用等于 System.err。

因此,我们可以等价的将上面的程序转换为如下代码:System.out.print('A'); System.err.print('A');

FileDescriptor 方法与使用

FileDescriptor 的方法比较少,下面就一起看一下 FileDescriptor 都包含了哪些方法

sync

public native void sync() throws SyncFailedException;

此方法是一个 native 方法,由 C 语言实现,它的主要用处是说:强制所有系统缓冲区与基础设备同步。该方法在此 FileDescriptor 的所有修改数据和属性都写入相关设备后返回。如果此 FileDescriptor 引用物理存储介质,比如文件系统中的文件,则一直要等到将与此 FileDesecriptor 有关的缓冲区的所有内存中修改副本写入物理介质中,sync 方法才会返回。sync 方法由要求物理存储(比例文件)处于某种已知状态下的代码使用。例如,提供简单事务处理的类可以使用 sync 来确保某个文件所有由给定事务造成的更改都记录在存储介质上。sync 只影响此 FileDescriptor 的缓冲区下游。如果正通过应用程序(例如,通过一个 BufferedOutputStream 对象)实现内存缓冲,那么必须在数据受 sync 影响之前将这些缓冲区刷新,并转到 FileDescriptor 中(例如,通过调用 OutputStream.flush)。

它的一般用法是

public static void main(String[] args) throws IOException {

FileDescriptor descriptor = null;
FileOutputStream outputStream = null;
byte[] buffer = {71,69,69,75,83};
try {
outputStream = new FileOutputStream("/Users/mr.l/Desktop/test3");
descriptor = outputStream.getFD();
outputStream.write(buffer);
descriptor.sync();
}catch(Exception excpt) {
excpt.printStackTrace();
}
finally{
if(outputStream != null)
outputStream.close();
}
}

valid

测试此文件描述符对象是否有效。如果文件描述符对象代表着 有效的开放文件,套接字或者其他有效的 I/O 连接 则返回true ,其他返回 false。它的用法如下

FileDescriptor descriptor = null;
try(FileInputStream inputStream = new FileInputStream("/Users/mr.l/Desktop/test3")) {
boolean check = false;
descriptor = inputStream.getFD();
check = descriptor.valid();
System.out.println("check = " + check);
}catch (Exception e){
e.printStackTrace();
}

打印输出 check = true, 因为此文件目前处于开放状态。如果把 valid() 方法放在 inputStream.close()方法后呢,就如下所表示的

FileInputStream inputStream = null;
FileDescriptor descriptor = null;
boolean check = false;
try {
inputStream = new FileInputStream("/Users/mr.l/Desktop/test3");
descriptor = inputStream.getFD();
check = descriptor.valid();
System.out.println("check = " + check);
}catch (Exception e){
e.printStackTrace();
} finally {
inputStream.close();
check = descriptor.valid();
System.out.println("check = " + check);
}

会输出两条消息

check = true check = false

因为在流关闭之后,文件不在有效,故返回false。

attach

解析 attach 方法前首先来看一下两个接口 Closeable 接口和 AutoCloseable 接口

  • AutoCloseable 接口 : 实现了此接口的类能够持有资源直到被关闭的时候。其中的 close() 方法是自动关闭的,离开try-with-resources(jdk1.7 的新特性,不清楚请移步至 你会使用try-with-resources吗)中的 try 语句块的时候。这种方式确保了能够及时释放资源,避免资源的枯竭和可能出现的错误。
  • Closeable 接口:Closeable 表示一个资源或者数据能够被关闭,close 方法被调用用来释放对象持有的资源,如果资源已经关闭了,那么调用 close 方法不会再产生作用。

然后回到 FileDescriptor 的描述中来,FileDescriptor 有三个属性

private Closeable parent;
private List<Closeable> otherParents;
privateboolean closed;

有一个 Closeable 对象的 parent,表示用来关闭单个资源,List<Closeable> otherParents,需要关闭对象的集合,下面源码中会用到, closed用来判断资源是否已经关闭。

attach 源码:

synchronized void attach(Closeable c) {
if (parent == null) {
parent = c;
} elseif (otherParents == null) {
otherParents = new ArrayList<>();
otherParents.add(parent);
otherParents.add(c);
} else {
otherParents.add(c);
}
}

此方法用于追踪需要关闭的对象,如果只有单个需要关闭的对象,那么直接调用后面的 closeAll() 方法即可,如果多个流指向同一个相同的描述符,FileDescriptor 会把需要关闭的资源放在 otherParents 的集合中,我们会循环list 中的每个引用,并且把它们添加到 parent 后面,这个 parent 顾名思义相当于就是 第一个需要被关闭的资源,这个方法主要为下面的 closeAll() 方法做铺垫。

closeAll

synchronized void closeAll(Closeable releaser) throws IOException {
if (!closed) {
closed = true;
IOException ioe = null;
try (Closeable c = releaser) {
if (otherParents != null) {
for (Closeable referent : otherParents) {
try {
referent.close();
} catch(IOException x) {
if (ioe == null) {
ioe = x;
} else {
ioe.addSuppressed(x);
}
}
}
}
} catch(IOException ex) {
if (ioe != null)
ex.addSuppressed(ioe);
ioe = ex;
} finally {
if (ioe != null)
throw ioe;
}
}
}

在资源没有被关闭的时候,在需要关闭的资源为 null 的情况下,会对需要关闭的资源集合循环遍历进行关闭操作

            </div>
目录
相关文章
|
13天前
|
存储 弹性计算 人工智能
【2025云栖精华内容】 打造持续领先,全球覆盖的澎湃算力底座——通用计算产品发布与行业实践专场回顾
2025年9月24日,阿里云弹性计算团队多位产品、技术专家及服务器团队技术专家共同在【2025云栖大会】现场带来了《通用计算产品发布与行业实践》的专场论坛,本论坛聚焦弹性计算多款通用算力产品发布。同时,ECS云服务器安全能力、资源售卖模式、计算AI助手等用户体验关键环节也宣布升级,让用云更简单、更智能。海尔三翼鸟云服务负责人刘建锋先生作为特邀嘉宾,莅临现场分享了关于阿里云ECS g9i推动AIoT平台的场景落地实践。
【2025云栖精华内容】 打造持续领先,全球覆盖的澎湃算力底座——通用计算产品发布与行业实践专场回顾
|
4天前
|
云安全 人工智能 安全
Dify平台集成阿里云AI安全护栏,构建AI Runtime安全防线
阿里云 AI 安全护栏加入Dify平台,打造可信赖的 AI
|
12天前
|
人工智能 自然语言处理 自动驾驶
关于举办首届全国大学生“启真问智”人工智能模型&智能体大赛决赛的通知
关于举办首届全国大学生“启真问智”人工智能模型&智能体大赛决赛的通知
|
7天前
|
人工智能 运维 Java
Spring AI Alibaba Admin 开源!以数据为中心的 Agent 开发平台
Spring AI Alibaba Admin 正式发布!一站式实现 Prompt 管理、动态热更新、评测集构建、自动化评估与全链路可观测,助力企业高效构建可信赖的 AI Agent 应用。开源共建,现已上线!
743 23
|
6天前
|
人工智能 Java Nacos
基于 Spring AI Alibaba + Nacos 的分布式 Multi-Agent 构建指南
本文将针对 Spring AI Alibaba + Nacos 的分布式多智能体构建方案展开介绍,同时结合 Demo 说明快速开发方法与实际效果。
477 37
|
7天前
|
机器学习/深度学习 人工智能 搜索推荐
万字长文深度解析最新Deep Research技术:前沿架构、核心技术与未来展望
近期发生了什么自 2025 年 2 月 OpenAI 正式发布Deep Research以来,深度研究/深度搜索(Deep Research / Deep Search)正在成为信息检索与知识工作的全新范式:系统以多步推理驱动大规模联网检索、跨源证据。
459 41
|
1天前
|
文字识别 监控 物联网
这是我写的实施一地两检的跨境高铁站旅客资料预报系统的系统架构
本文设计了一套基于IAPIS理念的高铁跨境旅客预报与边检联动系统,覆盖青青草原内地与喜羊羊特别行政区间“一地两检”场景。系统在旅客购票后即采集证件、生物特征及行程信息,通过Advance Passenger Info Checker等模块,向出发地和目的地移民管理机构实时推送数据,实现出入境许可预审。支持线上/线下购票、检票、退票全流程管控,结合面部识别、行为追踪技术监控旅客状态,防止滞留或非法通行。列车发车前进行最终核验,确保所有跨境旅客获边检许可。若旅行被中途取消,系统自动改签、退票并通知各方,保障安全与效率。(239字)