Java 8 File Operations - Copy,Delete,Move

Deleting a File or Directory

The Files class provides two deletion methods.

1 : The delete(Path) method deletes the file or throws an exception if the deletion fails

2 : The deleteIfExists(Path) method also deletes the file, but if the file does not exist, no exception is thrown.

Delete File
1
2
3
4
5
6
7
8
9
10
11
12
13

public static void main(String[] args) {

Path path = Paths.get("C:\\data\\temp\\temp.txt");
try {
Files.delete(path);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

Delete Empty Directory
1
2
3
4
5
6
7
8
9
10
11
12
13

public static void main(String[] args) {

Path path = Paths.get("C:\\data\\temp\\");
try {
Files.delete(path);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

Trying to delete Non Empty Directory will throw DirectoryNotEmptyException.
So, First we need to delete all files inside a particular non-empty directory:

Delete Non Empty Directory
1
2
3
4
5
6
7
8
9
10
11

public static void main(String[] args) {

Path path = Paths.get("C:\\data\\temp\\");
Files.walk(path)
.sorted(Comparator.reverseOrder())
.map(Path::toFile)
.forEach(File::delete);

}

Copying a File or Directory

You can copy a file or directory by using the
copy(Path, Path, CopyOption...) method. The copy fails if the target file exists, unless the REPLACE_EXISTING option is specified.

This method takes a varargs argument. The following StandardCopyOption and LinkOption enums are supported:

REPLACE_EXISTING – replace a file if it exists
COPY_ATTRIBUTES – copy metadata to the new file
NOFOLLOW_LINKS – shouldn’t follow symbolic links

Copy File
1
2
3
4
5
6
7
8

public static void main(String[] args) {
Path sourcepath = Paths.get("C:\\data\\temp\\temp.txt");
Path destinationepath = Paths.get("C:\\data\\temp\\destination.txt");
Files.copy(sourcepath, destinationepath, StandardCopyOption.REPLACE_EXISTING);

}

Copy Empty Directory
1
2
3
4
5
6
7
8

public static void main(String[] args) {
Path sourcepath = Paths.get("C:\\data\\temp\\mydir");
Path destinationepath = Paths.get("C:\\data\\temp\\destinationDir");
Files.copy(sourcepath, destinationepath, StandardCopyOption.REPLACE_EXISTING);

}

Copy Non empty Directory
Directories can be copied. However, files inside the directory are not copied, so the new directory is empty even when the original directory contains files.

Copy Non Empty Directory recursively
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

public static void main(String[] args) {
Path sourcepath = Paths.get("C:\\data\\temp\\mydir");
Path destinationepath = Paths.get("C:\\data\\temp\\destinationDir");
Files.walk(sourcepath)
.forEach(source -> copy(source, destinationepath.resolve(sourcepath.relativize(source))));
}

static void copy(Path source, Path dest) {
try {
Files.copy(source, dest, StandardCopyOption.REPLACE_EXISTING);
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}

Moving a File or Directory

You can move a file or directory by using the move(Path, Path, CopyOption...) method.
The move fails if the target file exists, unless the REPLACE_EXISTING option is specified.

Empty directories can be moved. If the directory is not empty, the move is allowed when the directory can be moved without moving the contents of that directory. On UNIX systems, moving a directory within the same partition generally consists of renaming the directory. In that situation, this method works even when the directory contains files.

This method takes a varargs argument – the following StandardCopyOption enums are supported:

REPLACE_EXISTING – Performs the move even when the target file already exists. If the target is a symbolic link, the symbolic link is replaced but what it points to is not affected.
ATOMIC_MOVE – Performs the move as an atomic file operation. If the file system does not support an atomic move, an exception is thrown. With an ATOMIC_MOVE you can move a file into a directory and be guaranteed that any process watching the directory accesses a complete file.

Move File
1
2
3
4
5
6
7
8

public static void main(String[] args) {
Path sourcepath = Path sourcepath = Paths.get("C:\\data\\temp\\temp.txt");
Path destinationepath = Paths.get("C:\\data\\temp\\mydir\\temp.txtr");

Files.move(sourcepath, destinationepath, StandardCopyOption.REPLACE_EXISTING);

}
Move Empty Directory
1
2
3
4
5
6
7
8

public static void main(String[] args) {
Path sourcepath = Paths.get("C:\\data\\temp\\copyme");
Path destinationepath = Paths.get("C:\\data\\temp\\mydir\\copyme");
Files.move(sourcepath, destinationepath, StandardCopyOption.REPLACE_EXISTING);


}
Move Non Empty Directory
1
2
3
4
5
6
7

public static void main(String[] args) {
Path sourcepath = Paths.get("C:\\data\\temp\\copyme");
Path destinationepath = Paths.get("C:\\data\\temp\\mydir\\copyme");
Files.move(sourcepath, destinationepath, StandardCopyOption.REPLACE_EXISTING);

}
Share Comments