List of usage examples for java.nio.file Files isWritable
public static boolean isWritable(Path path)
From source file:Main.java
public static void main(String[] args) throws Exception { Path path = FileSystems.getDefault().getPath("/home/docs/users.txt"); System.out.println(Files.isWritable(path)); }
From source file:Main.java
public static void main(String[] args) { Path path = FileSystems.getDefault().getPath("C:/tutorial/Java/JavaFX", "Demo.txt"); boolean is_writable = Files.isWritable(path); }
From source file:net.cyllene.hackerrank.downloader.HackerrankDownloader.java
public static void main(String[] args) { // Parse arguments and set up the defaults DownloaderSettings.cmd = parseArguments(args); if (DownloaderSettings.cmd.hasOption("help")) { printHelp();/*from ww w.j a v a 2 s . co m*/ System.exit(0); } if (DownloaderSettings.cmd.hasOption("verbose")) { DownloaderSettings.beVerbose = true; } /** * Output directory logic: * 1) if directory exists, ask for -f option to overwrite, quit with message * 2) if -f flag is set, check if user has access to a parent directory * 3) if no access, quit with error * 4) if everything is OK, remember the path */ String sDesiredPath = DownloaderSettings.outputDir; if (DownloaderSettings.cmd.hasOption("directory")) { sDesiredPath = DownloaderSettings.cmd.getOptionValue("d", DownloaderSettings.outputDir); } if (DownloaderSettings.beVerbose) { System.out.println("Checking output dir: " + sDesiredPath); } Path desiredPath = Paths.get(sDesiredPath); if (Files.exists(desiredPath) && Files.isDirectory(desiredPath)) { if (!DownloaderSettings.cmd.hasOption("f")) { System.out.println("I wouldn't like to overwrite existing directory: " + sDesiredPath + ", set the --force flag if you are sure. May lead to data loss, be careful."); System.exit(0); } else { System.out.println( "WARNING!" + System.lineSeparator() + "--force flag is set. Overwriting directory: " + sDesiredPath + System.lineSeparator() + "WARNING!"); } } if ((Files.exists(desiredPath) && !Files.isWritable(desiredPath)) || !Files.isWritable(desiredPath.getParent())) { System.err .println("Fatal error: " + sDesiredPath + " cannot be created or modified. Check permissions."); // TODO: use Exceptions instead of system.exit System.exit(1); } DownloaderSettings.outputDir = sDesiredPath; Integer limit = DownloaderSettings.ITEMS_TO_DOWNLOAD; if (DownloaderSettings.cmd.hasOption("limit")) { try { limit = ((Number) DownloaderSettings.cmd.getParsedOptionValue("l")).intValue(); } catch (ParseException e) { System.out.println("Incorrect limit: " + e.getMessage() + System.lineSeparator() + "Using default value: " + limit); } } Integer offset = DownloaderSettings.ITEMS_TO_SKIP; if (DownloaderSettings.cmd.hasOption("offset")) { try { offset = ((Number) DownloaderSettings.cmd.getParsedOptionValue("o")).intValue(); } catch (ParseException e) { System.out.println("Incorrect offset: " + e.getMessage() + " Using default value: " + offset); } } DownloaderCore dc = DownloaderCore.INSTANCE; List<HRChallenge> challenges = new LinkedList<>(); // Download everything first Map<String, List<Integer>> structure = null; try { structure = dc.getStructure(offset, limit); } catch (IOException e) { System.err.println("Fatal Error: could not get data structure."); e.printStackTrace(); System.exit(1); } challengesLoop: for (Map.Entry<String, List<Integer>> entry : structure.entrySet()) { String challengeSlug = entry.getKey(); HRChallenge currentChallenge = null; try { currentChallenge = dc.getChallengeDetails(challengeSlug); } catch (IOException e) { System.err.println("Error: could not get challenge info for: " + challengeSlug); if (DownloaderSettings.beVerbose) { e.printStackTrace(); } continue challengesLoop; } submissionsLoop: for (Integer submissionId : entry.getValue()) { HRSubmission submission = null; try { submission = dc.getSubmissionDetails(submissionId); } catch (IOException e) { System.err.println("Error: could not get submission info for: " + submissionId); if (DownloaderSettings.beVerbose) { e.printStackTrace(); } continue submissionsLoop; } // TODO: probably should move filtering logic elsewhere(getStructure, maybe) if (submission.getStatus().equalsIgnoreCase("Accepted")) { currentChallenge.getSubmissions().add(submission); } } challenges.add(currentChallenge); } // Now dump all data to disk try { for (HRChallenge currentChallenge : challenges) { if (currentChallenge.getSubmissions().isEmpty()) continue; final String sChallengePath = DownloaderSettings.outputDir + "/" + currentChallenge.getSlug(); final String sSolutionPath = sChallengePath + "/accepted_solutions"; final String sDescriptionPath = sChallengePath + "/problem_description"; Files.createDirectories(Paths.get(sDescriptionPath)); Files.createDirectories(Paths.get(sSolutionPath)); // FIXME: this should be done the other way String plainBody = currentChallenge.getDescriptions().get(0).getBody(); String sFname; if (!plainBody.equals("null")) { sFname = sDescriptionPath + "/english.txt"; if (DownloaderSettings.beVerbose) { System.out.println("Writing to: " + sFname); } Files.write(Paths.get(sFname), plainBody.getBytes(StandardCharsets.UTF_8.name())); } String htmlBody = currentChallenge.getDescriptions().get(0).getBodyHTML(); String temporaryHtmlTemplate = "<html></body>" + htmlBody + "</body></html>"; sFname = sDescriptionPath + "/english.html"; if (DownloaderSettings.beVerbose) { System.out.println("Writing to: " + sFname); } Files.write(Paths.get(sFname), temporaryHtmlTemplate.getBytes(StandardCharsets.UTF_8.name())); for (HRSubmission submission : currentChallenge.getSubmissions()) { sFname = String.format("%s/%d.%s", sSolutionPath, submission.getId(), submission.getLanguage()); if (DownloaderSettings.beVerbose) { System.out.println("Writing to: " + sFname); } Files.write(Paths.get(sFname), submission.getSourceCode().getBytes(StandardCharsets.UTF_8.name())); } } } catch (IOException e) { System.err.println("Fatal Error: couldn't dump data to disk."); System.exit(1); } }
From source file:Test.java
private static void displayFileAttributes(Path path) throws Exception { String format = "Exists: %s %n" + "notExists: %s %n" + "Directory: %s %n" + "Regular: %s %n" + "Executable: %s %n" + "Readable: %s %n" + "Writable: %s %n" + "Hidden: %s %n" + "Symbolic: %s %n" + "Last Modified Date: %s %n" + "Size: %s %n"; System.out.printf(format, Files.exists(path, LinkOption.NOFOLLOW_LINKS), Files.notExists(path, LinkOption.NOFOLLOW_LINKS), Files.isDirectory(path, LinkOption.NOFOLLOW_LINKS), Files.isRegularFile(path, LinkOption.NOFOLLOW_LINKS), Files.isExecutable(path), Files.isReadable(path), Files.isWritable(path), Files.isHidden(path), Files.isSymbolicLink(path), Files.getLastModifiedTime(path, LinkOption.NOFOLLOW_LINKS), Files.size(path)); }
From source file:com.igormaznitsa.sciareto.ui.tree.NodeProject.java
public NodeProject(@Nonnull final NodeProjectGroup group, @Nonnull final File folder) { super(group, true, folder.getName(), !Files.isWritable(folder.toPath())); this.folder = folder; reloadSubtree();// w ww . ja v a2s.c om }
From source file:com.acmutv.ontoqa.tool.io.IOManager.java
/** * Checks if a resource is writable./*from ww w . ja v a2s. co m*/ * @param resource the resource to check. * @return true if the resource is writable; false, otherwise. */ public static boolean isWritableResource(String resource) { final Path path = FileSystems.getDefault().getPath(resource).toAbsolutePath(); return Files.isWritable(path); }
From source file:com.github.blindpirate.gogradle.util.IOUtils.java
public static Path ensureDirExistAndWritable(Path path) { Assert.isTrue(path.isAbsolute(), "path must be absolute!"); File dir = path.toFile();//from w w w .ja va 2 s . com IOUtils.forceMkdir(dir); Assert.isTrue(Files.isWritable(dir.toPath()), "Cannot write to directory:" + path); return path; }
From source file:edu.cornell.mannlib.oce.startup.LogFileAdjuster.java
/** * Confirm that the log file will go into an existing directory, and that * either it does exist, or we can create it. Should also be writeable. *//* w w w.j a v a 2s . com*/ private void validateLogFileSetting(SpecialLog specialLog, String logFile) throws LogAdjustmentException { Path path = Paths.get(logFile); if (!Files.exists(path.getParent())) { throw new LogAdjustmentException("Cannot create " + specialLog + " log file '" + logFile + "', parent directory does not exist."); } if (!Files.exists(path)) { try { Files.createFile(path); } catch (IOException e) { throw new LogAdjustmentException("Failed to create " + specialLog + " log file '" + path + "'", e); } } if (!Files.isWritable(path)) { throw new LogAdjustmentException( "Cannot write to log file '" + logFile + "' for " + specialLog + " log."); } }
From source file:org.openmrs.module.owa.web.controller.OwaManageController.java
@ModelAttribute("settingsValid") public boolean settingsValid() { boolean settingsValid = false; String appFolderPath = Context.getAdministrationService().getGlobalProperty(AppManager.KEY_APP_FOLDER_PATH); if (null != appFolderPath) { File file = new File(appFolderPath); if (file.isDirectory() && Files.isWritable(file.toPath())) { settingsValid = true;/*from ww w. ja v a2 s .co m*/ } } return settingsValid; }
From source file:org.osiam.OsiamHome.java
public void initialize() { try {//from w w w . j a v a2 s. c o m if (Files.notExists(osiamHome)) { Files.createDirectories(osiamHome); } else { checkState(Files.isDirectory(osiamHome), "'osiam.home' (%s) is not a directory", osiamHome); checkState(Files.isReadable(osiamHome), "'osiam.home' (%s) is not readable", osiamHome); checkState(Files.isExecutable(osiamHome), "'osiam.home' (%s) is not accessible", osiamHome); } if (!isEmpty(osiamHome)) { return; } checkState(Files.isWritable(osiamHome), "'osiam.home' (%s) is not writable", osiamHome); Resource[] resources = new PathMatchingResourcePatternResolver().getResources("classpath:/home/**/*"); for (Resource resource : resources) { // don't process directories if (resource.getURL().toString().endsWith("/")) { continue; } copyToHome(resource, osiamHome); } if (Files.notExists(osiamHome.resolve("data"))) { Files.createDirectories(osiamHome.resolve("data")); } hasInitializedHome = true; } catch (IOException e) { throw new IllegalStateException("Could not initialize osiam.home", e); } }