I am making two methods which write and read bytes to and from files on the hard drive. Each of these methods could be called up to 30 times every second (dont ask!) and have a size of 32KB. At the moment my code looks like this:
public void save(int i) {
try {
FileOutputStream fos = new FileOutputStream("saves/" + "mySave" + i);
DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(fos));
for (int x = 0; x < 300; x ++) {
dos.write(30);//-127 to 128
dos.write(65);
}
dos.close();
} catch (IOException e) {
e.printStackTrace();
System.err.println("Unable to save file");
}
}
public void load(int i) {
try {
FileInputStream fis = new FileInputStream("saves/" + "mySave" + i);
DataInputStream dis = new DataInputStream(new BufferedInputStream(fis));
for (int x = 0; x < 300; x ++) {
id[x] = dis.read();//id is defined elsewhere
}
} catch (Exception e) {
e.printStackTrace();
System.err.println("Unable to load file");
}
}
Is there a faster way to do this? At the moment you can tell by the lag/stutter that files are being saved and loaded. I thought of creating a new thread which does all the saving and loading, so the original thread can run unbothered, but am yet to look into this.