From the course: Java 8+ Essential Training: Objects and APIs

Unlock the full course today

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

Copy files with Path and Files classes

Copy files with Path and Files classes - Java Tutorial

From the course: Java 8+ Essential Training: Objects and APIs

Start my 1-month free trial

Copy files with Path and Files classes

- I previously described how to copy files using the classes that were part of Java from the beginning. But in Java 7, a new set of classes and interfaces were created that do the work much more easily. This coding style is also supported in Android, starting with Android 8 Oreo, but like some other new language features, it won't work on older devices. I'll start by creating an instance of a class called Path. A path in Java represents either a file or a directory. I'll name this sourcefile, and I'll initialize it with Paths.get. This method receives a series of strings. The first string is the directory. Subsequent strings can either be the directory, or if it's the last string, it'll be the name of the file. So my file is stored in the files directory. I'll enter that as the first value, and then I'll enter the name of the file, Loremipsum.txt, and if you had sub-directories after files, you would place those directory names between the files directory and the file name. I'll…

Contents