Java IO programming — file operation class

The Java language provides support for file operating system operation, and this support is in Java io. The file class is defined, that is, in the whole Java In the IO package, the file class is the only class related to the operation of the file itself (create, delete, rename, etc.), and if you want to operate the file class, you must provide a complete path, and then you can call the corresponding methods for processing.

1. Basic use of file class

When you open the JDK document, you can find that the file class is a subclass of the comparable interface, so the objects of the file class can be sorted. When processing the file class, you need to set the access path for it. The path configuration is mainly processed through the construction method of the file class:

· construction method: public file (string pathname), set the complete path to be operated;

· construction method: public file (file parent, string child), set the parent path and subdirectory;

If you want to perform basic file operations now, you can use the following methods:

· create a new file: public Boolean createnewfile() throws IOException;

· judge whether the file exists: public Boolean exists();

· delete file: public Boolean delete();

Example: create a file using the file class (D: \ mldn. Txt)

It can be found from the code that the file class implements the processing of the file itself.

2. File class operation depth

1. In the process of actual software project development and operation, the project development is often carried out in windows, and the project release is carried out based on Linux or UNIX system to ensure the safety of production links when the project is deployed;

There are different path separators in different operating systems: Windows separator "\" and Linux separator "/. Therefore, the separators in different system environments must be considered in the initial development. Therefore, in order to solve this problem, the file class provides a constant: public static final string separator;

Example: normal path writing

However, with the continuous strengthening of the adaptability of the system, the current path operation can also be used at will.

2. When creating a file, there is an important premise: the parent path of the file must exist first.

·How to get the parent path: public file getparentfile();

·Create a multi-level directory: public Boolean mkdirs();

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
分享
二维码
< <上一篇
下一篇>>