Files Class
Package: java.nio.file
The Files
class consists entirely of static methods that can perform operations on files, which are represented by Path
objects. For example, to delete a file, you first get a Path
object that points to the file. Then, you pass the Path
object to the delete method of the Files
class to delete the file.
Methods
Method |
Description |
|
Copies the source file to the target file. |
|
Moves or renames the source file to the target path and deletes the original file. |
|
Creates a directory. All directories in the path up to the directory that the new directory is to be created within must already exist. |
|
Creates a new directory, including any intermediate directories in the path. |
|
Creates a file. |
|
Deletes the file or directory. The method throws an exception if the file or directory doesn’t exist or couldn’t be deleted. |
|
Deletes the file or directory if it exists. The method doesn’t throw an exception if the file or directory doesn’t exist. |
|
Returns |
|
Returns |
|
Returns the full absolute path to the file, including the drive letter if run on a Windows system. |
|
Gets a |
|
Gets a |
|
Returns |
|
Returns the pathname for this file or directory as a string. |
Testing whether a file exists
You can test to see whether a file exists, like this:
Path p = Paths.get(path);
if (!Files.exists(p))
System.out.println
(“The input file does not exist!”);
Creating a new file
To create a new file, use the createFile
method, like this:
Path p = Paths.get(“c:\test.txt”);
try
{
Files.createFile(p);
System.out.println (“File created!”);
}
catch (Exception e)
{
System.out.println (
“Error: “ + e.getMessage());
}
Note that the createFile
method throws an exception if the file couldn’t be created. The getMessage
method of this exception returns a message that explains why the file couldn’t be created.
Getting directory contents
The newDirectoryStream
method retrieves the contents of a directory as a DirectoryStream
object, which can then be processed with an enhanced for
statement. For more information, see DirectoryStream Class.
Copying a file
Use the copy
method to create a copy of a file:
Path oldPath = Paths.get(“C:\myfolder\myfile.txt”);
Path newPath = Paths.get(“C:\myfolder\yourfile.txt”);
Files.copy
In the preceding example, the file myfile.txt
is copied to yourfile.txt
.
Moving or renaming files
Use the move
method to move or rename files. If the source and target files are in the same directory, the file is simply renamed.
The following example renames a file named myfile.txt
to yourfile.txt
:
Path oldPath = Paths.get(“C:\myfolder\myfile.txt”);
Path newPath = Paths.get(“C:\myfolder\yourfile.txt”);
Files.move(oldPath, newPath);
Here’s an example that moves the file to a different folder:
Path oldPath = Paths.get(“C:\myfolder\myfile.txt”);
Path newPath = Paths.get(“C:\yourfolder\yourfile.txt”);
Files.move(oldPath, newPath);
Deleting a file
To delete a file, use the delete
method:
Path p = Paths.get(“C:\myfolder\myfile.txt”);
Files.delete(p);
If the file is a directory, the directory must be empty to be deleted.
Note that the delete
method throws a NoSuchFileException
if the file to be deleted does not exist. The deleteIfExists
does not throw an exception if the file does not exist:
Path p = Paths.get(“C:\myfolder\myfile.txt”);
Files.deleteIfExists(p);