From the course: Advanced Java Programming

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Using the Path class in Java

Using the Path class in Java - Java Tutorial

From the course: Advanced Java Programming

Start my 1-month free trial

Using the Path class in Java

- [Instructor] The best way to move, copy, and delete files in Java is with the path class. This offers the additional functionality which is not available in the file class. In this example, I have a simple file called Hello World, which just contains the text, Hello World. This is inside the same directory as my project, PathClassExample. One of the useful things that can be done with a path class is checking if a file exists and deleting it if it does. I can do this with my Hello World file. First, I need to create a path object, which I'll call path. Then I call a static get method of the paths class and pass in the name of my file. Because this is in the same directory as my project, I just need to put the name of the file. I also need to make sure I have the input statements for the path class and the paths class. Now I can check if the file exists and delete it if it does in one single line of code. I need to use a class called files to do this. Like the path class, this only…

Contents