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");
Share:
33,768
Kishan Bheemajiyani
Author by

Kishan Bheemajiyani

information about me

Updated on August 16, 2020

Comments

  • Kishan Bheemajiyani
    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
    Kishan Bheemajiyani over 10 years
    i tried its working . :D but why its giving that time exception?
  • codeMan
    codeMan over 10 years
    you 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
    Kishan Bheemajiyani over 10 years
    but if i do like this '/' so that means i am trying to do in root directory is it?
  • codeMan
    codeMan over 10 years
    Yes, refer this : linuxnix.com/2012/07/…
  • Kishan Bheemajiyani
    Kishan Bheemajiyani over 10 years
    ya 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
    codeMan over 10 years
    This is fit to be a comment, than an answer.
  • Kishan Bheemajiyani
    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
    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
    Kishan Bheemajiyani over 10 years
    @codeMan Yes bro its just good that he tried :) and ya your ans was Solving problem that is true :)