📜  Java I/O-FileDescriptor

📅  最后修改于: 2020-09-27 07:38:25             🧑  作者: Mango

Java FileDescriptor

FileDescriptor类用作表示特定于底层计算机的结构的句柄,这些结构表示打开的文件,打开的套接字或其他字节源或宿。手柄可以是错误的,也可以是错误的。

FileDescriptor类用于创建FileInputStream或FileOutputStream来包含它。

领域

Modifier Type Field Description
static FileDescriptor err A handle to the standard error stream.
static FileDescriptor in A handle to the standard input stream.
static FileDescriptor out A handle to the standard output stream.

建设者

Constructor Description
FileDescriptor() Constructs an (invalid) FileDescriptor object.

方法

Modifier and Type Method Description
void sync() It force all system buffers to synchronize with the underlying device.
boolean valid() It tests if this file descriptor object is 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: