英文:
Change permission for all files inside a directory using JAVA
问题
我想写一段代码来将目录中的所有文件设置为可写(非只读)。我的程序使用JAVA编写,应在Windows和Linux上均可运行。到目前为止,我的方法是遍历目录中包含的所有文件,并针对每个文件将权限设置为可写,使用以下方法:
static void makeDosFileWritable(Path pathToFile) {
try {
Files.setAttribute(pathToFile.toAbsolutePath(), "dos:readonly", false);
//日志记录
} catch (IOException e) {
//日志记录
}
}
static void makeUnixFileWritable(Path pathToFile) {
try {
Set<PosixFilePermission> ownerWritable = PosixFilePermissions.fromString("rw-rw-rw-");
Files.setPosixFilePermissions(pathToFile, ownerWritable);
//日志记录
} catch (IOException e) {
//日志记录
}
}
这个方法很好用。问题是,在顶层目录中,我也可能有嵌套的目录。我想知道如何处理这种情况。
- 我应该递归地遍历每个(子)目录并为每个文件更改权限吗?
- 还是,是否有机会以编程方式(使用JAVA)仅在顶级目录级别更改权限,使设置自动适用于目录及其子目录中包含的所有文件?
英文:
I'd like to write a piece of code in order to set all files inside a directory to writable (no read-only).
My program is in JAVA and it should work both on Windows and Linux. So far my approach was to iterate over all the files contained in the directory and for each of them, set the permission to writable, using these methods:
static void makeDosFileWritable(Path pathToFile) {
try {
Files.setAttribute(pathToFile.toAbsolutePath(), "dos:readonly", false);
//logs
} catch (IOException e) {
//logs
}
}
static void makeUnixFileWritable(Path pathToFile) {
try {
Set<PosixFilePermission> ownerWritable = PosixFilePermissions.fromString("rw-rw-rw-");
Files.setPosixFilePermissions(pathToFile, ownerWritable);
//logs
} catch (IOException e) {
//logs
}
}
This works fine. The problem is, inside the top directory, I could also have nested directory. I was wondering how to handle this.
- Should I go recursively through each (sub)directories and change the permission for each file?
- or, is there by chance a way to change programmatically (JAVA) the permission only at the level of the top directory and that the setting automatically apply to all files contained in the directory and its subdirectories?
答案1
得分: 2
你可以参考这个 GitHub 仓库 - 它包含了对于 Windows、*Nix 以及甚至 MacOs 权限更改的实现。相关的代码可以在这里找到。
尽管代码可能有些旧,但是这些实现仍然应该能够工作。
英文:
You can refer this Github repo - it has an implementation for changing permissions for Windows, *Nix and even MacOs. Relevant code can be found here.
The code may be older, however, the implementations should still work.
答案2
得分: 1
在Java中,如果您不想生成操作系统实用程序来执行此操作,则需要在代码中处理目录递归。实现一个函数,该函数以表示目录的File
作为其参数。使用诸如File.listFiles()
的方法展开目录中的文件,然后对每个文件进行测试,看它是否是目录,可以使用File.isDirectory()
来判断。如果它是一个目录,则使用新目录递归调用自身。
有许多需要注意的地方 - 例如,如果在列表中出现“..”目录,则不希望明确展开它。您可能需要检查循环链接。您可能希望将扩展限制为特定深度、特定设备等等。
编写这样的扩展功能需要相当数量的代码,以便以一种有帮助且稳健的方式进行扩展。但是,如果您组织有序,您只需要编写一次即可。
英文:
In Java, if you don't want to spawn an operating system utility to do it, you'll need to handle the directory recursion in your code. Implement a function that takes as its argument a File
that represents a directory. Expand the files in the directory using, for example, File.listFiles()
, then test each for whether it's a directory using File.isDirectory()
. If it's a directory, call yourself recursively with the new directory.
There are lots of things to watch out for -- you don't want to expand the ".." directory explicitly if it appears in the list, for example. You might need to check for circular links. You might want to restrict the expansion to a particular depth, or a particular device, and so on.
It needs a fair amount of code to do this expansion in a helpful and robust way but, if you're well-organized, you'll only need to write it once.
答案3
得分: 0
这是我能想到的。您可以通过调用 Runtime.getRuntime().exec(//允许用户更改文件夹权限的操作系统命令)
来使用命令行界面(CLI)。这应该是最简单的方法。
此外,如果您熟悉C或C++并且使用Windows操作系统,您可以查看 JNA 库,以访问本机Windows函数。
英文:
This is what t I can think of. You can use CLI instead by calling Runtime.getRuntime().exec(//The command of your OS that allows user to change folder permission)
. This should be the simplest way.
Besides, if you are familiar with C or C++ and using Windows, you can take a look at JNA library to access native Windows function.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论