// @topic T11903 Checking file permissions, creating directories // @brief Using Path, Paths, Files, AccessMode.WRITE READ EXECUTE package week11; import java.nio.file.AccessMode; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.FileSystem; import static java.nio.file.AccessMode.*; public class MainApp { public static void main(String[] args) { String strPath = "does/not/exist"; createDirectory( strPath ); strPath = "does/not/exist/myfile.txt"; Path filePath = Paths.get( strPath ); try { filePath.getFileSystem().provider().checkAccess( filePath, AccessMode.WRITE ); filePath.getFileSystem().provider().checkAccess( filePath, AccessMode.READ ); filePath.getFileSystem().provider().checkAccess( filePath, AccessMode.EXECUTE ); } catch ( java.io.IOException ex ) { System.out.println( "Exception: " + ex.toString() ); } }//main // create parts of directory path as needed. public static void createDirectory( String strPath ) { String[] pathArray = strPath.split("/"); // "does" "not" "exist" String assembledPath = ""; try { for ( int idx = 0; idx < pathArray.length; ++idx ) { // for each subdirectory, assemble the path string: if ( assembledPath.length() != 0 ) assembledPath += "/"; assembledPath += pathArray[idx]; Path directoryPath = Paths.get( assembledPath ); if ( !Files.exists(directoryPath) ) { Files.createDirectory(directoryPath); System.out.println( "created directory [" + assembledPath + "]"); } else { System.out.println( "directory [" + assembledPath + "] already exists"); } } } catch ( java.io.IOException ex ) { System.out.println( "Exception: " + ex.toString() ); } }//createDirectory }//class MainApp