Java Programming Tutorials

Java programming tutorials with many code examples!

Java File Permissions

Problem:

How to read file permissions in Java? In this post we’ll show three ways to that, one for using java.io.File and two ways using fantastic Files from Java NIO.

The problem is simple: we want to find out permissions of a file or directory from Java. The following program does that in three, different ways:

  1. Using static methods from java.io.File – for pre Java 7 code.
  2. Using static methods from java.io.Files – for checking basic permissions since Java 7.
  3. Using file metadata – for extensive (e.g. POSIX) permissions since Java 7.

The first two methods just check what permissions does JVM have for specified files. The last one shows permissions for file owner, group, and others (just as you can see in your Linux)

Solution:

Let’s check from Java file permissions:

package com.farenda.java.io;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.PosixFileAttributes;
import java.nio.file.attribute.PosixFilePermissions;
import java.util.List;

import static java.util.Arrays.asList;

public class CheckFilePermissionsExample {

    public static void main(String[] args) throws IOException {
        List<String> names = asList("/tmp", "/root", "/bin/ls");

        System.out.println("Pre Java 7 way - File.canR/W/E");
        for (String name : names) {
            preJava7Way(new File(name));
        }

        System.out.println("\nJava 7 Files.isR/W/E");
        for (String name : names) {
            usingJava7Files(name);
        }

        System.out.println("\nJava 7 using attributes from metadata");
        for (String name : names) {
            usingJava7Metadata(name);
        }
    }

    private static void preJava7Way(File file) {
        // For legacy code:
        StringBuilder perms = new StringBuilder();
        perms.append(file.canRead() ? 'r' : '-');
        perms.append(file.canWrite() ? 'w' : '-');
        perms.append(file.canExecute() ? 'x' : '-');
        System.out.printf("Permissions of %s: %s%n",
                file.getAbsolutePath(), perms);
    }

    private static void usingJava7Files(String name) {
        // Simple permissions using Files from Java 7:
        Path path = Paths.get(name);
        StringBuilder perms = new StringBuilder();
        perms.append(Files.isReadable(path) ? 'r' : '-');
        perms.append(Files.isWritable(path) ? 'w' : '-');
        perms.append(Files.isExecutable(path) ? 'x' : '-');
        System.out.printf("Permissions of %s: %s%n",
                path.toString(), perms);
    }

    private static void usingJava7Metadata(String name) throws IOException {
        Path path = Paths.get(name);
        // For DOS/Windows use DosFileAttributes.class:
        PosixFileAttributes attrs = Files.readAttributes(
                path, PosixFileAttributes.class);
        // convert objects to 'rwx' style strings:
        String asString = PosixFilePermissions.toString(attrs.permissions());
        System.out.printf("Posix permissions of %s: %s%n", name, asString);
    }
}

And here’s the result of running the above program with sample directories on my Linux laptop:

Pre Java 7 way - File.canR/W/E
Permissions of /tmp: rwx
Permissions of /root: ---
Permissions of /bin/ls: r-x

Java 7 Files.isR/W/E
Permissions of /tmp: rwx
Permissions of /root: ---
Permissions of /bin/ls: r-x

Java 7 using attributes from metadata
Posix permissions of /tmp: rwxrwxrwx
Posix permissions of /root: rwx------
Posix permissions of /bin/ls: rwxr-xr-x

In short, since Java 7 working with files is easier and much more fun! :-)

Share with the World!