JAVA如何检测二进制文件包含零值? [英] JAVA how to detect a binary file contains zero values?

查看:92
本文介绍了JAVA如何检测二进制文件包含零值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我想制作一个知道二进制文件零值的java代码

这样的文件值会给布尔值假:

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





我的尝试:



我使用java.io.File



变量



What I have tried:

Im using java.io.File

variable

File landMapH32File





条件但无法正常工作:



condition but not working:

if (landMapH32File.exists() && landMapH32File.length() > 0) {}



即使文件包含零值,此条件也将返回true。它必须返回false。


this condition will return true even if a file contains zero values. Its must return false.

推荐答案

当然它将返回true。您只测试文件是否存在并包含任何内容。你需要阅读内容并查找非零字节。
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.


感谢@Richard MacCutchan提供的建议:



这个代码对我有用:



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();
            }
        }
    }


这篇关于JAVA如何检测二进制文件包含零值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆