Unix、Linux、Windows、JDK中的文件描述符

文件描述符(File Descriptor,就是Unix、Linux代码中常看到的fd)是文件对应的索引值,是一个无符号非负整数。新建、打开、读写文件都需要文件描述符指定对应的文件。每个进程对应一个文件描述符表,文件描述符从0开始,所以一个文件会有不同的文件描述符。文件、目录、进程、Socket、硬件设备等都能看成文件。操作系统的一切都可以是文件描述符。习惯上,标准输入(standard input)的文件描述符是 0,标准输出(standard output)的文件描述符是 1,标准错误(standard error)的文件描述符是 2。

对应JDK的java.io.FileDescriptor:

/**
     * A handle to the standard input stream. Usually, this file
     * descriptor is not used directly, but rather via the input stream
     * known as {@code System.in}.
     *
     * @see     java.lang.System#in
     */
    public static final FileDescriptor in = new FileDescriptor(0);

    /**
     * A handle to the standard output stream. Usually, this file
     * descriptor is not used directly, but rather via the output stream
     * known as {@code System.out}.
     * @see     java.lang.System#out
     */
    public static final FileDescriptor out = new FileDescriptor(1);

    /**
     * A handle to the standard error stream. Usually, this file
     * descriptor is not used directly, but rather via the output stream
     * known as {@code System.err}.
     *
     * @see     java.lang.System#err
     */
    public static final FileDescriptor err = new FileDescriptor(2);

文件的上下文:文件的打开模式、文件的位置类型、文件的初始类型等。

在Windows操作系统上,文件描述符被称作文件句柄。

Java中使用FileDescriptor类来抽象文件描述符。
对于FileInputStream/FileOutputStream/RandomAccessFile,使用handle来表示底层的文件句柄。
对于ServerSocket/Socket,使用fd来表示底层的文件句柄。
FileDescriptor的fd和handle的无效值是-1。

    private int fd;

    private long handle;

用户进程想操作一个文件,操作系统会返回这个文件对应的文件描述符。用户进程和操作系统内核之后沟通时只需传递文件描述符即可,无需根据描述或者位置确定文件。

https://www.cnblogs.com/noteless/p/9605778.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风铃峰顶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值