Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (406)
games submitted by our members
Games in WIP (293)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Optimal saving and loading times for bytes  (Read 407 times)
0 Members and 1 Guest are viewing this topic.
Offline hvince95

Junior Member


Medals: 1
Projects: 2



« Posted 2012-11-24 01:39:03 »

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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
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.
Offline hvince95

Junior Member


Medals: 1
Projects: 2



« Reply #1 - Posted 2012-11-24 02:22:19 »

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

Thanks, Harry.
Offline HeroesGraveDev

JGO Wizard


Medals: 64
Projects: 8


Muahahahahahaha...


« Reply #2 - Posted 2012-11-24 03:22:38 »

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.  Pointing

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.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
// 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();

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline HeroesGraveDev

JGO Wizard


Medals: 64
Projects: 8


Muahahahahahaha...


« Reply #3 - Posted 2012-11-24 03:31:46 »

A few edits to make sure I got everything...
 Smiley

Pages: [1]
  ignore  |  Print  
 
 

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Get high quality music tracks for your game!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (84 views)
2013-05-17 21:29:12

alaslipknot (92 views)
2013-05-16 21:24:48

gouessej (123 views)
2013-05-16 00:53:38

gouessej (115 views)
2013-05-16 00:17:58

theagentd (127 views)
2013-05-15 15:01:13

theagentd (114 views)
2013-05-15 15:00:54

StreetDoggy (158 views)
2013-05-14 15:56:26

kutucuk (180 views)
2013-05-12 17:10:36

kutucuk (180 views)
2013-05-12 15:36:09

UnluckyDevil (187 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!
Page created in 0.18 seconds with 21 queries.