0
if (!file.exists())
  file.mkdirs();
  file.createNewFile();

The error states that I have a problem with actually 'writing' Go Falcons to the file, it will state that the file access is denied, Does that mean something is wrong with my code?

Error states: FileNotFoundException Access is denied

PS: how do I actually read this file, once it's writable?

5
  • 1
    FileWrite extends Exception ?? Perhaps you need to start with something simpler than file IO... Commented Jun 30, 2014 at 3:53
  • Please always tag your question with the language you're using. I've added java this time. Commented Jun 30, 2014 at 3:53
  • Check access permission for this file. Also, take a look on related question stackoverflow.com/questions/4281143/…. Commented Jun 30, 2014 at 3:58
  • Very sorry for not tagging it correctly Commented Jun 30, 2014 at 4:00
  • @user3788930 A hint for future programs, is that instead of printing an exception with System.out.println(), it is better to run the printStackTrace() method on the exception object itself. This gives you much more information about how and why your program is failing. Commented Jun 30, 2014 at 4:16

4 Answers 4

1

If I understand your question, one approach would be to use a PrintWriter

public static void main(String[] args) {
  File outFile = new File(System.getenv("HOME") // <-- or "C:/" for Windows.
      + "/hello.txt");
  try {
    PrintWriter pw = new PrintWriter(outFile);
    pw.println("Go Falcons");
    pw.close();
  } catch (FileNotFoundException e) {
    e.printStackTrace();
  }
}

Which creates a single line ascii file in my home folder named "hello.txt".

$ cat hello.txt
Go Falcons
Sign up to request clarification or add additional context in comments.

Comments

1

Your problem is that right before you attempt to create your output file, you first create a directory with the same name:

            if (!file.exists())
                file.mkdirs();      // creates a new directory
            file.createNewFile();   // fails to create a new file

Once you've already created the directory, you can no longer create the file, and of course, you cannot open a directory and write data to it as if it were a file, so you get an access denied error.

Just delete the file.mkdirs(); line and your code will work fine:

        if (!file.exists())
            file.createNewFile();   // creates a new file

Comments

1

Change:

if (!file.exists())
  file.mkdirs();
  file.createNewFile();

To:

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

But first, go read some tutorials or articles. Why are you extending Exception?

3 Comments

My teacher said something about "Every program I do from now on in class needs exceptions" Am I doing it wrong?
You should double check what your teachers about their expectations first
@user3788930 Yes, you are doing it wrong. You teacher was referring to the fact that you need to handle exceptions (catch or re-throw them), not suggesting that all of your classes extend the Exception class. Look here to learn more about how to handle exceptions.
0

You can take the substring of the path till folder structure(excluding file name) and create directories using method mkdirs(). Then create file using createNewFile() method. After that write to newly created file. Don't forget to handle exceptions.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.