Introduction to FileDescriptor in this chapter
FileDescriptor Introduction
FileDescriptor is a "file descriptor".
FileDescriptor can be used to indicate open files, open sockets, and so on.
In the case of FileDescriptor: When FileDescriptor represents a file, we can generally think of filedescriptor as the file. However, we cannot manipulate the file directly via FileDescriptor, and if we need to operate the file through FileDescriptor, we need to create a new filedescriptor corresponding FileOutputStream and then manipulate the file.
In, out, Err introduction
(in)--standard input (keyboard) descriptor
Out--standard output (screen) descriptor
(+) Err-the standard error output (screen) descriptor
The principles and usages of these 3 are similar, and we go through out to do a thorough study.
The function and principle of out
Out is a descriptor for the standard output (screen). But how does it work?
We can generally understand that out represents the standard output (screen). If we want to output the information to the screen, we can do it through out, but out does not provide an interface for outputting information to the screen (because the out nature is a FileDescriptor object and the FileDescriptor has no output interface). What do we do?
Quite simply, we create out corresponding "output stream objects" and then output the information to the screen via output interfaces such as write () of the output stream. The following code:
try {
FileOutputStream out = new FileOutputStream (filedescriptor.out);
Out.write (' A ');
Out.close ();
} catch (IOException e) {
}
Executing the above program will output the letter ' A ' on the screen.
In order to facilitate our operation, Java has already packaged a "convenient interface for the output of information on the screen": Through System.out, we can easily output information to the screen.
Therefore, we can equivalently convert the above program to the following code:
System.out.print (' A ');
Here's how the above two pieces of code works
Look at the definition of out. It is defined in the Filedescriptor.java, the relevant source code is as follows:
Public final class FileDescriptor {
private int fd;
public static final FileDescriptor out = new FileDescriptor (1);
Private FileDescriptor (int fd) {
this.fd = FD;
Usecount = new Atomicinteger ();
}
...
}
From this, you can see
Out is a FileDescriptor object. It is created by the constructor filedescriptor (int fd).
FileDescriptor (int fd): Assigns a value to the FD object (type int) and creates a new usecount using the Count variable.
FD object is a very important variable, "fd=1" on behalf of the "standard output", "fd=0" on behalf of the "standard input", "fd=2" represents the "standard error output."
FileOutputStream out = new FileOutputStream (filedescriptor.out); is to use the constructor FileOutputStream (FileDescriptor fdobj) to create the filed.out corresponding FileOutputStream object.
about how System.out is defined. Refer to "System.out.println" ("Hello World"); "
Todo
Through the above study, we know that we can customize the flow of standard file descriptors [i.e., in (standard input), out (standard output), ERR (standard error output)] To complete the input/output function; However, Java has encapsulated the corresponding interface for us, That is, we can more conveniently system.in, System.out, system.err to use them.
In addition, we can customize file descriptors such as "file", "Socket", and then manipulate them. Refer to the Testwrite (), Testread () interface in the example code below.
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/Java/