Usage based on java files class and paths class (detailed explanation)

Great changes have taken place in file IO in Java 7, and many new classes have been introduced:

...... Wait, to replace the original Java io. File IO operation mode of file

1. Path replaces file

A Path represents a path that is hierarchical and composed of a sequence of directory and file name elements separated by a special separator or delimiter.

Path is used to represent file paths and files. There are many ways to construct a path object to represent a file path, or a file:

1) The first is the two static methods of the final class paths. How to construct a path object from a path string:

2) Filesystems construct:

3) Conversion between file and path, conversion between file and URI:

4) Create a file:

Posixfilepermission is not supported to specify RWX permission under windows.

5)Files. Newbufferedreader reads the file:

You can see using files Newbufferedreader is much simpler than the original FileInputStream and BufferedReader packaging.

If the specified character encoding is incorrect, an exception malformedinputexception may be thrown, or a garbled code may be read:

6) File write operation:

7) Traverse a folder:

The above is to traverse a single directory. It will not traverse the whole directory. To traverse the entire directory, you need to use: files walkFileTree

8) Traverse the entire file directory:

A practical example:

Delete all eligible pictures under the directory: filepath matches(".*_[1|2]{1}\\.(?i)(jpg|jpeg|gif|bmp|png)")

Generate thumbnails of the specified size for all pictures in the directory. a. Jpg generates a_ 1.jpg

2. Powerful Java nio. file. Files

1) Create directories and files:

Note the creation of directories and files Createdirectories and files CreateFile cannot be mixed. You must have a directory before you can create a file in the directory.

2) File copy:

Copy from file to file: files copy(Path source,Path target,CopyOption options);

Copy from input stream to file: files copy(InputStream in,CopyOption options);

Copy from file to output stream: files copy(Path source,OutputStream out);

3) Traverse a directory and folder, as described above: files newDirectoryStream , Files. walkFileTree

4) Read file properties:

5) Read and set file permissions:

The files class is a powerful mess. Almost all the related attributes and operations of files and directories are supported by the desired API. I'm too lazy to continue here. Please refer to the jdk8 documentation for details.

A practical example:

The scenario is that when SQL Server exports data, DataTime will be imported into hexadecimal binary format, such as: cast (0x0000a2a500fc2e4f as datetime))

Therefore, the above program exports the last DataTime field, deletes cast (0x0000a2a500fc2e4f as datetime), and generates a new SQL script without datetime field value. Used to import into mysql.

In fact, there is a better way to do it halfway. Using SQL YOG, you can flexibly import the tables and data in SQL server into mysql. It is difficult to use the function of exporting data provided by SQL server.

The above usage (detailed explanation) based on java files class and paths class is all the content shared by Xiaobian. I hope it can give you a reference and support programming tips.

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