课程: Java 8+ Essential Training: Objects and APIs

今天就学习课程吧!

今天就开通帐号,24,600 门业界名师课程任您挑!

Copy files with Path and Files classes

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…

内容