
- #Java get file path relative to another file how to
- #Java get file path relative to another file code
Path targetFile = Paths.get("e:\\Java\\JavaSE\\NIO\\NioVideo.mp4")
#Java get file path relative to another file code
Copying Files ExamplesThe following code copies a video file from the current directory (relative path) to another directory (absolute path) and replaces the target file if exists: Path sourceFile = Paths.get("NioVideo.mp4")
SecurityException: if the user doesn’t have read/write permission.Ģ. UnsupportedOperationException: if the options contain an option that is not supported. IOException: if an I/O error occurs when reading or writing. DirectoryNotEmptyException: if the target file cannot be replaced because it is a non-empty directory (when the REPLACE_EXISTING option is specified). FileAlreadyExistsException: if the target file already exists but it cannot be replaced because the REPLACE_EXISTING option is not specified. If the copy option is not specified, then the copy fails if the target file already exists or is a symbolic link.These copy methods can throw the following checked exceptions: NOFOLLOW_LINKS: If a file is a symbolic link, then the link itself, not the target of the link, is copied. REPLACE_EXISTING: replace an existing file if it exists. COPY_ATTRIBUTES: copy attributes to the new file, e.g. Use the enums StandardCopyOption and LinkOption for this parameter with the following values: If the source is a directory, then only the directory is created even when the original directory contains files.The optional parameter CopyOption specifying how the copy should be done. options): copies a file to a target file, and returns the path to the target file. Path copy(Path source, Path target, CopyOption. This method is ideal if you have an OutputStream already opened.
long copy(Path source, OutputStream out): copies all bytes from a file to an output stream, and returns the number of bytes read or written.This method is ideal if you have an InputStream already opened. options): copies all bytes from an input stream to a file, and returns the number of bytes read or written. long copy(InputStream in, Path target, CopyOption.
Files helper class provides three different utility methods for copying a file:
#Java get file path relative to another file how to
This Java tutorial guides you how to copy a file or copy a whole directory (including sub files and sub directories) using the Java NIO API.