Java教程-Java FileDescriptor类
Java FileDescriptor类
FileDescriptor类作为底层特定于机器的结构的句柄,用于表示打开的文件、打开的套接字或其他字节源或接收器。该句柄可以是err、in或out。
FileDescriptor类用于创建包含它的FileInputStream或FileOutputStream。
字段
| Modifier | Type | Field | Description |
|---|---|---|---|
| static | FileDescriptor | err | 标准错误流的句柄。 |
| static | FileDescriptor | in | 标准输入流的句柄。 |
| static | FileDescriptor | out | 标准输出流的句柄。 |
构造函数
| Constructor | Description |
|---|---|
| FileDescriptor() | 构造一个(无效的)FileDescriptor对象。 |
方法
| Modifier and Type | Method | Description |
|---|---|---|
| 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:
0123456789: