1

I need to grant my applet the ability to save to a file. I tried to make a java.policy file to accomplish this and it's not working. Below is the code for the policy file and for the method that saves to the data file.

public void save()
{
    try
    {
        out = new BufferedWriter(new FileWriter("_stats.dat"));
        for (int i = 0; i < a.length; i++)
        {
            out.write(a[i].trim());
            out.newLine();
        }
        out.close();
    }
    catch (FileNotFoundException f)
    {
        System.out.println(f.getMessage());
    }
    catch (IOException i)
    {
        System.out.println(i.getMessage());
    }
}

grant {
    permission java.io.FilePermission "<<ALL FILES>>", "write";
};

They are both in the same directory and it appears that the save method is not recognizing the policy file or something. Thanks in advance for your feedback.

George Brighton
  • 5,131
  • 9
  • 27
  • 36
Feek
  • 297
  • 3
  • 15

1 Answers1

1

You might want to have a look at this previous post:

How to write to a file in applets in java?

Make sure your applet is signed.

Community
  • 1
  • 1
yamafontes
  • 5,552
  • 1
  • 18
  • 18
  • I'm planning on running the applet on my local machine (through appletviewer), not through a web browser. Is there anyway around not signing it because my professor views it through an appletviewer and not the html file. – Feek Nov 06 '13 at 23:45
  • *"my professor views it through an appletviewer"* Why code an applet? If it is due to spec. by teacher, please refer them to [Why CS teachers should stop teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). – Andrew Thompson Nov 06 '13 at 23:48
  • An applet launched free floating using JWS can be unsigned, and still access the local disks using the file services of the JNLP API. But then, the JNLP API also offers a `PersistenceService`. Here is a [demo of the `PersistenceService`](http://pscode.org/jws/api.html#ps). Of course, that is 'for the moment' until Oracle raises the default security level of Java.. – Andrew Thompson Nov 06 '13 at 23:54