权限被拒绝:Java中的文件创建 [英] Permission Denied: File Creation in Java

查看:146
本文介绍了权限被拒绝:Java中的文件创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Mac在Eclipse中编译以下代码后:

After compiling the following code in Eclipse using a Mac:

    import java.io.*;

    public class Filer{
        public static void main(String[] args) throws IOException{

        File f1;

        f1 = new File("/System/file.txt");

        if(!f1.exists()){
            f1.createNewFile();

        }
      }
    }

我收到一个错误:

    Exception in thread "main" java.io.IOException: Permission denied
        at java.io.UnixFileSystem.createFileExclusively(Native Method)
        at java.io.File.createNewFile(File.java:883)
        at Filer.main(Filer.java:11)

有人可以告诉我为什么吗?有什么办法可以更改权限?如果我将其编译为.jar文件并将其发送给某人,那么该人是否具有正确的权限?

Can anyone tell me why that is? Is there any way to change the permissions? And if I were to compile this as a .jar file and send it to someone, would that person have the correct permissions?

推荐答案

有人可以告诉我为什么吗?

Can anyone tell me why that is?

您的用户无权在该目录中创建文件.

Your user doesn't have permission to create a file in that directory.

是否可以更改权限?

Is there any way to change the permissions?

与更改任何目录的权限的方式相同.

The same way you would change the permissions of any directory.

在Java 7+中

Files.setPosixFilePermisions(f1.toPath(), 
    EnumSet.of(OWNER_READ, OWNER_WRITE, OWNER_EXECUTE, GROUP_READ, GROUP_EXECUTE));

如果我将其编译为.jar文件并将其发送给某人,那么该人是否具有正确的权限?

And if I were to compile this as a .jar file and send it to someone, would that person have the correct permissions?

我怀疑名为/System的目录的正确权限是您没有写访问权限.

I suspect the correct permissions for a directory called /System is that you NOT have write access.

有什么理由不使用主目录或当前工作目录吗?

Is there any reason not to use the home directory or the current working directory?

这篇关于权限被拒绝:Java中的文件创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆