Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
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 708 times)
0 Members and 1 Guest are viewing this topic.
Offline hvince95
« Posted 2012-11-24 00: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
« Reply #1 - Posted 2012-11-24 01: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: 269
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #2 - Posted 2012-11-24 02: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: 269
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #3 - Posted 2012-11-24 02: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.

Longarmx (41 views)
2014-10-17 03:59:02

Norakomi (33 views)
2014-10-16 15:22:06

Norakomi (26 views)
2014-10-16 15:20:20

lcass (30 views)
2014-10-15 16:18:58

TehJavaDev (60 views)
2014-10-14 00:39:48

TehJavaDev (60 views)
2014-10-14 00:35:47

TehJavaDev (50 views)
2014-10-14 00:32:37

BurntPizza (66 views)
2014-10-11 23:24:42

BurntPizza (38 views)
2014-10-11 23:10:45

BurntPizza (80 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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!