Read and Write a File
At the top of your game:
int[] highScore = new int[10];
String[] highScoreInitials = new String[10];
public void readScoresFromDisk()
{
try
{
FileInputStream fis = new FileInputStream("scores.dat");
ObjectInputStream ois = new ObjectInputStream(fis);
for(int x=0;x<10;x=x+1)
{
highScoreInitials[x] = (String) ois.readObject();
highScore[x] = ois.readInt();
}
ois.close();
}
catch(Exception e)
{
}
}
public void writeScoresToDisk()
{
try
{
FileOutputStream fos = new FileOutputStream("scores.dat");
ObjectOutputStream oos = new ObjectOutputStream(fos);
for(int x=0;x<10;x=x+1)
{
oos.writeObject(highScoreInitials[x]);
oos.writeInt(highScore[x]);
}
oos.close();
}
catch(IOException ioe)
{
System.out.println("could not write scores to disk.");
}
}
int[] highScore = new int[10];
String[] highScoreInitials = new String[10];
public void readScoresFromDisk()
{
try
{
FileInputStream fis = new FileInputStream("scores.dat");
ObjectInputStream ois = new ObjectInputStream(fis);
for(int x=0;x<10;x=x+1)
{
highScoreInitials[x] = (String) ois.readObject();
highScore[x] = ois.readInt();
}
ois.close();
}
catch(Exception e)
{
}
}
public void writeScoresToDisk()
{
try
{
FileOutputStream fos = new FileOutputStream("scores.dat");
ObjectOutputStream oos = new ObjectOutputStream(fos);
for(int x=0;x<10;x=x+1)
{
oos.writeObject(highScoreInitials[x]);
oos.writeInt(highScore[x]);
}
oos.close();
}
catch(IOException ioe)
{
System.out.println("could not write scores to disk.");
}
}