This code snippet show you an example on how to set the value of file attributes. Here we will set the DosFileAttributes
. To set the value of file attributes we use the Files.setAttributes()
method. To set DosFileAttributes
we can use the following attributes: "dos:archive"
, "dos:hidden"
, "dos:readonly"
and "dos:system"
.
For details let’s see the code snippet below:
package org.kodejava.io;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.DosFileAttributes;
public class UpdateDosFileAttributesExample {
public static void main(String[] args) throws Exception {
String path = "D:/resources/data.txt";
Path file = Paths.get(path);
// Get current Dos file attributes and print it.
DosFileAttributes attr = Files.readAttributes(file, DosFileAttributes.class);
printAttributes(attr);
// Set a new file attributes.
Files.setAttribute(file, "dos:archive", false);
Files.setAttribute(file, "dos:hidden", false);
Files.setAttribute(file, "dos:readonly", false);
Files.setAttribute(file, "dos:system", false);
// Read the newly set file attributes and print it.
attr = Files.readAttributes(file, DosFileAttributes.class);
printAttributes(attr);
}
/**
* Print the DosFileAttributes information.
*
* @param attr DosFileAttributes.
*/
private static void printAttributes(DosFileAttributes attr) {
System.out.println("isArchive() = " + attr.isArchive());
System.out.println("isHidden() = " + attr.isHidden());
System.out.println("isReadOnly() = " + attr.isReadOnly());
System.out.println("isSystem() = " + attr.isSystem());
System.out.println("----------------------------------------");
}
}
The output of the code snippet:
isArchive() = true
isHidden() = true
isReadOnly() = true
isSystem() = true
----------------------------------------
isArchive() = false
isHidden() = false
isReadOnly() = false
isSystem() = false
----------------------------------------
Latest posts by Wayan (see all)
- How do I create a string of repeated characters? - September 1, 2023
- How do I convert datetime string with optional part to a date object? - August 28, 2023
- How do I split large excel file into multiple smaller files? - April 15, 2023