Optimal saving and loading times for bytes

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.

Attempted to implement threads for each function but cannot seem to get it to work. Help?

Thanks, Harry.

The first thing would be to not call them up every 30 seconds and just save them in the part of the application that needs the data. :point:

Secondly, put all the bytes into a byte[] and write that in one go.
Also use Buffered I/O Streams.

Thirdly, if you are using Java 7, use NIO.

Fourthly, use File.separator instead of “/” (To others on the forum: Does Java take care of this for you or do you still need to do this?)

Fifthly, You should be checking if the file exists before attempting to read from it. (Don’t know how to do this with Java 6)

Multithreading should only be used if you can keep the application running without the files being loaded.


// JAVA 7 ONLY

if(!Files.exists(Paths.get("saves" + File.separator + "...etc")))
     getDefaultValues(); // IF THE FILE DOESN'T EXIST
BufferedInputStream in = new BufferedInputStream(Files.newInputStream(Paths.get("saves" + File.separator + "...etc"), StandardOpenOption.READ));
BufferedOutputStream out = new BufferedOutputStream(Files.newOutputStream(Paths.get("saves" + File.separator + "...etc"), StandardOpenOption.WRITE, StandardOpenOption.CREATE));

// JAVA 6

BufferedInputStream in = new BufferedInputStream(new FileInputStream("saves" + File.separator + "...etc"));
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("saves" + File.separator + "...etc"));

// READING

in.read(id); // "the variable id defined elsewhere"
in.close();

// WRITING

byte[] out = new byte[600]
for(int i = 0; i < 300; i++)
{
    out[i*2] = 30;
    out[i*2+1] = 65;
}

out.write();
out.flush();
out.close();


A few edits to make sure I got everything…
:slight_smile: