Java Code Examples for java.nio.file.Files#isWritable()
The following examples show how to use
java.nio.file.Files#isWritable() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FileHelper.java From TencentKona-8 with GNU General Public License v2.0 | 6 votes |
public static boolean isReadOnlyPath(Path path) throws IOException { // Files.isWritable(path) can not really be trusted. At least not on Windows. // If path is a directory, we try to create a new file in it. if (Files.isDirectory(path)) { try { Path f = Files.createFile(Paths.get(path.toString(), "dummyFileToCheckReadOnly")); System.out.printf("Dir is not read-only, created %s, exists=%b%n", f, Files.exists(f)); return false; } catch (AccessDeniedException e) { System.out.printf("'%s' verified read-only by %s%n", path, e.toString()); return true; } } else { boolean isReadOnly = !Files.isWritable(path); System.out.format("isReadOnly '%s': %b%n", path, isReadOnly); return isReadOnly; } }
Example 2
Source File: AppDataRootInitializer.java From molgenis with GNU Lesser General Public License v3.0 | 6 votes |
public static void init() throws IOException { Path appDataRoot = getAppDataRoot(); if (!appDataRoot.toFile().exists()) { LOG.info("Creating application data root directory: {}", appDataRoot); Files.createDirectory(appDataRoot); } else { if (!appDataRoot.toFile().isDirectory()) { throw new IOException( format("Application data root path '%s' is not a directory", appDataRoot)); } if (!Files.isWritable(appDataRoot)) { throw new IOException( format("Application data root directory '%s' is not writable", appDataRoot)); } } LOG.info("Application data directory: {}", appDataRoot); AppDataRootProvider.setAppDataRoot(appDataRoot); }
Example 3
Source File: FileHandler.java From TencentKona-8 with GNU General Public License v2.0 | 5 votes |
private boolean isParentWritable(Path path) { Path parent = path.getParent(); if (parent == null) { parent = path.toAbsolutePath().getParent(); } return parent != null && Files.isWritable(parent); }
Example 4
Source File: RestServerEndpoint.java From flink with Apache License 2.0 | 5 votes |
/** * Checks whether the given directory exists and is writable. If it doesn't exist, this method * will attempt to create it. * * @param uploadDir directory to check * @param log logger used for logging output * @throws IOException if the directory does not exist and cannot be created, or if the * directory isn't writable */ private static synchronized void checkAndCreateUploadDir(final Path uploadDir, final Logger log) throws IOException { if (Files.exists(uploadDir) && Files.isWritable(uploadDir)) { log.info("Using directory {} for file uploads.", uploadDir); } else if (Files.isWritable(Files.createDirectories(uploadDir))) { log.info("Created directory {} for file uploads.", uploadDir); } else { log.warn("Upload directory {} cannot be created or is not writable.", uploadDir); throw new IOException( String.format("Upload directory %s cannot be created or is not writable.", uploadDir)); } }
Example 5
Source File: TenantKeyFileUtil.java From FHIR with Apache License 2.0 | 5 votes |
/** * writes the tenant's key to the given file. * * @param tenantKeyFileName a valid file and location. * @param tenantKey a generated key */ public void writeTenantFile(String tenantKeyFileName, String tenantKey) { if (tenantKeyFileName == null) { throw new IllegalArgumentException("Value for tenant-key-file-name is never set"); } File output = new File(tenantKeyFileName); if (output.exists()) { throw new IllegalArgumentException("tenant-key-file-name must not exist [" + tenantKeyFileName + "]"); } if (tenantKey == null) { throw new IllegalArgumentException("Value for tenant-key is never set when writing tenant key"); } Path path = Paths.get(output.toURI()); boolean writeable = Files.isWritable(path.getParent()); if (!writeable) { throw new IllegalArgumentException("not a writeable directory"); } try { Files.write(path, tenantKey.getBytes()); } catch (IOException e) { throw new IllegalArgumentException("IOException writing the bytes"); } }
Example 6
Source File: XMLProperties.java From Openfire with Apache License 2.0 | 5 votes |
/** * Creates a new XMLPropertiesTest object. * * @param file the file that properties should be read from and written to. * @throws IOException if an error occurs loading the properties. */ public XMLProperties(Path file) throws IOException { this.file = file; if (Files.notExists(file)) { // Attempt to recover from this error case by seeing if the // tmp file exists. It's possible that the rename of the // tmp file failed the last time Jive was running, // but that it exists now. Path tempFile; tempFile = file.getParent().resolve(file.getFileName() + ".tmp"); if (Files.exists(tempFile)) { Log.error("WARNING: " + file.getFileName() + " was not found, but temp file from " + "previous write operation was. Attempting automatic recovery." + " Please check file for data consistency."); Files.move(tempFile, file, StandardCopyOption.REPLACE_EXISTING); } // There isn't a possible way to recover from the file not // being there, so throw an error. else { throw new NoSuchFileException("XML properties file does not exist: " + file.getFileName()); } } // Check read and write privs. if (!Files.isReadable(file)) { throw new IOException("XML properties file must be readable: " + file.getFileName()); } if (!Files.isWritable(file)) { throw new IOException("XML properties file must be writable: " + file.getFileName()); } try (Reader reader = Files.newBufferedReader(file, StandardCharsets.UTF_8)) { buildDoc(reader); } }
Example 7
Source File: Utils.java From termd with Apache License 2.0 | 5 votes |
/** * Removes the specified file - if it is a directory, then its children * are deleted recursively and then the directory itself. * * @param path The file {@link Path} to be deleted - ignored if {@code null} * or does not exist anymore * @param options The {@link LinkOption}s to use * @return The <tt>path</tt> argument * @throws IOException If failed to access/remove some file(s) */ public static Path deleteRecursive(Path path, LinkOption... options) throws IOException { if ((path == null) || (!Files.exists(path))) { return path; } if (Files.isDirectory(path)) { try (DirectoryStream<Path> ds = Files.newDirectoryStream(path)) { for (Path child : ds) { deleteRecursive(child, options); } } } try { // seems that if a file is not writable it cannot be deleted if (!Files.isWritable(path)) { path.toFile().setWritable(true, false); } Files.delete(path); } catch (IOException e) { // same logic as deleteRecursive(File) which does not check if deletion succeeded System.err.append("Failed (").append(e.getClass().getSimpleName()).append(")") .append(" to delete ").append(path.toString()) .append(": ").println(e.getMessage()); } return path; }
Example 8
Source File: ProxyClassesDumper.java From openjdk-8-source with GNU General Public License v2.0 | 5 votes |
private static void validateDumpDir(Path path) { if (!Files.exists(path)) { throw new IllegalArgumentException("Directory " + path + " does not exist"); } else if (!Files.isDirectory(path)) { throw new IllegalArgumentException("Path " + path + " is not a directory"); } else if (!Files.isWritable(path)) { throw new IllegalArgumentException("Directory " + path + " is not writable"); } }
Example 9
Source File: RemoteFileSyncer.java From prebid-server-java with Apache License 2.0 | 5 votes |
/** * Creates if doesn't exists and checks write permissions for the given directory. */ private static void createAndCheckWritePermissionsFor(FileSystem fileSystem, String filePath) { try { final String dirPath = Paths.get(filePath).getParent().toString(); final FileProps props = fileSystem.existsBlocking(dirPath) ? fileSystem.propsBlocking(dirPath) : null; if (props == null || !props.isDirectory()) { fileSystem.mkdirsBlocking(dirPath); } else if (!Files.isWritable(Paths.get(dirPath))) { throw new PreBidException(String.format("No write permissions for directory: %s", dirPath)); } } catch (FileSystemException | InvalidPathException e) { throw new PreBidException(String.format("Cannot create directory for file: %s", filePath), e); } }
Example 10
Source File: FileHandler.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 5 votes |
private boolean isParentWritable(Path path) { Path parent = path.getParent(); if (parent == null) { parent = path.toAbsolutePath().getParent(); } return parent != null && Files.isWritable(parent); }
Example 11
Source File: DatabaseUtils.java From aion with MIT License | 5 votes |
/** * Ensures that the path defined by the dbFile is valid and generates all the directories that * are missing in the path. * * @param dbFile the path to be verified. * @throws IllegalArgumentException when: * <ol> * <li>the given path is not valid; * <li>the directory structure cannot be created; * <li>the path cannot be written to; * <li>the give file is not a directory * </ol> */ public static void verifyAndBuildPath(File dbFile) { // to throw in case of issues with the path IllegalArgumentException exception = new IllegalArgumentException( "The path «" + dbFile.getAbsolutePath() + "» is not valid as reported by the OS or a read/write permissions error occurred." + " Please provide an alternative DB dbFile path in /config/config.xml."); Path path; try { // ask the OS if the path is valid String canonicalPath = dbFile.getCanonicalPath(); path = Paths.get(canonicalPath); } catch (Exception e) { e.printStackTrace(); throw exception; } // try to create the directory if (!dbFile.exists()) { if (!dbFile.mkdirs()) { throw exception; } } if (!Files.isWritable(path) || !Files.isDirectory(path)) { throw exception; } }
Example 12
Source File: ProxyClassesDumper.java From Java8CN with Apache License 2.0 | 5 votes |
private static void validateDumpDir(Path path) { if (!Files.exists(path)) { throw new IllegalArgumentException("Directory " + path + " does not exist"); } else if (!Files.isDirectory(path)) { throw new IllegalArgumentException("Path " + path + " is not a directory"); } else if (!Files.isWritable(path)) { throw new IllegalArgumentException("Directory " + path + " is not writable"); } }
Example 13
Source File: FileHandler.java From jdk8u-dev-jdk with GNU General Public License v2.0 | 5 votes |
private boolean isParentWritable(Path path) { Path parent = path.getParent(); if (parent == null) { parent = path.toAbsolutePath().getParent(); } return parent != null && Files.isWritable(parent); }
Example 14
Source File: PathDocFileFactory.java From openjdk-8-source with GNU General Public License v2.0 | 4 votes |
/** Return true if the file can be written. */ public boolean canWrite() { return Files.isWritable(file); }
Example 15
Source File: SelectRootPathUtils.java From PeerWasp with MIT License | 4 votes |
public static boolean isValidRootPath(Path path) { return path != null && Files.exists(path) && Files.isDirectory(path) && Files.isWritable(path); }
Example 16
Source File: FileInfo.java From CloverETL-Engine with GNU Lesser General Public License v2.1 | 4 votes |
@Override public Boolean canWrite() { return Files.isWritable(file.toPath()); }
Example 17
Source File: PathOperations.java From ph-commons with Apache License 2.0 | 4 votes |
/** * Rename a directory. * * @param aSourceDir * The original directory name. May not be <code>null</code>. * @param aTargetDir * The destination directory name. May not be <code>null</code>. * @return A non-<code>null</code> error code. */ @Nonnull public static FileIOError renameDir (@Nonnull final Path aSourceDir, @Nonnull final Path aTargetDir) { ValueEnforcer.notNull (aSourceDir, "SourceDirectory"); ValueEnforcer.notNull (aTargetDir, "TargetDirectory"); final Path aRealSourceDir = _getUnifiedPath (aSourceDir); final Path aRealTargetDir = _getUnifiedPath (aTargetDir); // Does the source directory exist? if (!aRealSourceDir.toFile ().isDirectory ()) return EFileIOErrorCode.SOURCE_DOES_NOT_EXIST.getAsIOError (EFileIOOperation.RENAME_DIR, aRealSourceDir); // Are source and target different? if (EqualsHelper.equals (aRealSourceDir, aRealTargetDir)) return EFileIOErrorCode.SOURCE_EQUALS_TARGET.getAsIOError (EFileIOOperation.RENAME_DIR, aRealSourceDir); // Does the target directory already exist? if (aRealTargetDir.toFile ().exists ()) return EFileIOErrorCode.TARGET_ALREADY_EXISTS.getAsIOError (EFileIOOperation.RENAME_DIR, aRealTargetDir); // Is the source a parent of target? if (PathHelper.isParentDirectory (aRealSourceDir, aRealTargetDir)) return EFileIOErrorCode.TARGET_IS_CHILD_OF_SOURCE.getAsIOError (EFileIOOperation.RENAME_DIR, aRealSourceDir, aRealTargetDir); // Is the source parent directory writable? final Path aSourceParentDir = aRealSourceDir.getParent (); if (aSourceParentDir != null && !Files.isWritable (aSourceParentDir)) return EFileIOErrorCode.SOURCE_PARENT_NOT_WRITABLE.getAsIOError (EFileIOOperation.RENAME_DIR, aRealSourceDir); // Is the target parent directory writable? final Path aTargetParentDir = aRealTargetDir.getParent (); if (aTargetParentDir != null && aTargetParentDir.toFile ().exists () && !Files.isWritable (aTargetParentDir)) return EFileIOErrorCode.TARGET_PARENT_NOT_WRITABLE.getAsIOError (EFileIOOperation.RENAME_DIR, aRealTargetDir); // Ensure parent of target directory is present PathHelper.ensureParentDirectoryIsPresent (aRealTargetDir); return _perform (EFileIOOperation.RENAME_DIR, PathOperations::_atomicMove, aRealSourceDir, aRealTargetDir); }
Example 18
Source File: PathInfo.java From CloverETL-Engine with GNU Lesser General Public License v2.1 | 4 votes |
@Override public Boolean canWrite() { return Files.isWritable(path); }
Example 19
Source File: StandardDocFileFactory.java From openjdk-jdk9 with GNU General Public License v2.0 | 4 votes |
/** Return true if the file can be written. */ @Override public boolean canWrite() { return Files.isWritable(file); }
Example 20
Source File: StandardDocFileFactory.java From openjdk-jdk9 with GNU General Public License v2.0 | 4 votes |
/** Return true if the file can be written. */ public boolean canWrite() { return Files.isWritable(file); }