2
votes

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.

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

4 Answers

3
votes

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.

1
votes

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

}
0
votes

if call mkdir, it will create a directory with that name (in that case, "data.txt" will be a directory).

the AccessDenied exception is caused by the fact that you cannot write data into a directory.

you COULD check that directory you want to write to exits:

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

slightly newer is:

File.mkdirs()

which will create multiple directories if needed.

0
votes

The better way at first to check if folder and file is exist, and than create folders and file.

    if (!file.getParentFile().exists()) {
        file.getParentFile().mkdirs();
        file.createNewFile();
    }

also mkdirs() can't throw exception so you can do like this:

    if (!file.getParentFile().exists()) {
        if (file.getParentFile().mkdirs()) {
            file.createNewFile();
        } else {
            throw new IOException("Failed to create directory " + file.getParent());
        }
    }