Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to modify the permissions of Java files and folders

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report