Skip to main content

Posts

Showing posts with the label nio

Java NIO: Working With Path Objects

import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; import java.nio.file.attribute.BasicFileAttributes; public class PathDemo { public static void main(String[] args) { createTestDirectoryStructure(); Path path = Paths.get(".\\testroot\\path1\\sub1\\..\\sub2\\file1"); System.out.printf("File:\"%s\" %n\t File name:%s %n\t Parent name:%s %n\t Root:%s %n\t Absolute:%b %n",path,path.getFileName(), path.getParent(), path.getRoot(),path.isAbsolute()); System.out.printf("%n\t Name count:%d %n\t First name:%s %n\t Third name:%s %n",path.getNameCount(), path.getName(0), path.getName(1)); System.out.printf("%n\t URI:%s%n",path.toUri()); /** OUTPUT File:".\testroot\path1\sub1\..\sub2\file1"