The tutorial then covers methods for checking , deleting , copying , and moving files. Random access files are explained and issues specific to symbolic and hard links are examined. Next, some of the very powerful, but more advanced, topics are covered.
First, the capability to recursively walk the file tree is demonstrated, followed by information about how to search for files using wild cards.
Next, how to watch a directory for changes is explained and demonstrated. Then, methods that didn't fit elsewhere are given some attention. The check for the existence of the file and the creation of the directory if it does not exist are a single operation that is atomic with respect to all other filesystem activities that might affect the directory.
The createDirectories method should be used where it is required to create all nonexistent parent directories first. The attrs parameter is optional file-attributes to set atomically when creating the directory. Unlike the createDirectory method, an exception is not thrown if the directory could not be created because it already exists.
The attrs parameter is optional file-attributes to set atomically when creating the nonexistent directories. Each file attribute is identified by its name. If this method fails, then it may do so after creating some, but not all, of the parent directories. If dir is not an absolute path then its toAbsolutePath may need to be invoked to get its absolute path. This may invoke the security manager's checkPropertyAccess method to check access to the system property user.
The resulting Path is associated with the same FileSystem as the given directory. The details as to how the name of the file is constructed is implementation dependent and therefore not specified. Where possible the prefix and suffix are used to construct candidate names in the same manner as the File. As with the File. Alternatively, a shutdown-hook , or the File. When no file attributes are specified, then the resulting file may have more restrictive access permissions to files created by the File.
Parameters: dir - the path to directory in which to create the file prefix - the prefix string to be used in generating the file's name; may be null suffix - the suffix string to be used in generating the file's name; may be null , in which case ". The resulting Path is associated with the default FileSystem. This method works in exactly the manner specified by the createTempFile Path,String,String,FileAttribute[] method for the case that the dir parameter is the temporary-file directory.
Parameters: prefix - the prefix string to be used in generating the file's name; may be null suffix - the suffix string to be used in generating the file's name; may be null , in which case ". The details as to how the name of the directory is constructed is implementation dependent and therefore not specified.
Where possible the prefix is used to construct candidate names. As with the createTempFile methods, this method is only part of a temporary-file facility. A shutdown-hook , or the File.
This method works in exactly the manner specified by createTempDirectory Path,String,FileAttribute[] method for the case that the dir parameter is the temporary-file directory. The target parameter is the target of the link. It may be an absolute or relative path and may not exist.
When the target is a relative path then file system operations on the resulting link are relative to the path of the link.
The attrs parameter is optional attributes to set atomically when creating the link. Where symbolic links are supported, but the underlying FileStore does not support symbolic links, then this may fail with an IOException. Additionally, some operating systems may require that the Java virtual machine be started with implementation specific privileges to create symbolic links, in which case this method may throw IOException.
The link parameter locates the directory entry to create. The existing parameter is the path to an existing file. This method creates a new directory entry for the file so that it can be accessed using link as the path. On some file systems this is known as creating a "hard link". Whether the file attributes are maintained for the file or for each directory entry is file system specific and therefore not specified.
Typically, a file system requires that all links directory entries for a file be on the same file system. Furthermore, on some platforms, the Java virtual machine may require to be started with implementation specific privileges to create hard links or to create links to directories.
An implementation may require to examine the file to determine if the file is a directory. Consequently this method may not be atomic with respect to other file system operations. If the file is a symbolic link then the symbolic link itself, not the final target of the link, is deleted.
If the file is a directory then the directory must be empty. In some implementations a directory has entries for special files or links that are created when the directory is created. In such implementations a directory is considered empty when only the special entries exist. This method can be used with the walkFileTree method to delete a directory and all entries in the directory, or an entire file-tree where required.
On some operating systems it may not be possible to remove a file when it is open and in use by this Java virtual machine or other programs. As with the delete Path method, an implementation may need to examine the file to determine if the file is a directory. If the file is a symbolic link, then the symbolic link itself, not the final target of the link, is deleted. This method copies a file to the target file with the options parameter specifying how the copy is performed.
By default, the copy fails if the target file already exists or is a symbolic link, except if the source and target are the same file, in which case the method completes without copying the file. File attributes are not required to be copied to the target file.
If symbolic links are supported, and the file is a symbolic link, then the final target of the link is copied. If the file is a directory then it creates an empty directory in the target location entries in the directory are not copied. This method can be used with the walkFileTree method to copy a directory and all entries in the directory, or an entire file-tree where required.
If the target file exists and is a symbolic link, then the symbolic link itself, not the target of the link, is replaced. The exact file attributes that are copied is platform and file system dependent and therefore unspecified. Minimally, the last-modified-time is copied to the target file if supported by both the source and target file store.
Copying of file timestamps may result in precision loss. If the file is a symbolic link, then the symbolic link itself, not the target of the link, is copied. It is implementation specific if file attributes can be copied to the new link. An implementation of this interface may support additional implementation specific options. Copying a file is not an atomic operation. If an IOException is thrown then it possible that the target file is incomplete or some of its file attributes have not been copied from the source file.
The check for the existence of the file and the creation of the new file may not be atomic with respect to other file system activities.
If a symbolic link is copied the security manager is invoked to check LinkPermission "symbolic". By default, this method attempts to move the file to the target file, failing if the target file exists except if the source and target are the same file, in which case this method has no effect.
If the file is a symbolic link then the symbolic link itself, not the target of the link, is moved. This method may be invoked to move an empty directory. When invoked to move a directory that is not empty then the directory is moved if it does not require moving the entries in the directory. For example, renaming a directory on the same FileStore will usually not require moving the entries in the directory. When moving a directory requires that its entries be moved then this method fails by throwing an IOException.
To move a file tree may involve copying rather than moving directories and this can be done using the copy method in conjunction with the Files. If the target file exists then it is implementation specific if the existing file is replaced or this method fails by throwing an IOException. If the move cannot be performed as an atomic file system operation then AtomicMoveNotSupportedException is thrown.
This can arise, for example, when the target location is on a different FileStore and would require that the file be copied, or target location is associated with a different provider to this object. Where the move requires that the file be copied then the last-modified-time is copied to the new file. An implementation may also attempt to copy other file attributes but is not required to fail if the file attributes cannot be copied.
When the move is performed as a non-atomic operation, and a IOException is thrown, then the state of the files is not defined. The original file and the target file may both exist, the target file may be incomplete or some of its file attributes may not been copied from the original file.
If the file system supports symbolic links then this method is used to read the target of the link, failing if the file is not a symbolic link. The target of the link need not exist. The returned Path object will be associated with the same file system as link. Once a reference to the FileStore is obtained it is implementation specific if operations on the returned FileStore , or FileStoreAttributeView objects obtained from it, continue to depend on the existence of the file.
In particular the behavior is not defined for the case that the file is deleted or moved to a different file store. If both Path objects are equal then this method returns true without checking if the file exists.
If the two Path objects are associated with different providers then this method returns false. Otherwise, this method checks if both Path objects locate the same file, and depending on the implementation, may require to open or access both files. If the file system and files remain static, then this method implements an equivalence relation for non-null Paths.
It is reflexive : for Path f , isSameFile f,f should return true. It is transitive : for three Paths f , g , and h , if isSameFile f,g returns true and isSameFile g,h returns true , then isSameFile g,h will return return true. See Also: BasicFileAttributes.
The exact definition of hidden is platform or provider dependent. On UNIX for example a file is considered to be hidden if its name begins with a period character '. On Windows a file is considered hidden if it isn't a directory and the DOS hidden attribute is set. Depending on the implementation this method may require to access the file system to determine if the file is considered hidden.
This method uses the installed FileTypeDetector implementations to probe the given file to determine its content type. Each file type detector's probeContentType is invoked, in turn, to probe the file type.
If the file is recognized then the content type is returned. If the file is not recognized by any of the installed file type detectors then a system-default file type detector is invoked to guess the content type. A given invocation of the Java virtual machine maintains a system-wide list of file type detectors. Installed file type detectors are loaded using the service-provider loading facility defined by the ServiceLoader class. Installed file type detectors are loaded using the system class loader.
If the system class loader cannot be found then the extension class loader is used; If the extension class loader cannot be found then the bootstrap class loader is used. File type detectors are typically installed by placing them in a JAR file on the application class path or in the extension directory, the JAR file contains a provider-configuration file named java.
If the process of locating or instantiating the installed file type detectors fails then an unspecified error is thrown. The ordering that installed providers are located is implementation specific.
The string is guaranteed to be parsable according to the grammar in the RFC. A file attribute view provides a read-only or updatable view of a set of file attributes.
This method is intended to be used where the file attribute view defines type-safe methods to read or update the file attributes. Reference: API Doc. Hi Pankaj : very nice set of information but as expected from you the way of presentation can be much more better. I know you long before as we both belong to the same organization.
I follow you in youtube that was also very nice. Your email address will not be published. Prev Simple Java Programs.
Next Java String Array. Pankaj I love Open Source technologies and writing about my experience about them is my passion. Follow Author. Comments Supriya says:. January 2, at am. Subhashis says:. December 6, at am. Leave a Reply Cancel reply Your email address will not be published.
0コメント