Results 1 to 7 of 7

Thread: FileWriter question

  1. #1

    Thread Starter
    Member
    Join Date
    May 2001
    Location
    Baltimore
    Posts
    61

    FileWriter question

    I am using a FileWriter in a JApplet class to write to a text file. The text file that I am writing to is first read by a Buffered Reader that gives the data to strings in my application. The reading in process works as expected but when the condition is met that I have set up for the FileWriter to write to the text file I receive this error when I run the Applet in a webbrowser:
    java.security.AccessControlException: access denied (java.io.FilePermission C:\Big Walt Productions\Five Word Draw\draw.txt write)
    and the file is not written to.

    Here is the code I am using for both the FileWriter and Buffered Reader:

    FileWriter writer;
    BufferedReader buff_hiscore;

    /**
    * Reads data from text file
    *
    */
    public void update_highscore()
    {

    try
    {
    buff_hiscore = new BufferedReader(new FileReader("C:/Big Walt Productions/Five Word Draw/draw.txt"));

    for (int i = 0; i < 2; i++)
    {
    next_line = buff_hiscore.readLine();
    if(i==0)
    {
    str_highscore_name = next_line;
    }

    else
    {
    str_highscore = next_line;
    int_highscore = Integer.parseInt(str_highscore);
    }

    }

    buff_hiscore.close();

    }

    catch(IOException e)
    {
    }


    lbl_highscore_name.setText(str_highscore_name);
    lbl_highscore.setText(str_highscore);

    }




    public void high_score_check()
    {

    try
    {
    writer = new FileWriter("C:/Big Walt Productions/Five Word Draw/draw.txt");

    if(int_score > int_highscore)
    {
    for(int i= 0; i < 2; i++)
    {
    if(i==0)
    {

    writer.write(str_playername);
    }

    else
    {

    writer.write(str_score);

    }
    }

    }

    writer.close();
    }
    catch(IOException e)
    {
    }

    int_highscore = int_score;
    lbl_highscore_name.setText(str_playername);
    lbl_highscore.setText(str_score);




    }


    Can someone please help me with this problem?
    Striver2000

  2. #2
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: FileWriter question

    Wouldn't be too good if an Applet could access my file system.
    The security manager can be a pain somtimes but it has it's purpose. There are ways around some stuff others probably not like trying to connect to a host other than the one the applet was downloaded from.

  3. #3
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: FileWriter question

    Sorry i didn't quite provide an answer. What you need to do is set up a policy file to grant the permission or permissions that you want to apply to the Applet. Sun provideds a good step through that doesen't seem to hard to follow.

    http://java.sun.com/docs/books/tutor...ur1/index.html

  4. #4

    Thread Starter
    Member
    Join Date
    May 2001
    Location
    Baltimore
    Posts
    61

    Re: FileWriter question

    The tutorial was helpful but I still have some questions. First, I am going to run my applet from my website
    so I am assuming that to allow the applet to be able to write to a file I must also upload the policy file I created to my webspace, however, the file I created has no program association which means it has no extension, and when I try to upload it I recieve a invalid file response so how would I fix this? Also, the tutorial did not tell me how to use the policy I created to allow me to write to file from my Applet am I suppose to insert code in my JApplet class to use the policy file or add code to my html document so when the Applet runs it knows that the file has permission to be written to, if so what code would I have to use and where would I have to place if ? All help is gratefully appreciated
    Striver2000

  5. #5
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: FileWriter question

    I don't think you would have to add any code to your files in order to grant permissions. I am pretty sure after you run the policytool and click "Add Policy Entry" you can use the CodeBase to associate .class files stored within a specific directory with the desired permission's.

  6. #6

    Thread Starter
    Member
    Join Date
    May 2001
    Location
    Baltimore
    Posts
    61

    Re: FileWriter question

    I still have to upload the policy file to my webspace right but when I try it says its not a valid file because it does have a file extension associated with so what should I do?
    Striver2000

  7. #7
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: FileWriter question

    Posted by Striver2000

    I still have to upload the policy file to my webspace right but when I try it says its not a valid file because it does have a file extension associated with so what should I do?
    Mind you i haven't tried any of this because i simply never had a need to grant an Applet special permissions but i would think that the policy file has to reside on your machine or any machine wishing to grant the Applet privileges.

    So just upload your files then use the policytool to specify the place where the files are stored and pick your permissions and you should be finished.

Posting Permissions

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



Click Here to Expand Forum to Full Width