Results 1 to 6 of 6
  1. #1
    adwodon is offline Member
    Join Date
    Dec 2010
    Posts
    10
    Rep Power
    0

    Default Access is denied when trying to create a file.

    This is something I think has happened to me before when I was learning Java however I didnt take notice of it as it worked on my uni computer but on my home computer running windows 7 I get the following error message when trying to make a file:

    java.io.FileNotFoundException: C:\Users\James\Desktop\Measurements\1Mar (Access is denied)
    Failed to make JPEG
    at java.io.FileOutputStream.open(Native Method)
    at java.io.FileOutputStream.<init>(Unknown Source)
    at java.io.FileOutputStream.<init>(Unknown Source)
    at org.jfree.chart.ChartUtilities.saveChartAsJPEG(Cha rtUtilities.java:520)
    at org.jfree.chart.ChartUtilities.saveChartAsJPEG(Cha rtUtilities.java:474)
    at dataProcessing.Analysis.plotMap(Analysis.java:118)
    at dataProcessing.Analysis.main(Analysis.java:142)
    The file im making is a JPEG which is created from the JFreeChart class:

    Java Code:
    public static void saveChartAsJPEG(File file, JFreeChart chart,
                int width, int height, ChartRenderingInfo info) throws IOException {
    
            if (file == null) {
                throw new IllegalArgumentException("Null 'file' argument.");
            }
            if (chart == null) {
                throw new IllegalArgumentException("Null 'chart' argument.");
            }
            OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
            try {
                writeChartAsJPEG(out, chart, width, height, info);
            }
            finally {
                out.close();
            }
    
        }
    The class I made to run it is (I very much doubt the problem is here either):

    Java Code:
    public static void plotMap(Map<String, DataSet> dataMap, String title, String saveLocation){
    		Iterator<String> it = dataMap.keySet().iterator();
    		DefaultCategoryDataset dataSet = new DefaultCategoryDataset();
    		while(it.hasNext()){
    			String key = it.next().toString();
    			DataSet data = dataMap.get(key);
    			dataSet.addValue(data.avgRSSI(), "test", key);
    		}
    		JFreeChart chart=createChart(dataSet, title);
    		File image = new File(saveLocation);
    		try {
    			ChartUtilities.saveChartAsJPEG(image, chart, 500, 300);
    		} catch (IOException e) {
    			e.printStackTrace();
    			System.out.println("Failed to make JPEG");
    		}
    		
    	}
    Has anyone experienced this?
    Is there any way around it?

    I need to make a tonne of graphs for a mountain of data I have to sift through for a 3rd year physics group project and I fear I've hit a brick wall here :confused:

    If there isn't a way around it can anyone recommend another way of getting the graphs done?

  2. #2
    adwodon is offline Member
    Join Date
    Dec 2010
    Posts
    10
    Rep Power
    0

    Default

    I ran a test class and this has nothing to do with my code, I can't write a file and thats it.

    Java Code:
    import java.io.BufferedWriter;
    import java.io.FileWriter;
    import java.io.File;
    import java.io.Writer;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    
    public class TestClass2 {
        public static void main(String[] args) {
            Writer writer = null;
    
            try {
                String text = "This is a text file";
    
                File file = new File("C:\\write.txt");
                writer = new BufferedWriter(new FileWriter(file));
                writer.write(text);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (writer != null) {
                        writer.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    This gives the exact same error as before:

    java.io.FileNotFoundException: C:\write.txt (Access is denied)
    at java.io.FileOutputStream.open(Native Method)
    at java.io.FileOutputStream.<init>(Unknown Source)
    at java.io.FileOutputStream.<init>(Unknown Source)
    at java.io.FileWriter.<init>(Unknown Source)
    at dataProcessing.TestClass2.main(TestClass2.java:18)
    I'm running windows 7, I haven't done anything out of the ordinary or knowingly done anything that would affect this.

    Does anyone have any idea why it wont write files to my computer?
    Surely this isnt unheard of?

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,368
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by adwodon View Post
    Does anyone have any idea why it wont write files to my computer?
    Surely this isnt unheard of?
    Check your account information whether or not you have write permissions anywhere on the file system. This is not a Java problem. e.g. it would be if you could write a file with, say, a text editor or similar.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    adwodon is offline Member
    Join Date
    Dec 2010
    Posts
    10
    Rep Power
    0

    Default

    Its my personal computer and I am the only user, I have 'full control' in the permissions for all folders, I can do what I want when not using Java.

    Thats what I don't understand, its my computer, why cant eclipse write files to it?

    I could make the file myself in that folder, but I cant do it in Java.

    I ran this:

    Java Code:
    public static void main(String[] args) {
        	try {
        	    AccessController.checkPermission(new FilePermission("C:\\Users\\James\\Desktop\\Measurements\\", "read,write"));
        	    System.out.println("success?");
        	    // Has permission
        	} catch (SecurityException e) {
        		System.out.println("failure :(");
        	    // Does not have permission
        	}
    And it fails yet as you can see I have the permissions to do things in the folder:
    Last edited by adwodon; 03-18-2011 at 01:43 PM.

  5. #5
    adwodon is offline Member
    Join Date
    Dec 2010
    Posts
    10
    Rep Power
    0

    Default

    Someone has saved the day :)

    I need to have the file location with \\ at the end

    so it would be:

    Java Code:
     File file = new File("C:\\write.txt\\");
    I guess its a windows 7 thing?

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,368
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by adwodon View Post
    Someone has saved the day :)

    I need to have the file location with \\ at the end

    so it would be:

    Java Code:
     File file = new File("C:\\write.txt\\");
    I guess its a windows 7 thing?
    Yep, that would be my guess as well; I've never seen it before though ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Jni registry 'access denied' error.
    By durino13 in forum Advanced Java
    Replies: 4
    Last Post: 09-17-2011, 07:06 AM
  2. Replies: 22
    Last Post: 04-09-2011, 04:59 AM
  3. Replies: 6
    Last Post: 02-10-2011, 09:55 AM
  4. Access is denied
    By tholland in forum New To Java
    Replies: 2
    Last Post: 12-20-2010, 08:57 PM
  5. access denied
    By Dennis in forum Advanced Java
    Replies: 8
    Last Post: 06-05-2010, 08:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •