Click here to Skip to main content
16,004,833 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi there, i want to make a java code that know a binary file have zero value
file value like this will give boolean false:
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00


And file value like this will give boolean true:
30 09 02 20 09 02 F0 08 02 D4 08 02 A4 08 06 60 08 01 B8 15 01 F4 15 01 F4 15 01 2C 15 01 64 14 01 64 14 01 98 13 01 34 13 01 D0 12 01 6C 12 01 A4 11 01 DC 10 01 14 10 01 80 0E 01 B8 0D 01 54 0D 01 F0 0C 01 8C 0C 01 C0 0B 01 F8 0A 01 30 0A


What I have tried:

Im using java.io.File

variable
File landMapH32File


condition but not working:
if (landMapH32File.exists() && landMapH32File.length() > 0) {}

this condition will return true even if a file contains zero values. Its must return false.
Posted
Updated 20-Jul-17 3:12am

Thanks @Richard MacCutchan for giving sugguestion:

this code work for me:

public static void main(String[] args) throws IOException {

		File file = new File("c:\\arena_l_lobby_land_map.h32");
        FileInputStream in = null;

        try {
            in = new FileInputStream(file);
            int c;
            boolean d = false;

            while ((c = in.read()) != -1) {
            	if (c > 0) {
            		d = true;
            	}
            }
            System.out.println(d);
        } finally {
            if (in != null) {
                in.close();
            }
        }
    }
 
Share this answer
 
Of course it will return true. You are only testing whether the file exists and contains anything. You need to read the contents and look for non-zero bytes.
 
Share this answer
 
Comments
EADever 20-Jul-17 8:42am    
Can you give a sample code to read contents and look to non-zero bytes?
EADever 20-Jul-17 8:47am    
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream(new File("c:\\arena_l_lobby_land_map.h32"));
int iByteCount = fis.read();
if (iByteCount == -1)
System.out.println("stream not empty");
else
System.out.println("stream is empty");
}

this is correct?
Richard MacCutchan 20-Jul-17 8:51am    
No, you need to read each byte and look for the first one that contains a value greater than zero.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900