List of utility methods to do Path File Write nio
boolean | canWrite(Path path) can Write if (!isFile(path)) { return false; try { return path.toFile().canWrite(); } catch (UnsupportedOperationException e) { return false; |
void | checkFilePermissions(final String filepath, final boolean canRead, final boolean canWrite) check File Permissions final Path path = Paths.get(filepath); final File file = path.toFile(); if (canRead && !file.canRead()) { throw new IllegalArgumentException("Can't read file" + filepath); if (canWrite && !file.canWrite()) { throw new IllegalArgumentException("Can't write to file" + filepath); |
boolean | compareTable(final String table, final List compare Table final Map<String, Integer> d1LineMap = new HashMap<String, Integer>(); for (final Path dir : d1) { final Path tableDir = dir.resolve(table); if (!(Files.exists(tableDir) && Files.isDirectory(tableDir))) { errors.write("Dump directory " + tableDir + " doesn't exist"); return false; buildLineMap(d1LineMap, tableDir); ... |
boolean | isWritePermission(Path p) is Write Permission return isPermission(p, Files::isWritable);
|
BufferedWriter | newBufferedWriter(Path path) new Buffered Writer return Files.newBufferedWriter(path, CHARSET);
|
BufferedWriter | newBufferedWriter(Path path, OpenOption... options) new Buffered Writer return newBufferedWriter(path, StandardCharsets.UTF_8, options);
|
BufferedWriter | newGzipBufferedWriter(Path path) This method is able to determine whether a file is GZipped and return a BufferedReader in any case return new BufferedWriter( new OutputStreamWriter(new GZIPOutputStream(new FileOutputStream(path.toFile())))); |
boolean | okayToOverwrite(Path file) Returns true if okay to overwrite a file ("cp -i") String answer = System.console().readLine("overwrite %s (yes/no)? ", file); return (answer.equalsIgnoreCase("y") || answer.equalsIgnoreCase("yes")); |
void | overwriteFile(final String newFilePath, final String oldFilePath) Overwrite the destination file with the source file FileChannel src = null; FileChannel dest = null; try { File oldFile = new File(oldFilePath).getCanonicalFile(); File newFile = new File(newFilePath).getCanonicalFile(); if (!oldFile.isFile()) { throw new IOException("old file path does not exist (" + oldFilePath + ")"); if (!newFile.isFile()) { throw new IOException("new file path does not exist (" + newFilePath + ")"); src = new FileInputStream(newFile).getChannel(); dest = new FileOutputStream(oldFile).getChannel(); long transferCount = 0; long size = src.size(); do { transferCount += dest.transferFrom(src, transferCount, size - transferCount); } while (transferCount < size); } catch (IOException e) { throw e; } finally { try { if (src != null) { src.close(); if (dest != null) { dest.close(); } catch (IOException e) { e.printStackTrace(); |
boolean | removeMatchingLines(final Map remove Matching Lines boolean passed = true; try (DirectoryStream<Path> listing = Files.newDirectoryStream(tableDir)) { for (final Path file : listing) { try (BufferedReader in = getReaderForFile(file)) { String line = in.readLine(); while (line != null) { if (!d1LineMap.containsKey(line)) { passed = false; ... |