6.3. Read and change file and directory attributes - Source Code

Code:


package c6.s3;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributeView;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;
import java.nio.file.attribute.UserDefinedFileAttributeView;
import java.util.List;

/**
 * @author Mikalai Zaikin
 */
public class AttrTest {

    public static void main(String... args) throws IOException  {
        Path file = Paths.get("C:\\home\\zaikin\\foo\\test.txt");
        BasicFileAttributeView basicView = Files.getFileAttributeView(file, BasicFileAttributeView.class);
        BasicFileAttributes basicAttrs = basicView.readAttributes();
        FileTime newTime = FileTime.fromMillis(basicAttrs.lastModifiedTime().toMillis() + 60000);
        basicView.setTimes(newTime, null, null);

        UserDefinedFileAttributeView userView = Files.getFileAttributeView(file, UserDefinedFileAttributeView.class);

        String attribName  = "COPYRIGHT";
        String attribValue = "MIKALAI.ZAIKIN";
        userView.write(attribName, Charset.defaultCharset().encode(attribValue));

        List<String> attribList = userView.list();
        for (String s : attribList) {
            System.out.print(String.format("User defined attribute: %s", s));
            ByteBuffer buf = ByteBuffer.allocate(userView.size(s));
            userView.read(s, buf);
            buf.flip();
            String value = Charset.defaultCharset().decode(buf).toString();
            System.out.println(String.format("; value: %s", value));
        }
    }
}

					

output:

User defined attribute: COPYRIGHT; value: MIKALAI.ZAIKIN
					

Code:

package c6.s3;

import java.io.IOException;
import java.nio.file.FileStore;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * @author Mikalai Zaikin
 */
public class FileStoreAttributes {

    public static void main(String... args) throws IOException {
        Path file = Paths.get("C:\\home\\zaikin\\foo\\test.txt");
        FileStore store = Files.getFileStore(file);

        long total = store.getTotalSpace()  / 1024 / 1024;
        long avail = store.getUsableSpace() / 1024 / 1024;
        long used = (store.getTotalSpace() - store.getUnallocatedSpace()) / 1024 / 1024;

        System.out.print(String.format("Root : %s%n", file.getRoot()));
        System.out.print(String.format("Store name: %s%n", store.name()));
        System.out.print(String.format("Total: %d MB; used: %d MB; available: %d MB", total, used, avail));
    }
}
					

output:

Root : C:\
Store name: SYSTEM
Total: 89996 MB; used: 83315 MB; available: 6681 MB
					

Code:

package c6.s3;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.AclEntry;
import java.nio.file.attribute.AclFileAttributeView;
import java.nio.file.attribute.FileOwnerAttributeView;
import java.nio.file.attribute.UserPrincipal;

/**
 * @author Mikalai Zaikin
 */
public class OwnerTest {

    public static void main(String... args) throws IOException  {
        Path file = Paths.get("C:\\home\\zaikin\\foo\\test.txt");
        UserPrincipal owner = file.getFileSystem().getUserPrincipalLookupService().lookupPrincipalByName("zaikin");
        Files.setOwner(file, owner);

        FileOwnerAttributeView ownerView = Files.getFileAttributeView(file, FileOwnerAttributeView.class);
        UserPrincipal o = ownerView.getOwner();
        System.out.println(String.format("Owner : %s%n",o.getName()));

        AclFileAttributeView aclView = Files.getFileAttributeView(file, AclFileAttributeView.class);
        for (AclEntry entry : aclView.getAcl()) {
            System.out.print(entry.principal().getName());
            System.out.print(entry.permissions());
            System.out.println();
        }
    }
}
					

output:

Owner : HOME\zaikin

BUILTIN\Administrators[READ_NAMED_ATTRS, WRITE_DATA, EXECUTE, DELETE_CHILD, WRITE_NAMED_ATTRS, READ_ATTRIBUTES, READ_DATA, WRITE_OWNER, READ_ACL, DELETE, WRITE_ATTRIBUTES, APPEND_DATA, WRITE_ACL, SYNCHRONIZE]
NT AUTHORITY\SYSTEM[READ_NAMED_ATTRS, WRITE_DATA, EXECUTE, DELETE_CHILD, WRITE_NAMED_ATTRS, READ_ATTRIBUTES, READ_DATA, WRITE_OWNER, READ_ACL, DELETE, WRITE_ATTRIBUTES, APPEND_DATA, WRITE_ACL, SYNCHRONIZE]
HOME\zaikin[READ_NAMED_ATTRS, WRITE_DATA, EXECUTE, DELETE_CHILD, WRITE_NAMED_ATTRS, READ_ATTRIBUTES, READ_DATA, WRITE_OWNER, READ_ACL, DELETE, WRITE_ATTRIBUTES, APPEND_DATA, WRITE_ACL, SYNCHRONIZE]
BUILTIN\Users[READ_NAMED_ATTRS, READ_DATA, READ_ACL, EXECUTE, SYNCHRONIZE, READ_ATTRIBUTES]
					

Professional hosting         Free 'Oracle Certified Expert Web Services Developer 6' Guide     Free SCDJWS 5.0 Guide