[Java] How to get the authority of the folder

Programming study diary

January 4, 2021 When you access a file, you may not be able to implement the program because you do not have permission for the file. Folders also need to get permissions, so here's a quick summary.

How to get folder permissions in java.io

java.io is an old API. Folder permissions are obtained with the canWtrite method and isHidden method of the java.io.File class.

import java.io.File;

public class Sample {
    public static void main(String[] args) {
        File file = new File("c:\\test");
 
        if (file.canWrite()) {
            System.out.println("Can write");
        } else {
            System.out.println("Read-only");
        }
 
        if (file.isHidden()) {
            System.out.println("It's a hidden file");
        } else {
            System.out.println("Not a hidden file");
        }
    }
}

How to get folder permissions in java.nio

java.nio is a new API with improved functionality from Java 7. Folder permissions are obtained with the getAttribute method of the java.nio.Files class.

Sample code


import java.nio.Files;

public class Sample {
    public static void main(String[] args) {
        Path path = Paths.get("c:\\test");
 
        //"True" for read-only
        System.out.println(Files.getAttribute(path, "dos:readonly"));
 
        //“True” for hidden files
        System.out.println(Files.getAttribute(path, "dos:hidden"));

    }
}

References

[Introduction to Java] How to create a folder (java.nio.file)

Recommended Posts

[Java] How to get the authority of the folder
[Java] How to get the URL of the transition source
[Java] How to get the maximum value of HashMap
[Java] How to get the current directory
How to get the date in java
How to get the length of an audio file in java
How to get today's day of the week
[Java] How to get the redirected final URL
How to get the absolute path of a directory running in Java
How to write Scala from the perspective of Java
As of April 2018 How to get Java 8 on Mac
[Android] How to get the setting language of the terminal
[Swift] How to get the document ID of Firebase
[Java] How to easily get the longest character string of ArrayList using stream
[Java] How to create a folder
How to get the class name / method name running in Java
An unsupported Java version How to get rid of errors
Get to the abbreviations from 5 examples of iterating Java lists
How to get the longest information from Twitter as of 12/12/2016
How to derive the last day of the month in Java
[jsoup] How to get the full amount of a document
How to get the class name of the argument of LoggerFactory.getLogger when using SLF4J in Java
Get the result of POST in Java
[Java] How to use the File class
[Java] How to use the hasNext function
[Java] Get the day of the specific day of the week
[java] Summary of how to handle char
[Java] How to use the HashMap class
[Java] How to use the toString () method
Studying how to use the constructor (java)
[Processing × Java] How to use the loop
How to determine the number of parallels
[Java] How to set the Date time to 00:00:00
[Java] [Maven3] Summary of how to use Maven3
[Processing × Java] How to use the class
How to install the legacy version [Java]
Output of the book "Introduction to Java"
[Processing × Java] How to use the function
[Java] How to use the Calendar class
[Rails] How to get the URL of the transition source and redirect
How to get the contents of Map using for statement Memorandum
How to get the id of PRIMAY KEY auto_incremented in MyBatis
[Java] How to convert from String to Path type and get the path
How to check for the contents of a java fixed-length string
How to increment the value of Map in one line in Java
[java] Summary of how to handle character strings
[Java] How to use Thread.sleep to pause the program
Customize how to divide the contents of Recyclerview
[Java] How to get and output standard input
[Java] Summary of how to abbreviate lambda expressions
[Java] Get the length of the surrogate pair string
[Java] How to get the current date and time and specify the display format
How to get Class from Element in Java
Output of how to use the slice method
[Java] (for MacOS) How to set the classpath
[Java] [ibatis] How to get records of 1-to-N relationship with List <Map <>>
How to find out the Java version of a compiled class file
[Java] How to get random numbers excluding specific numbers
How to use the replace () method (Java Silver)
How to get and study java SE8 Gold
How to get the value after "_" in Windows batch like Java -version