2

I'm trying to empty a jasper file. Before write, I checked it for write access and I t gives me that I can write on it. But it terminates with java.io.FileNotFoundException (Access is denied) - file.canWrite() What am I missing?

        try {
                File f = new File("C:\\Program Files (x86)\\XXXXXX\\XXX\\X\\X.jrxml");
                if(f.canWrite()){
                    BufferedWriter bf = new BufferedWriter(new FileWriter(f));
                    bf.write("");
                    bf.close();
                }

    } catch (Exception e) {
        e.printStackTrace();
    }
4
  • Do you have permission to write to that directory? Commented Feb 10, 2015 at 15:38
  • yes i have permissions. Commented Feb 10, 2015 at 15:42
  • Have you cross checked if some other process is still using that file? That can be a possible cause. Commented Feb 10, 2015 at 15:44
  • And also, are you running on Windows 7? Commented Feb 10, 2015 at 15:52

2 Answers 2

1

Writing in your Program Files folder often has quite restricted access, and you must be on an Administrator account to do so usually. I would suggest you try saving your .jrxml in a more accessible directory outside \\Program Files (x86) like your ApplicationData folder, it may even already be there.

You can also try something like Isolated Storage to ensure you have permissions, this should show you how to use it.

Sign up to request clarification or add additional context in comments.

Comments

1

Apparently, canWrite may return true even if you cannot write on windows (according to this : http://bugs.java.com/bugdatabase/view_bug.do?bug_id=8060110). The javadoc is not really clear about that.

You can try using Files.isWritable if you are using at least java 7 to see if you have the expected result. Otherwise, it seems that the only way to check the rights is to try writing and see what happens.

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.