Get the App
SLTechnology News&Howtos  ›  Development  › 

How to modify the permissions of Java files and folders

Shulou Source: shulou.com Published: 2022-06-03 03:45:31 10月25日 Update

This article mainly shows you the "Java file, folder permissions how to modify", the content is easy to understand, clear, hope to help you solve doubts, the following let the editor lead you to study and learn "Java files, folders permissions how to modify" this article.

Use the File class

This is the most common method in the past, but it has some drawbacks in LINUX or UNIX systems, where it is not so easy to display a specified permission of 440770.

File dirFile = new File (dirPath); dirFile.setReadable (true, false); dirFile.setExecutable (true, false); dirFile.setWritable (true, false)

So we usually use some workaround to modify folder permissions. I need to set the permissions to 770 on LINUX.

Runtime runtime = getRuntime (); String command = "chmod 770" + dirPath;try {Process process = runtime.exec (command); process.waitFor (); int existValue = process.exitValue (); if (existValue! = 0) {logger.log (Level.SEVERE, "Change file permission failed.");} catch (Exception e) {logger.log (Level.SEVERE, "Command execute failed.", e);}

There is a problem with this approach. When dirPath contains spaces or semicolons, it not only has an impact on functionality, but also has a hidden danger to security.

Case 1: dirPath = / home/an aa.txt

The command executed in the LINUX system is chmod 770 / home/an aa.txt, and the system will think that the file permissions of modify / home/an and aa.txt are 770. the modification of file permissions failed.

Case 2: when dirPath = / home/aaa.txt;rm test.txt

At this point, two instructions are executed on the LINUX system:

Chmod 770 / home/omc/aaa.txtrm test.txt

At this point, there will be security risks.

NIO mode

Private void changeFolderPermission (File dirFile) throws IOException {Set perms = new HashSet (); perms.add (PosixFilePermission.OWNER_READ); perms.add (PosixFilePermission.OWNER_WRITE); perms.add (PosixFilePermission.OWNER_EXECUTE); perms.add (PosixFilePermission.GROUP_READ); perms.add (PosixFilePermission.GROUP_WRITE); perms.add (PosixFilePermission.GROUP_EXECUTE); try {Path path = Paths.get (dirFile.getAbsolutePath ()); Files.setPosixFilePermissions (path, perms) } catch (Exception e) {logger.log (Level.SEVERE, "Change folder" + dirFile.getAbsolutePath () + "permission failed.", e);}}

From the API query, we know that this approach of NIO natively supports LINUX and UNIX low-level systems, but the test found that there seems to be no effect under the Windows system without distinguishing between the file owner and others, which is consistent with the practical File. From the underlying code, I found that the File class is still used.

In addition, UnsupportedOperationException IOException SecurityException may be thrown

The above is all the contents of the article "how to modify the permissions of Java files and folders". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Files permissions methods systems folders contents articles security situations pitfalls learning help practicality consistency not only code semicolons functions commands frequent Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Redmi OPPO Reno Xiaomi Docker