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?

  • 1
    `FileWrite extends Exception` ?? Perhaps you need to start with something simpler than file IO... – John3136 Jun 30 '14 at 03:53
  • Please always tag your question with the language you're using. I've added `java` this time. – Blorgbeard Jun 30 '14 at 03:53
  • Check access permission for this file. Also, take a look on related question http://stackoverflow.com/questions/4281143/java-io-filenotfoundexception-access-is-denied. – Mihai8 Jun 30 '14 at 03:58
  • Very sorry for not tagging it correctly – user3788930 Jun 30 '14 at 04: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. – azurefrog Jun 30 '14 at 04:16

4 Answers4

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?

jotik
  • 17,044
  • 13
  • 58
  • 123
  • My teacher said something about "Every program I do from now on in class needs exceptions" Am I doing it wrong? – user3788930 Jun 30 '14 at 04:07
  • You should double check what your teachers about their expectations first – Ryan Fung Jun 30 '14 at 04:09
  • @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](http://docs.oracle.com/javase/tutorial/essential/exceptions/) to learn more about how to handle exceptions. – azurefrog Jun 30 '14 at 04:13
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
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
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
azurefrog
  • 10,785
  • 7
  • 42
  • 56
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.

Dhan
  • 42
  • 2