Dos View Setting an attribute's value and extracting a single attribute by name - Java File Path IO

Java examples for File Path IO:DOS File

Introduction

Use setAttribute() and getAttribute() methods, respectively

Demo Code

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.DosFileAttributes;

public class Main {
  public static void main(String[] args) {
    DosFileAttributes attr = null;
    Path path = Paths.get("C:/folder1/folder2/folder4", "test.txt");

    //setting the hidden attribute to true
    try {/*from  w  w  w.ja  va2s  . c om*/
        Files.setAttribute(path, "dos:hidden", true, LinkOption.NOFOLLOW_LINKS);
    } catch (IOException e) {
        System.err.println(e);
    }

    //getting the hidden attribute 
    try {
        boolean hidden = (Boolean) Files.getAttribute(path, "dos:hidden", LinkOption.NOFOLLOW_LINKS);
        System.out.println("Is hidden ? " + hidden);
    } catch (IOException e) {
        System.err.println(e);
    }
  }
}

Result

DOS attributes can be acquired with the following names:

  • hidden
  • readonly
  • system
  • archive

The generally accepted form is [view-name:]attribute-name.

The view-name is dos.


Related Tutorials