Explain the file file class and filedescriptor file description class in Java

File

File is an abstract representation of "file" and "directory pathname". File directly inherits from object and implements serializable interface and comparable interface. Implementing the serializable interface means that the file object supports serialization operations. The implementation of the comparable interface means that the size of file objects can be compared; Files can be directly stored in ordered collections (such as TreeSet and treemap). 1. Common method of creating a new directory: create a new directory according to the relative path. The example code is as follows (create a new directory "dir" under the current path):

Method 2: create a new directory according to the absolute path. The example code is as follows (create a new directory "/ home / skywang / dir"):

Note: the above is the source code of the new directory "/ home / skywang / dir" under Linux system. Under windows, to create a new directory "D: / dir", the source code is as follows:

Method 3

Note: it is similar to "method 2", except that "method 2" passes in the full path, while "method 3" passes in the URI corresponding to the full path. 2. Several common methods of creating a new subdirectory. For example, we want to create a new subdirectory under the subdirectory "dir" of the current directory. There are several methods: Method 1

Note: the above method is used to "dir / sub1" in the current directory. The prerequisite for its normal operation is that the parent directory "dir" of "sub1" already exists! Method 2

Note: the above method is used to "dir / sub2" in the current directory. The prerequisite for its normal operation is that the parent directory "dir" of "sub2" already exists! Method 3

Note: the above method is used to "dir / sub3" in the current directory. It does not need dir to exist and can run normally; If the parent path of "sub3" does not exist, the mkdirs () method will automatically create the parent directory. Method 4

Note: the above method is used to create a new directory "/ home / skywang / dir / sub3". It does not need dir to exist and can run normally; If the parent path of "sub4" does not exist, the mkdirs () method will automatically create the parent directory. Method 5

Note: it is similar to method 4, except that the full path is passed in method 4, and the URI corresponding to the full path is passed in method 5. 3. Several common methods of creating new files. For example, we want to create a new file under the subdirectory "dir" of the current directory. There are several methods. Method 1

Note: the above code is used to create a new file "file1. TXT" under the "dir" directory (relative path). Method 2

Note: the above code is used to create a new file "File2. TXT" under the "dir" directory (relative path). Method 3

Note: the above code is used to create a new file "/ home / skywang / dir / file3. TXT" (absolute path) under. This is based on the absolute path under Linux. Under windows, you can create a new file "D: / dir / file4. TXT" through the following code.

Method 4

Note: it is similar to method 3, except that the full path is passed in method 3, and the URI corresponding to the full path is passed in method 4. 4. File API usage example for the detailed usage of the API in file, refer to the example code (filetest. Java):

Running results (running results under Ubuntu 12.04 system, not windows!):

Result description: running the program will create a new directory "dir" and its subdirectories and sub files in the directory where the source file is located. As shown below:

FileDescriptor

Filedescriptor is a file descriptor. Filedescriptor can be used to represent open files, open sockets, etc. Take filedescriptor to represent a file: when filedescriptor represents a file, we can generally regard filedescriptor as the file. However, we cannot operate on this file directly through filedescriptor; If you need to operate the file through filedescriptor, you need to create a new fileoutputstream corresponding to filedescriptor, and then operate the file. In, out, err Introduction (1) in -- descriptor of standard input (keyboard) (2) out -- descriptor of standard output (screen) (3) err -- descriptor of standard error output (screen). Their principles and usage are similar. Let's conduct in-depth research through out. 1.1 function and principle of out out is the descriptor of standard output (screen). But what does it do? We can understand that out stands for standard output (screen). If we want to output information to the screen, we can operate through out; However, out does not provide an interface to output information to the screen (because out is essentially a filedescriptor object, and filedescriptor has no output interface). What shall I do? Very simply, we create the "output stream object" corresponding to out, and then output the information to the screen through the output interface such as write() of the "output stream". The following code:

Executing the above procedure will output the letter 'a' on the screen. In order to facilitate our operation, Java has already encapsulated the "interface that can easily output information on the screen": through system Out, we can easily output information to the screen. Therefore, we can convert the above program into the following code equivalently:

Let's talk about the principle of the above two pieces of code and see the definition of out. It is defined in filedescriptor In Java, the relevant source code is as follows:

From this, we can see that (1) out is a filedescriptor object. It is created through the constructor filedescriptor (int FD). (2) The operation of filedescriptor (int FD): assign a value to the FD object (int type) and create a new use count variable usecount. FD object is a very important variable. "FD = 1" represents "standard output", "FD = 0" represents "standard input", and "FD = 2" represents "standard error output".

The constructor fileoutputstream (filedescriptor fdobj) is used to create the "fileoutputstream object corresponding to file. Out". About system How out is defined. You can refer to "learn more about system. Out. Println (" Hello world ")" through the above learning, 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 convenient system in,System. out,System. Err to use them. In addition, we can also customize the file descriptors of "file", "socket" and so on, and then operate them. Refer to testwrite(), testread() and other interfaces in the following example code. 2. The source code of the sample code is as follows (filedescriptortest. Java):

Operation results:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>