How to check file permissions in Java (OS independent)

I have the following code snippet:

public class ExampleClass {

public static void main(String[] args) throws FileNotFoundException {
    String filePath = args[0];
    File file = new File(filePath);

    if (!file.exists())
        throw new FileNotFoundException();

    if (file.canWrite())
        System.out.println(file.getAbsolutePath() + ": CAN WRITE!!!");
    else
        System.out.println(file.getAbsolutePath() + ": CANNOT WRITE!!!!!");

    if (file.canRead())
        System.out.println(file.getAbsolutePath() + ": CAN READ!!!");
    else
        System.out.println(file.getAbsolutePath() + ": CANNOT READ!!!!!");

    if (file.canExecute())
        System.out.println(file.getAbsolutePath() + ": CAN EXECUTE!!!");
    else
        System.out.println(file.getAbsolutePath() + ": CANNOT EXECUTE!!!!!");
}
}

It works on Linux, but the problem is that it does not work on Windows 7. So, the question is: Does anyone know the method of checking file permissions in the Java OS INDEPENDENT?

+5
source share
3 answers

This may be caused by something (for example, an antivirus product) "intermediary" file access inconsistently.

Of course, it's hard to believe that Java methods are File.canXxxx()usually broken for every taste of Windows.


. . Sun... . , Windows, Sun . ( API Java 7 ...)

FWIW, , , . , . . https://stackoverflow.com/a/6093037/139985 . ( ...)

+5

, Java . , , , - .

-, , Windows (Property... - Read only/Hide/Archive ..). , , , . , //etc , . (, setReadable() false, , ). , execute a txt , , bat , , , , bat. , , bat . Windows, JVM, , , JVM. , .

, , Java 7. , , Java -, . jdk 1.7.0_19, :

  • Windows Read Only Hidden.

  • Java, ( Stephen C , setXxxxx() ).

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    
    public class FilePermissionTester {
        public static void main( String[] args ) throws IOException {
            File file = new File("Y:\\some\\remote\\drive\\directoy\\xxxxx.txt");
            System.out.println( "exists:"  + file.exists() );
            System.out.println( "is file:"  + file.isFile() );
            System.out.println( "can read:" + file.canRead() );
            System.out.println( "can execute:" + file.canExecute() );
            System.out.println( "can write:" + file.canWrite() );
            System.out.println( "is hidden:" + file.isHidden() );
    
            System.out.println("change it to be unreadable, and it works? " + file.setReadable(false));
            System.out.println( "can read:" + file.canRead() );
            System.out.println("change it to be writable, and it works? " + file.setWritable(true));
            System.out.println( "can write:" + file.canWrite() );
            FileInputStream fileInputStream = new FileInputStream(file);
            fileInputStream.read();
            fileInputStream.close();
    
    
        }
    
    }
    

:

exists:true
is file:true
can read:true
can execute:true
can write:false
is hidden:true
change it to be unreadable, and it works? false
can read:true
change it to be writable, and it works? true
can write:true

, . Save As.. .

, , setReadable(false) false, . JavaDoc , setReadable() return false, readable false, . Java API , native . , - .

, , java.util.File, setHidden(). , pacakges java.security, AccessController?

+2

I did some tests in the NIO API (from Java 7) and they seem to work just fine.

import java.io.FileNotFoundException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class PermissionCheck {

    public static void main(String[] args) throws FileNotFoundException {

        String filePath = args[0];
        Path p = Paths.get(filePath);

        if (Files.notExists(p))
            throw new FileNotFoundException();

        if (Files.isWritable(p))
            ...

        if (Files.isReadable(p))
            ...

        if (Files.isExecutable(p))
            ...
    }
}

JDKs: 1.7.0_25, 1.8.0_91

OS: Windows 7, 8 (64 bit)

0
source

All Articles