Question

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!

Was it helpful?

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!

OTHER TIPS

If you want an applet to store data on the local machine, from 6u10 the javax.jnlp.PersistenceService is available. Creating a secure "signed applet" is difficult, and I wouldn't recommend it to anyone.

Applets run on the client so cannot access the servers disk. The code you posted will write to the clients local disk. I'd suggest changing it though to specify the directory you want to place the file. The users home directory would seem a good place for it

java.io.File file = new java.io.File(System.getProperty("user.home"), "highscores.txt");

just sign your applet. it is easy using netbeans.

  1. open the project in netbeans.
  2. right click on your project and select properties now you will have new window.
  3. go to > application > webstart on that window.
  4. check enable webstart.
  5. press customize button at signing and choose "self signed by a generated key".
  6. check "applet descriptor" and target your applet.
  7. press ok.

  8. rebuild the project (now netbeans will create certificates with all privileges )

  9. use "launch.html" at your projects dist dir to run the applet via jnlp.

That's all.

** used netbeans version = 7.0 ** used JDK = 1.6 ** certificates will expire in 6 months.

Thanks

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top