如何布尔数组转换为二进制,反之亦然在Java中? [英] How to convert boolean array to binary and vice versa in Java?

查看:142
本文介绍了如何布尔数组转换为二进制,反之亦然在Java中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是输出最有效的方式一个布尔数组(和投入)Java中的文件吗?我将使用一个字符串,每个字符是为't'或'F',然后我就想,为什么不采取八个时更少的空间?

What is the most efficient way to output a boolean array to (and input from) a file in Java? I was going to use a string with each character being either 't' or 'f' and then I thought, why not take eight time less space?

注意

其实,我不知道哪个回答是更好的方法,我只是选择了彼得,因为我明白。由于这两个回答者!

I actually have no idea which answer is the better method, I've just chosen Peter's because I understand it. Thanks to both answerers!

推荐答案

假设你有一个布尔[]

Say you have a boolean[]

boolean[] ar = {true,false,false,true,false,true,true,true,false,true,false,false,false,true,tr‌​ue};

和你想写这一个硬盘,你不关心它是如何在内存中实现的。

and you want to write this to a disk, and you don't care how its is implemented in memory.

public static void main(String... args) throws IOException {
    boolean[] ar = {true, false, false, true, false, true, true, true, false, true, false, false, false, true, true};

    FileOutputStream out = new FileOutputStream("test.dat");
    writeBooleans(out, ar);
    out.close();

    FileInputStream in = new FileInputStream("test.dat");
    boolean[] ar2 = new boolean[ar.length]; 
    readBooleans(in, ar2);
    in.close();

    System.out.println(Arrays.toString(ar));
    System.out.println(Arrays.toString(ar2));
    System.out.println("The file size was "+new File("test.dat").length()+" bytes.");
}

private static void writeBooleans(OutputStream out, boolean[] ar) throws IOException {
    for (int i = 0; i < ar.length; i += 8) {
        int b = 0;
        for (int j = Math.min(i + 7, ar.length-1); j >= i; j--) {
            b = (b << 1) | (ar[j] ? 1 : 0);
        }
        out.write(b);
    }
}

private static void readBooleans(InputStream in, boolean[] ar) throws IOException {
    for (int i = 0; i < ar.length; i += 8) {
        int b = in.read();
        if (b < 0) throw new EOFException();
        for (int j = i; j < i + 8 && j < ar.length; j++) {
            ar[j] = (b & 1) != 0;
            b >>>= 1;
        }
    }
}

打印

[true, false, false, true, false, true, true, true, false, true, false, false, false, true, true]
[true, false, false, true, false, true, true, true, false, true, false, false, false, true, true]
The file size was 2 bytes.

但如果我看文件实际上是多大

but if I look at how big the file actually is

$ ls -l test.dat
-rw-rw-r-- 1 peter peter 2 2012-02-19 14:04 test.dat
$ du -h test.dat 
4.0K    test.dat

报告说,长度为2个字节,但使用的磁盘空间实际上是4 KB。

It says the length is 2 bytes, but the disk space used is actually 4 KB.

注:约1分钟的时间价值约同80 MB的SSD(昂贵的磁盘,多用于HDD)所以,如果你不认为你将节省至少80 MB利用这一点,你可以是浪费你的时间。 ;)

Note: About 1 minute of your time is worth about the same as 80 MB of SSD (expensive disk, more for HDD) So if you don't think you will be saving at least 80 MB by using this, you could be wasting your time. ;)

您可以使用BitSet中,可以采取16倍的空间较小,因为每个字符是16位的。

You can use BitSet, which can take 16x less space as each character is 16-bit.

这篇关于如何布尔数组转换为二进制,反之亦然在Java中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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