Trying to create multiple directories using the createDirectory() method will throw a NoSuchFileException. Instead use the createDirectories() method.
Trying to create a directory that already exists will throw a FileAlreadyExistsException. i.e running the same code used to create a single directory more than once on the same path/filename.
See Code Snippet below.
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.io.IOException;
import static java.lang.System.err;
public class OcpPrepare {
public static void main(String[] args) {
//-----------First Part-----------//
//trying to create multiple direcotries One, Two and Three
Path newDir = FileSystems.getDefault().getPath("C:\\Users\\", "One\\Two\\Three");
try{
Files.createDirectory(newDir);
}
catch(IOException e){
err.println(e); //NoSuchFileException Thrown ;;
}
//-----------Second Part-----------//
//trying to create a directory that already exist
Path newDir = FileSystems.getDefault().getPath("C:\\Users\\One");
try{
Files.createDirectory(newDir);
}
catch(IOException e){
err.println(e); //FileAlreadyExistsException Thrown ;;
}
}
}
Trying to create a directory that already exists will throw a FileAlreadyExistsException. i.e running the same code used to create a single directory more than once on the same path/filename.
See Code Snippet below.
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.io.IOException;
import static java.lang.System.err;
public class OcpPrepare {
public static void main(String[] args) {
//-----------First Part-----------//
//trying to create multiple direcotries One, Two and Three
Path newDir = FileSystems.getDefault().getPath("C:\\Users\\", "One\\Two\\Three");
try{
Files.createDirectory(newDir);
}
catch(IOException e){
err.println(e); //NoSuchFileException Thrown ;;
}
//-----------Second Part-----------//
//trying to create a directory that already exist
Path newDir = FileSystems.getDefault().getPath("C:\\Users\\One");
try{
Files.createDirectory(newDir);
}
catch(IOException e){
err.println(e); //FileAlreadyExistsException Thrown ;;
}
}
}