java.io.IOException: Permission denied in Java

2019-05-21 05:03发布

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.

2条回答
淡お忘
2楼-- · 2019-05-21 05:34

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);

查看更多
Animai°情兽
3楼-- · 2019-05-21 05:44

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");
查看更多
登录 后发表回答