Java Utililty Methods Path File Write nio

List of utility methods to do Path File Write nio

Description

The list of methods to do Path File Write nio are organized into topic(s).

Method

booleancanWrite(Path path)
can Write
if (!isFile(path)) {
    return false;
try {
    return path.toFile().canWrite();
} catch (UnsupportedOperationException e) {
    return false;
voidcheckFilePermissions(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);
booleancompareTable(final String table, final List d1, final List d2, final BufferedWriter errors)
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);
...
booleanisWritePermission(Path p)
is Write Permission
return isPermission(p, Files::isWritable);
BufferedWriternewBufferedWriter(Path path)
new Buffered Writer
return Files.newBufferedWriter(path, CHARSET);
BufferedWriternewBufferedWriter(Path path, OpenOption... options)
new Buffered Writer
return newBufferedWriter(path, StandardCharsets.UTF_8, options);
BufferedWriternewGzipBufferedWriter(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()))));
booleanokayToOverwrite(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"));
voidoverwriteFile(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();
booleanremoveMatchingLines(final Map d1LineMap, final Path tableDir, final String table, final BufferedWriter errors)
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;
...