0

Ok, simply put I am making a quiz game in a java applet, and I want to serialize an object which stores the high scores. When I do this it works perfectly in eclipse but not in a browser.

Here is the code of my applet where it reads the file: and yes I have all of the appropriate imports

package histApplet;

public class QuizApplet extends Applet
{
private static final String TRACKERLOC = "histApplet/track.ser";
private StatsTracker tracker;
private int difflevel = 1;
//other instance variables


public void init()
{
    //other code
    if(new File(TRACKERLOC).exists())
    {
        tracker = null;
        FileInputStream fis = null;
        ObjectInputStream in = null;
        try
        {
            fis = new FileInputStream(TRACKERLOC);
            in = new ObjectInputStream(fis);
            tracker = (StatsTracker)in.readObject();
            in.close();
        }
        catch(IOException ex)
        {
            ex.printStackTrace();
        }
        catch(ClassNotFoundException ex)
        {
            ex.printStackTrace();
        }
    }
    else
    {
        tracker = new StatsTracker(difflevel);
    }
    //other code
}

And here is my html code

<html>
<head><title>QuizApplet</title></head>
<body>

<center><applet code="histApplet/QuizApplet.class" height=550 width=700>
</applet></center>

</body>
</html>

If I comment out this code it works in a browser but otherwise doesn't. I'm not sure why this doesn't work, and any help would be greatly appreciated.

Ryan Stull
  • 1,056
  • 14
  • 35

3 Answers3

1

See my answer on how to write into a text file in Java.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
1

Java Applets execute in a sandbox within the browser, so have limited access to resources in the client machine running the applet (into the browser). File system can't be accessed by an Applet, as explained in several sites SecuringJava, Oracle.

You need to sign your Applet (trusted code) in order to get access to the file system, Oracle.

David Oliván
  • 2,717
  • 1
  • 19
  • 26
0

As written by David, applets can't access the local file system.

They can send data to the host they came from (and receive answers from there), so you could store the highscores on the server, if you have some server-side program which accepts these highscores there.

An alternative would be using a JNLP-deployed applet, then your applet could access an applet-specific local storage with a PersistenceService.

Paŭlo Ebermann
  • 73,284
  • 20
  • 146
  • 210
  • What type of program would I use to accept the file? Would using sockets work, or is that server specific? Also, would the program have to be running 24/7 or could it be activated just when it needs to receive the data? And sorry for all of the questions, I'm pretty new to using servers and the internet in my programming. – Ryan Stull Mar 15 '11 at 01:57
  • Any server side application would be possible. If this program should be able to interpret the serialized data (and not simply to send it back to the next applet), then it would be easier to use a Java-based solution. – Paŭlo Ebermann Mar 15 '11 at 11:23
  • You could have some program listening on a socket (running all the time), you could start a program if needed by means of inetd (then the client would use a socket connection, too), or use some HTTP-based solution like a Servlet, PHP-page or CGI-script. If the server program is Java-based, it should be better an already running process since the VM takes some time for startup. (This would be a Servlet or standalone socket solution, for example.) – Paŭlo Ebermann Mar 15 '11 at 11:33