Java : Creating Files, but Access Denied

12,340

Solution 1

You don't need the following piece of code which is trying to create a directory with the file name. When you call FileWriter with "C:\Users\cvi7\Documents\data.txt", you get an "Access denied" error because a folder with the name "C:\Users\cvi7\Documents\data.txt" exists and hence you can't create a file with that name.

if(!file.exists()) {
    file.mkdir();
}

You only need it if you want to create the directory "C:\Users\cvi7\Documents". However, if it a fixed directory, you would rather create it yourself first rather than having the program create it.

Java FileWriter will create the file if it doesn't exist.

Solution 2

You are creating a directory, not a file. So you are unable to write that file because its not exist. First delete data.txt directory from Documents directory and use the following code:

private static void setup() throws IOException {

    String username = System.getProperty("user.name");
    System.out.println("username: "+username);
    File file = new File("C:\\Users\\" + username + "\\Documents\\", "data.txt");
    if(!file.exists()) {
        System.out.println("creating file");
        if(file.createNewFile()) {
        System.out.println("Succesfully created file");
        } else{
        System.out.println("Failed to create file");
        }
    }
    BufferedWriter out = new BufferedWriter(new FileWriter(file));
    out.write("Bacon");
    out.close();

}
Share:
12,340
Connor
Author by

Connor

Updated on July 12, 2022

Comments

  • Connor
    Connor almost 2 years

    Yes, I did attempt googling and searching through StackOverflow, however, I was unable to come up with an answer, as I am still new to Java.

    Currently I am attempting to write data to a file, all I have as of now is:

    private static void setup() throws IOException {
    
        String username = System.getProperty("user.name");
    
        File file = new File("C:\\Users\\" + username + "\\Documents\\", "data.txt");
        if(!file.exists()) {
            file.mkdir();
        }
        BufferedWriter out = new BufferedWriter(new FileWriter(file));
        out.write("Bacon");
        out.close();
    
    }
    

    However upon running the program, I receive an error regarding File Permissions, reading: Exception in thread "main" java.io.FileNotFoundException: C:\Users\cvi7\Documents\data.txt (Access is denied)

    Now I see, java.io.FileNotFoundException, however, how would I create the file if the new File() doesn't? I am assuming it probably has to do with permissions however in the case my brain had a fail, I would hope to hear about this too.

    • Code-Apprentice
      Code-Apprentice over 6 years
      Can you create files in that directory manually, say with NotePad or Word?
    • Connor
      Connor over 6 years
      @Code-Apprentice yes, however, it returns the same errors.
    • Code-Apprentice
      Code-Apprentice over 6 years
      I am confused...can you create files or do you get errors? It can't be both.
    • Connor
      Connor over 6 years
      Well, I can manually create "data.txt", however with and without the file being created, the same FileNotFound error is given.
    • Code-Apprentice
      Code-Apprentice over 6 years
      It looks like you create a directory named "data.txt". Is that really what you want?