java.io.IOException:Java 中的权限被拒绝

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22679052/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 17:14:52  来源:igfitidea点击:

java.io.IOException: Permission denied in Java

javafilepermissionsfile-permissionsuser-permissions

提问by Krishna

I am trying to create a file into the same folder in my project, but I am not able to create that file dynamically. I am trying this:

我正在尝试在我的项目的同一文件夹中创建一个文件,但我无法动态创建该文件。我正在尝试这个:

try {
            System.out.println("path"+System.getProperty("user.dir"));
            File file = new File("/textfile.txt");
            file.createNewFile();
            //file.createNewFile();
        } catch (Exception e) {
            e.printStackTrace();
        }

What I am getting error is this:

我得到的错误是这样的:

java.io.IOException: Permission denied

Any suggestion will be welcomed.

任何建议将受到欢迎。

采纳答案by codeMan

To create a File into the same folder in your project, your path has to be relative.

要在项目的同一文件夹中创建文件,您的路径必须是相对的。

The path that you are giving is absolute, because it is starting from /. For your path to be relative, remove /from the path and try this :

您提供的路径是绝对的,因为它从/. 为了您的路径是相对的,请/从路径中删除并尝试以下操作:

File file = new File("textfile.txt");

回答by Harsh Parikh

What you can do is to create a variable string, store the file name, and pass that string into the File file=new File(string);

您可以做的是创建一个变量字符串,存储文件名,然后将该字符串传递给 File file=new File(string);