Java FileDescriptor
FileDescriptor类用作表示打开的文件(打开的套接字)的基础计算机特定结构的句柄或其他字节源或宿。手柄可以是错误的,内入或外出的。
FileDescriptor类用于创建FileInputStream或FileOutputStream包含它。
字段
修饰符 |
类型 |
字段 |
说明 |
static |
FileDescriptor |
err |
标准错误流的句柄。 |
static |
FileDescriptor |
in |
标准输入流的句柄。 |
static |
FileDescriptor |
out |
标准输出流的句柄。 |
构造函数
构造函数 |
说明 |
FileDescriptor() |
构造一个(无效的)FileDescriptor object。 |
方法
修饰符和类型 |
方法 |
说明 |
void |
sync() |
它强制所有系统缓冲区与基础设备同步。 |
boolean |
valid() |
它将测试此文件描述符对象是否有效。 |
Java FileDescriptor示例
import java.io.*;
public class FileDescriptorExample {
public static void main(String[] args) {
FileDescriptor fd = null;
byte[] b = {
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58 }
;
try {
FileOutputStream fos = new FileOutputStream("Record.txt");
FileInputStream fis = new FileInputStream("Record.txt");
fd = fos.getFD();
fos.write(b);
fos.flush();
fd.sync();
// confirms data to be written to the disk int value = 0;
// for every available bytes while ((value = fis.read()) != -1) {
char c = (char) value;
// converts bytes to char System.out.print(c);
}
System.out.println("\nSync() successfully executed!!");
}
catch (Exception e) {
e.printStackTrace();
}
}
}
输出:
0123456789:Sync() successfully executed!!
Record.txt: