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.

CrossRef.eps For more information, see Path Class.

Methods

Method

Description

static Path copy(Path source, Path target)

Copies the source file to the target file.

static Path move(Path source, Path target)

Moves or renames the source file to the target path and deletes the original file.

static Path create Directory(Path dir)

Creates a directory. All directories in the path up to the directory that the new directory is to be created within must already exist.

static Path create Directories(Path dir)

Creates a new directory, including any intermediate directories in the path.

static Path createFile(Path file)

Creates a file.

static void delete (Path path)

Deletes the file or directory. The method throws an exception if the file or directory doesn’t exist or couldn’t be deleted.

static void delete IfExists(Path path)

Deletes the file or directory if it exists. The method doesn’t throw an exception if the file or directory doesn’t exist.

static boolean exists(Path path)

Returns true if the file exists on the hard drive or false if the file doesn’t exist on the hard drive.

static boolean notExists(Path path)

Returns true if the file doesn’t exist on the hard drive or false if the file does exist on the hard drive.

static String toAbsolutePath()

Returns the full absolute path to the file, including the drive letter if run on a Windows system.

static DirectoryStream newDirectoryStream (Path p)

Gets a DirectoryStream object that you can use to read the contents of the directory.

static DirectoryStream newDirectoryStream (Path p, String filter)

Gets a DirectoryStream object that’s filtered by the filter string, which can contain wildcards (such as *.txt to retrieve just .txt files).

static boolean isDirectory(Path path)

Returns true if this File object is a directory or false if it is a file or does not exist.

static String toString()

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);

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset