import java.nio.file.Path;
import java.nio.file.Paths;
public class PathExample {
public static void main(String[] args) {
// Creating an Absolute Path (Linux, MacOS, FreeBSD etc.)
String fileName = "/Users/linqiong/Downloads/profile";
Path path = Paths.get(fileName);
// Creating a Relative Path
Path currentDir = Paths.get(".");
System.out.println(currentDir.toAbsolutePath());
// output: /Users/linqiong/workspace/app/java/base/.
Path parentDir = Paths.get("..");
System.out.println(parentDir.toAbsolutePath());
// output: /Users/linqiong/workspace/app/java/base/..
}
}
String fileName = "/Users/linqiong/Downloads/profile";
Path path = Paths.get(fileName);
boolean pathExists = Files.exists(path, new LinkOption[]{LinkOption.NOFOLLOW_LINKS});
Path newFolder = Paths.get("/Users/linqiong/Downloads/java");
try {
Path newDir = Files.createDirectory(newFolder);
} catch (FileAlreadyExistsException e) {
System.out.println("The directory already exists");
} catch (IOException e) {
e.printStackTrace();
}
Path sourcePath = Paths.get("/Users/linqiong/Downloads/profile");
Path destPath = Paths.get("/Users/linqiong/Downloads/java/profile");
try {
Files.copy(sourcePath, destPath);
} catch (FileAlreadyExistsException e) {
//destination file already exists
} catch (IOException e) {
e.printStackTrace();
}
try {
Files.delete(destPath);
} catch (IOException e) {
e.printStackTrace();
}