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.

Copying files with the Path class

Copying files with the Path class - Java Tutorial

From the course: Advanced Java Programming

Start my 1-month free trial

Copying files with the Path class

- [Instructor] One of the useful methods in the files class is the copy method, this method has options to specify how the copy should be done. In this example I am going to copy the contents of a file called example.txt which I have in my desktop folder. This contains several lines of text. I want to create a new file and copy the contents into it. First I need to create the Path variable, which I'll call source and use Paths.get and pass in the path to example.txt. I also need to make sure I have the import statements for Path and Paths. Next I want to create a file called new.txt in the same directory as example.txt so I create another Path called dest and put Paths.get and then pass in the path to the new file I want to create. All I have to do now is call the static copy method of the files class, I need to pass in my source file and my dest file and I need to make sure I have imported the files class. I also need to make sure I catch any IO exceptions. So I'll surround this with…

Contents