Reading data from a binary file.

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!


	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++];
		}
		}
	}

The docs are here:


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.

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


		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?

maybe

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.

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.