Example usage for java.nio.file.attribute DosFileAttributes isHidden

List of usage examples for java.nio.file.attribute DosFileAttributes isHidden

Introduction

In this page you can find the example usage for java.nio.file.attribute DosFileAttributes isHidden.

Prototype

boolean isHidden();

Source Link

Document

Returns the value of the hidden attribute.

Usage

From source file:Test.java

public static void main(String[] args) throws Exception {
    Path path = FileSystems.getDefault().getPath("/home/docs/users.txt");
    DosFileAttributeView view = Files.getFileAttributeView(path, DosFileAttributeView.class);
    DosFileAttributes attributes = view.readAttributes();

    System.out.println("isArchive: " + attributes.isArchive());
    System.out.println("isHidden: " + attributes.isHidden());
    System.out.println("isReadOnly: " + attributes.isReadOnly());
    System.out.println("isSystem: " + attributes.isSystem());
}

From source file:Main.java

public static void main(String[] args) {
    DosFileAttributes attr = null;
    Path path = Paths.get("C:/tutorial/Java/JavaFX", "Topic.txt");

    try {//  w w  w .  j  a  v a  2 s .  c  o m
        attr = Files.readAttributes(path, DosFileAttributes.class);
    } catch (IOException e) {
        System.err.println(e);
    }
    System.out.println("Is Hidden ? " + attr.isHidden());
}

From source file:com.nsn.squirrel.tab.utils.PathUtils.java

/**
 * @param path//from   w w w  .  j  a  v  a 2 s . c  o m
 * @return
 */
public static String getDosAttributesString(Path path) {

    DosFileAttributeView basicView = Files.getFileAttributeView(path, DosFileAttributeView.class,
            LinkOption.NOFOLLOW_LINKS);
    StringBuilder attrs = new StringBuilder();
    try {
        // + all basic attributes
        DosFileAttributes dosAttrs = basicView.readAttributes();
        attrs.append(dosAttrs.isReadOnly() ? "r" : "-"); //$NON-NLS-1$ //$NON-NLS-2$
        attrs.append(dosAttrs.isHidden() ? "h" : "-");//$NON-NLS-1$ //$NON-NLS-2$
        attrs.append(dosAttrs.isArchive() ? "a" : "-");//$NON-NLS-1$ //$NON-NLS-2$
        attrs.append(dosAttrs.isSystem() ? "s" : "-");//$NON-NLS-1$ //$NON-NLS-2$
    } catch (IOException e) {
        log.warn("unable to read DOS attributes.", e); //$NON-NLS-1$
    }
    return attrs.toString();
}

From source file:cane.brothers.e4.commander.utils.PathUtils.java

/**
 * @param path// www.  ja v a 2 s  .c om
 * @return
 */
public static String getDosAttributesString(Path path) {
    DosFileAttributeView basicView = Files.getFileAttributeView(path, DosFileAttributeView.class,
            LinkOption.NOFOLLOW_LINKS);
    StringBuilder attrs = new StringBuilder();

    try {
        // + all basic attributes
        DosFileAttributes dosAttrs = basicView.readAttributes();

        attrs.append(dosAttrs.isReadOnly() ? "r" : "-"); //$NON-NLS-1$ //$NON-NLS-2$
        attrs.append(dosAttrs.isHidden() ? "h" : "-");//$NON-NLS-1$ //$NON-NLS-2$
        attrs.append(dosAttrs.isArchive() ? "a" : "-");//$NON-NLS-1$ //$NON-NLS-2$
        attrs.append(dosAttrs.isSystem() ? "s" : "-");//$NON-NLS-1$ //$NON-NLS-2$
    } catch (IOException e) {
        log.warn("unable to read DOS attributes.", e); //$NON-NLS-1$
    }
    return attrs.toString();
}

From source file:org.fim.util.DosFilePermissionsTest.java

@Test
public void weCanRetrieveTheStringVersion() {
    DosFileAttributes dosFileAttributes = mock(DosFileAttributes.class);

    assertThat(DosFilePermissions.toString(dosFileAttributes)).isEqualTo("");

    Mockito.when(dosFileAttributes.isArchive()).thenReturn(true);
    assertThat(DosFilePermissions.toString(dosFileAttributes)).isEqualTo("A");

    Mockito.when(dosFileAttributes.isHidden()).thenReturn(true);
    assertThat(DosFilePermissions.toString(dosFileAttributes)).isEqualTo("AH");

    Mockito.when(dosFileAttributes.isReadOnly()).thenReturn(true);
    assertThat(DosFilePermissions.toString(dosFileAttributes)).isEqualTo("AHR");

    Mockito.when(dosFileAttributes.isSystem()).thenReturn(true);
    assertThat(DosFilePermissions.toString(dosFileAttributes)).isEqualTo("AHRS");
}

From source file:de.jwi.jfm.FileWrapper.java

public String getAttributes() throws IOException {
    FileSystem fileSystem = FileSystems.getDefault();
    Set<String> fileSystemViews = fileSystem.supportedFileAttributeViews();

    File file = getFile();//from   www  .j  av a2 s .  com
    Path p = file.toPath();

    try {
        if (fileSystemViews.contains("posix")) {
            Set<PosixFilePermission> posixFilePermissions = Files.getPosixFilePermissions(p,
                    LinkOption.NOFOLLOW_LINKS);

            PosixFileAttributes attrs = Files.getFileAttributeView(p, PosixFileAttributeView.class)
                    .readAttributes();

            String owner = attrs.owner().toString();
            String group = attrs.group().toString();

            String permissions = PosixFilePermissions.toString(attrs.permissions());

            String res = String.format("%s %s %s", permissions, owner, group);
            return res;
        } else if (fileSystemViews.contains("dos")) {
            StringWriter sw = new StringWriter();
            DosFileAttributeView attributeView = Files.getFileAttributeView(p, DosFileAttributeView.class);
            DosFileAttributes dosFileAttributes = null;

            dosFileAttributes = attributeView.readAttributes();
            if (dosFileAttributes.isArchive()) {
                sw.append('A');
            }
            if (dosFileAttributes.isHidden()) {
                sw.append('H');
            }
            if (dosFileAttributes.isReadOnly()) {
                sw.append('R');
            }
            if (dosFileAttributes.isSystem()) {
                sw.append('S');
            }

            return sw.toString();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return "";
}

From source file:VOBackupFile.java

/**
 * Wrapper for metadata of backuped file.
 *
 * @param file backuped file//from   w  ww.  j a  va 2  s.c  om
 */
public VOBackupFile(File file) {
    this.path = file.getAbsolutePath();
    this.directory = file.isDirectory();
    this.symLink = Files.isSymbolicLink(file.toPath());

    this.size = ((file.isDirectory() || symLink) ? 0 : file.length());
    this.modify = new Date(file.lastModified());

    if (symLink) {
        try {
            symlinkTarget = Files.readSymbolicLink(file.toPath()).toString();
        } catch (IOException e) {
            e.printStackTrace(); //TODO Lebeda - oetit do logu
        }
    } else {
        // advanced attributes

        try {

            owner = Files.getOwner(file.toPath(), LinkOption.NOFOLLOW_LINKS).getName();

            if (Files.getFileStore(file.toPath()).supportsFileAttributeView(DosFileAttributeView.class)) {
                dosAttr = Boolean.TRUE;
                final DosFileAttributes dosFileAttributes = Files.readAttributes(file.toPath(),
                        DosFileAttributes.class, LinkOption.NOFOLLOW_LINKS);

                dosArchive = dosFileAttributes.isArchive();
                dosHidden = dosFileAttributes.isHidden();
                dosSystem = dosFileAttributes.isSystem();
                dosReadOnly = dosFileAttributes.isReadOnly();
            } else {
                dosAttr = Boolean.FALSE;
            }

            if (Files.getFileStore(file.toPath()).supportsFileAttributeView(PosixFileAttributeView.class)) {
                posixAttr = Boolean.TRUE;
                final PosixFileAttributes posixFileAttributes = Files.readAttributes(file.toPath(),
                        PosixFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
                posixGroup = posixFileAttributes.group().getName();
                posixPermitions = PosixFilePermissions.toString(posixFileAttributes.permissions());
            } else {
                posixAttr = Boolean.FALSE;
            }

        } catch (IOException e) {
            e.printStackTrace(); //Todo implementovat
        }
    }

}

From source file:org.syncany.operations.actions.FileSystemAction.java

protected void setFileAttributes(FileVersion reconstructedFileVersion, File reconstructedFilesAtFinalLocation)
        throws IOException {
    if (FileUtil.isWindows()) {
        if (reconstructedFileVersion.getDosAttributes() != null) {
            logger.log(Level.INFO,
                    "     - Setting DOS attributes: " + reconstructedFileVersion.getDosAttributes() + " ...");

            DosFileAttributes dosAttrs = FileUtil
                    .dosAttrsFromString(reconstructedFileVersion.getDosAttributes());
            Path filePath = Paths.get(reconstructedFilesAtFinalLocation.getAbsolutePath());

            Files.setAttribute(filePath, "dos:readonly", dosAttrs.isReadOnly());
            Files.setAttribute(filePath, "dos:hidden", dosAttrs.isHidden());
            Files.setAttribute(filePath, "dos:archive", dosAttrs.isArchive());
            Files.setAttribute(filePath, "dos:system", dosAttrs.isSystem());
        }// www  . j av  a 2  s  . c  o m
    } else if (FileUtil.isUnixLikeOperatingSystem()) {
        if (reconstructedFileVersion.getPosixPermissions() != null) {
            logger.log(Level.INFO, "     - Setting POSIX permissions: "
                    + reconstructedFileVersion.getPosixPermissions() + " ...");

            Set<PosixFilePermission> posixPerms = PosixFilePermissions
                    .fromString(reconstructedFileVersion.getPosixPermissions());

            Path filePath = Paths.get(reconstructedFilesAtFinalLocation.getAbsolutePath());
            Files.setPosixFilePermissions(filePath, posixPerms);
        }
    }
}

From source file:org.syncany.operations.down.actions.FileSystemAction.java

protected void setFileAttributes(FileVersion reconstructedFileVersion, File reconstructedFilesAtFinalLocation)
        throws IOException {
    if (EnvironmentUtil.isWindows()) {
        if (reconstructedFileVersion.getDosAttributes() != null) {
            logger.log(Level.INFO,
                    "     - Setting DOS attributes: " + reconstructedFileVersion.getDosAttributes() + " ...");

            DosFileAttributes dosAttrs = FileUtil
                    .dosAttrsFromString(reconstructedFileVersion.getDosAttributes());
            Path filePath = Paths.get(reconstructedFilesAtFinalLocation.getAbsolutePath());

            try {
                Files.setAttribute(filePath, "dos:readonly", dosAttrs.isReadOnly());
                Files.setAttribute(filePath, "dos:hidden", dosAttrs.isHidden());
                Files.setAttribute(filePath, "dos:archive", dosAttrs.isArchive());
                Files.setAttribute(filePath, "dos:system", dosAttrs.isSystem());
            } catch (IOException e) {
                logger.log(Level.WARNING, "     - WARNING: Cannot set file attributes for " + filePath, e);
            }//from   w  ww . j av  a2 s.  c o  m
        }
    } else if (EnvironmentUtil.isUnixLikeOperatingSystem()) {
        if (reconstructedFileVersion.getPosixPermissions() != null) {
            logger.log(Level.INFO, "     - Setting POSIX permissions: "
                    + reconstructedFileVersion.getPosixPermissions() + " ...");

            Set<PosixFilePermission> posixPerms = PosixFilePermissions
                    .fromString(reconstructedFileVersion.getPosixPermissions());

            Path filePath = Paths.get(reconstructedFilesAtFinalLocation.getAbsolutePath());

            try {
                Files.setPosixFilePermissions(filePath, posixPerms);
            } catch (IOException e) {
                logger.log(Level.WARNING, "     - WARNING: Cannot set file permissions for " + filePath, e);
            }
        }
    }
}

From source file:org.application.backupsync.PathName.java

public JSONObject getAttrs() throws IOException, JSONException {
    JSONObject result;// ww w.  jav  a  2 s. c  o  m
    BasicFileAttributes attr;
    DosFileAttributes dosAttr;
    PosixFileAttributes posixAttr;

    result = new JSONObject();
    attr = Files.readAttributes(this.path, BasicFileAttributes.class);

    result.append("ctime", attr.creationTime().toMillis());
    result.append("mtime", attr.lastModifiedTime().toMillis());
    //result.append("symlink", attr.isSymbolicLink()); //Redundant
    result.append("size", attr.size());

    if (System.getProperty("os.name").startsWith("Windows")) {
        dosAttr = Files.readAttributes(this.path, DosFileAttributes.class);

        result.append("dos:archive", dosAttr.isArchive());
        result.append("dos:hidden", dosAttr.isHidden());
        result.append("dos:readonly", dosAttr.isReadOnly());
        result.append("dos:system", dosAttr.isSystem());
    } else {
        posixAttr = Files.readAttributes(this.path, PosixFileAttributes.class);

        result.append("posix:symlink", posixAttr.isSymbolicLink());
        result.append("posix:owner", posixAttr.owner());
        result.append("posix:group", posixAttr.group());
        result.append("posix:permission", PosixFilePermissions.toString(posixAttr.permissions()));
    }

    return result;
}