Applet reading/writing to server side file
Hi Java Forums,
I hope you guys can help me, I'm new to the forums and relatively new to Java. I have written a game which extends Canvas, and I am currently trying to write methods to save/read high scores to a file in the same directory the game is stored in. When I run the game in a JPanel it works fine, however when I upload to web space and run the game in an applet through the browser, the methods literally do nothing, they do not throw exceptions or anything. Ive done some reading around and suspect I need to add a policy file, but can't find any specific instructions to help with my problem. Here are the methods I have written to read/write to a file
Code:
public void saveHighScores() {
try {
FileWriter fs = new FileWriter("./out.txt");
BufferedWriter out = new BufferedWriter(fs);
for (int i = 0; i < 5; i++) {
out.write(names.get(i) + " ");
out.write(scores.get(i).toString() + " ");
}
out.close();
}catch (Exception e) {
System.err.println(e);
}
}
Code:
public void loadHighScores() {
try {
Scanner in = new Scanner(new File("./out.txt"));
while (in.hasNext()) {
names.add(in.next());
scores.add(Integer.parseInt(in.next()));
}
in.close();
} catch (Exception e) {
System.out.println(e);
}
}
Re: Applet reading/writing to server side file
To read/write to a server side file you will need to communicate with the server. The classes for communicating with a server include: Socket, HTTPURLConnection and URLConnection.
Your code uses Files which are for the local PC.
Where is it you want to read/write the files: locally on the PC with the browser or on the server the applet was loaded from?
Re: Applet reading/writing to server side file
I want the file to be saved on the server the applet is hosted on, so that the high score list is maintained for every user who plays the game on different machines. could you possibly direct me to a tutorial or some example code which demonstrates how to do this?
Re: Applet reading/writing to server side file
Sorry, I don't have a link to a tutorial myself.
Try Google