java.io.IOException: Permission denied in Java
33,768
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");
![Kishan Bheemajiyani](https://i.stack.imgur.com/JVRqD.jpg?s=256&g=1)
Comments
-
Kishan Bheemajiyani almost 4 years
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.
-
Kishan Bheemajiyani over 10 yearsi tried its working . :D but why its giving that time exception?
-
codeMan over 10 yearsyou were trying to create a file in the root / directory of your linux computer and you may not be executing the java program as a root user, hence the exception.
-
Kishan Bheemajiyani over 10 yearsbut if i do like this '/' so that means i am trying to do in root directory is it?
-
codeMan over 10 yearsYes, refer this : linuxnix.com/2012/07/…
-
Kishan Bheemajiyani over 10 yearsya that is right too. i can do that but thing is that for the absolute path was saying :) it was onto the root directory prob.
-
codeMan over 10 yearsThis is fit to be a comment, than an answer.
-
Kishan Bheemajiyani over 10 years@codeMan its fine i accept your ans okey now dont be more argumentative and ya he is also right about this.
-
codeMan over 10 years@Krishna I have no intention of arguing or offending, It was just a comment. I completely agree that there is nothing wrong with Harsh Parikh's answer. but I dont see it solving the problem.
-
Kishan Bheemajiyani over 10 years@codeMan Yes bro its just good that he tried :) and ya your ans was Solving problem that is true :)