How to operate files in Java 7

Source: Internet
Author: User

Abstract: The code snippets in this article are modified by verified programs. Observe these code snippets and you will find that, compared with previous versions, file-related operations in Java 7 become much simpler. The following code snippets are modified by verified programs. Observe these code snippets and you will find that file-related operations in Java 7 are much easier than those in previous versions. By using the various methods provided in the new Files class, you can complete the following file operations with only one line of code: create and delete a file copy file move/rename this file is based on the premise that you are familiar with the new Path class provided in Java 7, if you are not familiar with this class, here is a simple sentence: Path is a logical concept of location in the file system, such as c: \ and .. /foobar.txt is a Path. The code snippet below the file creation and deletion shows you the basic usage of creating a file using the Files. createFile (Path target) method. Path target = Paths. get ("D :\\ Backup \ MyStuff.txt"); Path file = Files. createFile (target); in many cases, for security reasons, you may want to set properties on the created file, such as whether the file is readable, writable, or writable. These attributes depend on the type of the file system. You need to use the corresponding permission helper class of the file system to complete this operation. For example, PosixFilePermission and PosixFilePermissions are designed for POSIX file systems. The following describes how to set the read and write permissions for files in the POSIX file system. Path target = Paths. get ("D :\\ Backup \ MyStuff.txt"); Set <PosixFilePermission> perms = PosixFilePermissions. fromString ("rw-"); FileAttribute <Set <PosixFilePermission> attr = PosixFilePermissions. asFileAttribute (perms); Files. createFile (target, attr); this java. nio. file. the attribute package provides many classes about FilePermission. Warning when creating a file with the permission attribute, pay attention to whether the folder containing the file has the mandatory constraint of permission. For example, you will find that, due to these restrictions, even though you have assigned the rw-rw permission to the created file, the actual creation result is rw-r -. It is easier to delete Files. Use the Files. delete (Path) method. Path target = Paths. get ("D :\\ Backup \ MyStuff.txt"); Files. delete (target); the Code below the copy and move Files shows you the use of Files. copy (Path source, Path target) is used to copy objects. Path source = Paths. get ("C :\\ My Documents \ Stuff.txt"); Path target = Paths. get ("D :\\ Backup \ MyStuff.txt"); Files. copy (source, target); often, you may want to specify some operation settings during file copying. In Java 7, you can use StandardCopyOption enum to set these attributes. The following is an example. Import static java. nio. file. standardCopyOption. *; Path source = Paths. get ("C :\\ My Documents \ Stuff.txt"); Path target = Paths. get ("D :\\ Backup \ MyStuff.txt"); Files. copy (source, target, REPLACE_EXISTING); attributes that can be used during the copy operation include COPY_ATTRIBUTES (Retain file attributes) and ATOMIC_MOVE (ensure that the operation of the mobile transaction is successful, otherwise roll back ). The operations for moving objects are similar to those for copying objects. Use the Files. move (Path source, Path target) method. Similarly, you can specify the attributes of a move operation and set them using parameters in the Files. move (Path source, Path target, CopyOptions...) method. Import static java. nio. file. standardCopyOption. *; Path source = Paths. get ("C :\\ My Documents \ Stuff.txt"); Path target = Paths. get ("D :\\ Backup \ MyStuff.txt"); Files. move (source, target, REPLACE_EXISTING, COPY_ATTRIBUTES );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.