# [java] Read from binary file problem

## Recommended Posts

chefgon_ign    100
I've got a Super Mario clone that I'm making in a Java Applet. The level data is read in through a binary file. This works great when all the files are local (even through a web browser), but once I uploaded it to my web site it will no longer read from the file. I have a Pac Man game that loads its level almost exactly the same way, only from a text file instead of a binary file. This game loads fine from the web site. Any idea what the problem might be?

##### Share on other sites
ds2systems    122
This probably has something to do with byte ordering. If your file is saved on an intel machine, it will likely be in little endian format. For example, the word value 0x1234 would be stored as 0x3412. If the applet is reading in big endian, it would then read 0x3412.

##### Share on other sites
chefgon_ign    100
I don't think thats it, because I'm reading in single bytes at a time. Byte-ordering would only apply to multiple byte values, correct? Also, my host is a Windows host anyway, so it should work the same there as it does on my home machine.

Either way, I need to narrow down the problem. It didn't occur to me that it might be reading the values wrong. When I get home I'll edit the source so that it displays some values that it reads in, that will tell me if its reading the values wrong or if it just isn't reading them at all.

update - I had it display some values from the array that it reads the values into, and they're all zero. Its most likely having trouble opening the file.

update 2 - I thought that SmartFTP might have been accidentally uploading the level file as an ASCII trasnfer, so I re-uploaded in forced binary mode and it didn't change anything.

update 3 - Confirmed that my applet is not opening the file, but I'm still not sure why.

update 4 - Okay, I got it to give me the error message, it can't find my file. I've tried changing the name of the file, moving the location of the file, it just won't find it no matter what I do.

It gives me a java.io.FileNotFoundException exception when it tries to open the file with a URL.openStream() command. The folder I'm pointing to is here, the file is called "1-1". I can't figure out why it does't find it, the path matches exactly and it works fine when all the files are on my hard drive.

[Edited by - chefgon_ign on May 7, 2007 12:05:02 AM]

##### Share on other sites
ds2systems    122
Something to try: Change the name to something else. Maybe it is having trouble with the hyphen.

##### Share on other sites
chefgon_ign    100
I've tried changing the name, adding an extension, changing the folder.. nothing works. I'm really out of ideas. Here's the source code of the file read method, maybe somebody will notice something that I'm not seeing.

	public void readFile()	{		URL fileurl = null;		try{fileurl = new URL(base, fileToRead);}		catch(MalformedURLException e){}		try {InputStream in = fileurl.openStream();					for(int i=0; i<=207; i++)		{			for(int n=0; n<=14; n++)			{				backgroundgrid[i][n] = in.read();							}		}		in.close();		}catch (Exception e) {JOptionPane.showMessageDialog(null, "error: " + e);}				return;	}

It works fine when the files are on my local hard drive, but when I run it from my webhost, I get a "file not found" exception every time.

##### Share on other sites
Try printing out the URL to see if it being constructed properly.

##### Share on other sites
Ezzaral    122
Quote:
 Original post by CaptainJesterTry printing out the URL to see if it being constructed properly.

Yep, start there. If the url looks correct, make sure that your web context has access to where the file resides and that the path is relative to the root context of the web app. It's probably merely a context issue or perhaps a server config issue to map the file resource location.

##### Share on other sites
chefgon_ign    100
Thanks for all the suggestions, guys. After hours upon hours of frustration, I finally got it to work by adding a ".bin" extension to the binary file. I had tried a bunch of other extensions with no luck, so I'm not sure why that one works, but as long as its working I'm not going to waste any more time trying to figure it out. I can finally get back to the fun stuff!