如何将二进制(0和1)存储/加载到内存中?

时间:2016-12-10 23:07:56

标签: java memory binary

我想将一些0和1存储到内存中

我不知道如何清楚地解释这一点,但我会尽力这样做。 比方说,我有一个大约420字节的IMAGE文件。

red icon

我想想象它的二进制代码意味着我想要看到0和1。我运行这段代码来做到这一点,这很好......

import java.util.Scanner;
import java.io.BufferedInputStream;
import java.io.FileInputStream;

public class fileToBin {
    public static void main(String[] args) throws Exception {

        StringBuilder sb = new StringBuilder();

        Scanner ana = new Scanner(System.in);
        System.out.println("File?");
        String fileName = ana.nextLine();

        try (BufferedInputStream is = new BufferedInputStream(new FileInputStream(fileName))) {

            for (int b; (b = is.read()) != -1;) {

                String s = "0000000" + Integer.toBinaryString(b);

                s = s.substring(s.length() - 8); 
                sb.append(s);
            }
        }

        System.out.println(sb);
    }
}

我发送 FF0000.png 作为输入并获得以下输出...

100010010101000001001110010001110000110100001010000110100000101000000000000000000000000000001101010010010100100001000100010100100000000000000000000000001000000000000000000000000000000010000000000010000000011000000000000000000000000011000011001111100110000111001011000000000000000000000000000000010111001101010010010001110100001000000000101011101100111000011100111010010000000000000000000000000000010001100111010000010100110101000001000000000000000010110001100011110000101111111100011000010000010100000000000000000000000000001001011100000100100001011001011100110000000000000000000011101100001100000000000000000000111011000011000000011100011101101111101010000110010000000000000000000000000100111001010010010100010001000001010101000111100001011110111011011101001000110001000000010000000000000000000011001100001110100000111110100011011110111101000010010000100100000111000011101101100001101101010001111001011100000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010010001000000100111010000001001110000000000011100010000001011000100000010011001000010110110011110110101011011010001100001110111001011110010011001011111011001101010000000000000000000000000000000000100100101000101010011100100010010101110010000100110000010000010

我理解这是特定文件的内存导向(如果我对这些术语的任何错误,请纠正我)。

现在,假设我没有任何图像文件,我没有检索任何图像文件的二进制代码。我唯一拥有的是0和1,我不知道这组0和1是否实际上代表了一个文件。我不知道这代表什么。

我想将此0和1插入/加载到计算机内存中。我怎么能这样做?

这可以称为我之前操作的反向过程,我从文件中检索二进制代码。现在,我想将一些0和1插入内存并将其保存为文件。这不需要是IMAGE文件,任何文件扩展名都可以。因为我认为我不知道是否存在任何图像文件。

所以,我的主要任务是我有一些0和1,我想将它加载到内存并保存为文件。有可能吗?如何使用Java或任何其他编程语言执行此操作?这个内存和二进制表示如何工作?

对不起我的noobness并感谢您的耐心等待:))

1 个答案:

答案 0 :(得分:1)

给出名为String的{​​{1}}二进制文件以及名为str的某种OutputStream(例如FileOutputStream}:

对于out中的每8个字符,使用Integer.parseInt获取字节的数值,将write的数字值设为str

out

请注意,如果String str = ...; OutputStream out = ...; for (int i = 0; i < str.length; i += 8) { String byteStr = str.substring(i, i+8); int byteVal = Integer.parseInt(byteStr, 2); out.write(byteVal); } 不是8的倍数,这将导致IndexOutOfBoundsException