Search code examples
javafiletextappletsigned-applet

Signed Java Applet writing to a text file


I want to make an Applet write to a text file. I think have overcome the security problem with my own certificate, but the text file isn't receiving the output.

My Applet can be seen at tomrenn.com/fallball, and should ask if you trust the certificate.

I'm not sure an Applet can write to a text file within the web hosted contents, so I'm asking if this is possible; and if not, at least be able to do it on the local machine. I had been using a FileOutputStream, but my teacher recommended this very basic output method.

public void writeToFile()
{
  try {
     java.io.File file = new java.io.File("highscores.txt");
     java.io.PrintWriter output = new java.io.PrintWriter(file);
     output.print(name + " " +score);
     output.close();
  } catch (IOException e) {
     System.err.println ("Unable to write to file");
     System.exit(-1);
  }
}

This is not what I would be implementing, just trying to see if I get any output. Thanks for looking over my question!


Solution

  • From an Applet, you cannot directly write to the server's file system. You can issue a request to the server that causes the server to write to its own file system, but an Applet does not have a way to write to a file system on a remote machine. (Of course, unless it's mounted NFS or otherwise.) To issue such a request, you could use Apache HttpClient to issue HTTP requests, for example. This may be more heavyweight than you are looking for. You can also have the client issue a POST to the server to say, "This is my high score," and let the server manage high scores.

    A signed Applet has every right to write to the local file system of the person running the Applet. If you are writing to the "current directory" (rather than an absolute full path), then make sure you know what directory the Applet is running in. Otherwise you may indeed create a file, but not be able to find it!