Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (578)
games submitted by our members
Games in WIP (499)
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 620 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 Kernel


Medals: 212
Projects: 11
Exp: 2 years


If it wasn't Awesome, it wasn't me.


« 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 Kernel


Medals: 212
Projects: 11
Exp: 2 years


If it wasn't Awesome, it wasn't me.


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

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

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

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

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (27 views)
2014-04-15 18:08:23

BurntPizza (24 views)
2014-04-15 03:46:01

UprightPath (39 views)
2014-04-14 17:39:50

UprightPath (21 views)
2014-04-14 17:35:47

Porlus (37 views)
2014-04-14 15:48:38

tom_mai78101 (61 views)
2014-04-10 04:04:31

BurntPizza (120 views)
2014-04-08 23:06:04

tom_mai78101 (220 views)
2014-04-05 13:34:39

trollwarrior1 (187 views)
2014-04-04 12:06:45

CJLetsGame (194 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
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!