Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
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  
  Reading data from a binary file.  (Read 498 times)
0 Members and 1 Guest are viewing this topic.
Offline LiquidNitrogen
« Posted 2014-03-18 02:43:51 »

I have a bunch of level files that were created with a different language. Im now trying to load them with java into my libgdx game. the maps are in my android "assets/maps" folder. i think the program doesnt know where to look for the specified file.. i always get "unable to load level in the log". If someone could give me a clue what im doing wrong, id really appreciate it!

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  
   public void loadMap(String name){
      int readPointer = 2;
      byte bytes[];
      try{
           File file = new File(name);
           InputStream insputStream = new FileInputStream(file);
           
           long length = file.length();
           bytes = new byte[(int) length];

           insputStream.read(bytes);
           insputStream.close();
           Gdx.app.log( FluffyBunniesGame.LOG, "loaded level." );
        }catch(Exception e){
            Gdx.app.log( FluffyBunniesGame.LOG, "Unable to load level." );
            return;
        }
      mapSizeX = bytes[0];
      mapSizeY = bytes[1];
      for (int x = 0; x < mapSizeX; x++){
      for (int y = 0; y < mapSizeY; y++){
         map[x][y].type = bytes[readPointer++];
         map[x][y].frame = bytes[readPointer++];
      }
      }
   }
Offline ewg

Junior Member


Medals: 4


Java games rock!


« Reply #1 - Posted 2014-03-18 03:33:11 »

The docs are here:
https://github.com/libgdx/libgdx/wiki/File-handling
http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/files/FileHandle.html

Files in the asset folder in libGdx are of the "Internal" type and you get a FileHandle object for them like this:

FileHandle fileHandle = Gdx.files.internal("maps/map1.bin"); //internal means it is in the assets folder

And there are readBytes() methods in the FileHandle class to read the whole file into a byte array.
Online trollwarrior1
« Reply #2 - Posted 2014-03-18 06:44:09 »

you also need to log e.toString(); and also stacktrace

1  
2  
3  
4  
      StackTraceElement[] stackTrace = e.getStackTrace();
      for(int i=0;i<stackTrace.length;i++) {
         log(stackTrace[i].toString());
      }


Exceptions are made so you can debug easier.. Why would you not print them?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Damocles
« Reply #3 - Posted 2014-03-18 14:27:54 »

maybe
1  
Gdx.app.log
throws a "Log cant use dot int the end" Exception ?

seriously, always print Details about what Exception was thrown, and the Stack trace.
Here check first if your can reference the File (path) in the first place.


Online Riven
« League of Dukes »

JGO Overlord


Medals: 746
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #4 - Posted 2014-03-20 22:31:15 »

on a side note: InputStream.read(byte[n]) is allowed to read 1..n bytes before it returns. Use a loop and check the return value for how many bytes were read (-1 in case of EOF), or use DataInputStream.readFully(...) to do it for you.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Pages: [1]
  ignore  |  Print  
 
 

 

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

The first screenshot will be displayed as a thumbnail.

CogWheelz (6 views)
2014-07-30 21:08:39

Riven (20 views)
2014-07-29 18:09:19

Riven (13 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (32 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (42 views)
2014-07-24 01:59:36

Riven (42 views)
2014-07-23 21:16:32

Riven (28 views)
2014-07-23 21:07:15

Riven (29 views)
2014-07-23 20:56:16
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!