Java Reference
In-Depth Information
static boolean moveFiles(Path from, Path to) {
if(!isDirectory(from)) {
System.out.println(
"Cannot move files. " + from + " is not a
directory.");
return false;
} else if(!isDirectory(to)) {
System.out.println("Cannot move files. " + to + " is not a
directory.");
return false;
}
try (DirectoryStream<Path> files = Files.newDirectoryStream(from,
"*.*")) {
System.out.println("Starting move files...");
for(Path file : files) {
Files.move(file, to.resolve(file.getFileName()));
// Move
the file
System.out.println(" " + file.getFileName() + " moved.");
}
} catch(IOException e) {
System.err.println("I/O error in copyFiles. " + e);
return false;
}
return true;
}
MoveAndCopyFiles.java
The next method just pauses the program until the Enter key is pressed:
static void waitForEnter() {
try {
System.in.read();
} catch(IOException e) {
System.err.println(e);
}
}
MoveAndCopyFiles.java
Pausing the program is achieved by waiting for input from System.in . The read() can throw an excep-
tion of type IOException so the method call is in a try block.
The last method you need to complete the example is main() :
Search WWH ::




Custom Search