Social networks
You can find me on:
   
Open sharing content

These articles are available under Creative Commons license BY-SA-3.0

In the previous post I told you about the old fashioned way to handle files in java.
In this article we will focus on the new API.
The File class is no longer used. We will use the “Path” class instead.
Files is a utility class to create either files or directories, that takes a path in the constructor:

	Path filePath=Paths.get("/home/laura/demo.txt");
	Files.createFile(filePath);
	
	Path dirPath=Paths.get("/home/laura/newdir");
	Files.createDirectory(dirPath);
	
	Path nestedDirPath=Paths.get("/home/laura/newdir3/newdir4");
	Files.createDirectories(nestedDirPath);

The “createDirectories” method created the whole file system structure (the nested directories).

With NIO.2 copying or deleting a file or directory have become a matter of one line of code, thanks to the very straightforward methods “copy”, “delete”, etc.

Many things can be achieved with the utility classes Files and Paths.

Comments are closed.