8

I'm having an issue getting this to work. It takes in a string which consists of several pieces of information put together. However, when I try to write the String to a file in order to track changes in the program over time, I receive an access is denied error:

 void writeToFile(String input) throws Exception{
            File file = new File("C:\\WeatherExports\\export.txt");
            if(!file.exists()){
                    file.createNewFile();
            }
            BufferedWriter inFile = new BufferedWriter(new FileWriter(file,true));
            try{
                    inFile.append(input);
                    inFile.newLine();
            } catch(Exception e){
                    e.printStackTrace();
            }
            inFile.close();
    }

STACKTRACE YEILDS:

java.io.FileNotFoundException: C:\WeatherExports\export.txt (Access is denied)

Full Stacktrace:

java.io.FileNotFoundException: C:\WeatherExports\export.txt (Access is denied)
at java.io.FileOutputStream.openAppend(Native Method)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileWriter.<init>(Unknown Source)
at org.weatheralert.InfoManipMethods.writeToFile(InfoManipMethods.java:58)
at org.weatheralert.Form.actionPerformed(Form.java:108)
at javax.swing.JTextField.fireActionPerformed(Unknown Source)
at javax.swing.JTextField.postActionEvent(Unknown Source)
at javax.swing.JTextField$NotifyAction.actionPerformed(Unknown Source)
at javax.swing.SwingUtilities.notifyAction(Unknown Source)
at javax.swing.JComponent.processKeyBinding(Unknown Source)
at javax.swing.JComponent.processKeyBindings(Unknown Source)
at javax.swing.JComponent.processKeyEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

Line 58:

BufferedWriter inFile = new BufferedWriter(new FileWriter(file,true));
user207421
  • 305,947
  • 44
  • 307
  • 483
Nick
  • 191
  • 1
  • 2
  • 9
  • 1
    Does the user you're running the program as have permissions to create and write files to that directory? – NG. Sep 02 '13 at 23:58
  • It's my own account which is an admin on my computer. I have UAC turned off for myself and one other user on the computer. – Nick Sep 03 '13 at 00:06
  • I can successfully create the file if I don't add directories and place the file directly inside disk C: – Nick Sep 03 '13 at 00:07
  • Does the directory exist? Do you have write permissions to create the directory and write the file? – MadProgrammer Sep 03 '13 at 00:15
  • does the WeatherExports folder already exist on teh C: drive? – Sean F Sep 03 '13 at 00:17
  • The directory doesn't exist. I'm trying to create it. Originally I'd had: file.mkdirs(); file.createNewFile(); – Nick Sep 03 '13 at 00:31
  • BufferedWriter doesn't appear in the stack trace and therefore has nothing to do with it. – user207421 Sep 03 '13 at 00:44
  • Too bad that's the line that the error was being thrown on, sir. – Nick Sep 03 '13 at 00:50
  • Too bad that `BufferedWriter` doesn't appear in the stack trace. What's your explanation for that? Too bad that the line you mention also contains `new FileWriter(),` which *does* appear in the stack trace. Too bad you didn't have a proper look at your own evidence before posting. Or commenting. – user207421 Sep 03 '13 at 05:23
  • http://stackoverflow.com/a/23697734/715269 – Gangnus May 16 '14 at 14:25

3 Answers3

14

You have to have folders created first. But you can't call file.mkdirs() - you need to call file.getParentFile().mkdirs() - otherwise, you will create a folder with the name of the file (which will then prevent you from creating a file with the same name).

I'll also mention that you should check the result code of mkdirs(), just in case it fails.

And though you didn't ask for it, I'll still mention that you don't need to call createNewFile() (your FileWriter will create it).

and, just for thoroughness, be sure to put your file.close() in a finally block, and throw your exception (don't just print it) - here you go:

 void writeToFile(String input) throws IOException{
            File file = new File("C:\\WeatherExports\\export.txt");
            if (!file.getParentFile().mkdirs())
                    throw new IOException("Unable to create " + file.getParentFile());
            BufferedWriter out = new BufferedWriter(new FileWriter(file,true));
            try{
                    out.append(input);
                    out.newLine();
            } finally {
                    out.close();
            } 
    }
Kevin Day
  • 16,067
  • 8
  • 44
  • 68
6

There's another possibility (just for anyone who may be reading this after the fact). I had the same problem, but all the parent folders existed. The problem turned out to be that there was a folder with the same name as the file I was trying to create.

k_g
  • 4,333
  • 2
  • 25
  • 40
0

On my case I was passing the directory where I should put the file I am generating. I just appended the Filename to the directory and mine worked fine.

Leroy
  • 352
  • 2
  • 11