Java Path File Read nio setReadOnly(Path file)

Here you can find the source of setReadOnly(Path file)

Description

set Read Only

License

Open Source License

Declaration

public static boolean setReadOnly(Path file) 

Method Source Code

//package com.java2s;
/**/*from   www  .  jav  a  2 s .co  m*/
 * This file is part of CubeEngine.
 * CubeEngine is licensed under the GNU General Public License Version 3.
 *
 * CubeEngine is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * CubeEngine is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with CubeEngine.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.nio.file.Files;
import java.nio.file.Path;

import java.nio.file.attribute.DosFileAttributeView;
import java.nio.file.attribute.PosixFileAttributeView;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.PosixFilePermissions;

import java.util.Set;

public class Main {
    private static final Set<PosixFilePermission> READ_ONLY_PERM = PosixFilePermissions.fromString("r--r-----");

    public static boolean setReadOnly(Path file) {
        try {
            Files.getFileAttributeView(file, PosixFileAttributeView.class).setPermissions(READ_ONLY_PERM);
        } catch (Exception ignore) {
            try {
                Files.getFileAttributeView(file, DosFileAttributeView.class).setReadOnly(true);
            } catch (Exception ignored) {
                return false;
            }
        }
        return true;
    }
}

Related

  1. readStringFromPath(Path path)
  2. readTextFile(Path p)
  3. readTextFileFromResources(String filepath, ClassLoader classLoader)
  4. readTextFromFile(String textFilePath)
  5. setReadable(final Path path, boolean readable)