I have a game and I want to write a high score to a text file and read from it. This file will be stored on my site eg w_ww.mysite.com/hs.txt
I have some code for reading and writing which works when i write to a file on my computer but when i try to do the same thing online I can read but not write. Can anyone help me?
if i run my applet through Eclipse I can read/write but if I run a applet through a html file using firefox I can only read.
Here is my HTML code:
<HTML>
<HEAD>
</HEAD>
<BODY>
<applet code="Main.class" archive="Main.jar" width="800" height="600">
</applet>
</HTML>
here is my read and write:
URL url = null;
File f = null;
String fileToRead = "hs.txt"; //high scores text file
//INIT
try
{
url = new URL(getCodeBase(), fileToRead);
f = new File(fileToRead);
}
catch(Exception e2)
{
m_taHighScores.append("Init failed...");//m_taHighScores is a text area in the applet which displays the highscores.
}
//WRITE
try
{
BufferedWriter out = new BufferedWriter(new FileWriter(f,true));
out.write("HIGHSCORE: ");
m_taHighScores.append("WRITING:" + "HIGHSCORE: " + String.valueOf(m_snake.m_iScore) + "\n");
out.write(String.valueOf(m_snake.m_iScore)+"\n");
out.close();
m_taHighScores.append("WRITE SUCCEEDED \n");
}
catch(Exception e2)
{
m_taHighScores.append("Write failed... \n");
}
//READ
try
{
StringBuffer strBuff;
String line;
InputStream in = url.openStream();
BufferedReader bf = new BufferedReader(new InputStreamReader(in));
strBuff = new StringBuffer();
while((line = bf.readLine()) != null)
{
strBuff.append(line + "\n");
}
m_taHighScores.append("READING: \n");
m_taHighScores.append(strBuff.toString());
m_taHighScores.append("READ SUCCEEDED \n");
}
catch (Exception e2)
{
m_taHighScores.append("Read failed... \n");
}
//m_taHighScores.append("\n" + String.valueOf(m_snake.m_iScore));
m_taHighScores.setVisible(true);
thanks,
roland