简体   繁体   English

写入和读取Java二进制文件

[英]writing to and reading from binary file java

I am compressing a text file using Huffman code I generated then I converted all characters to string of 0's and 1's. 我正在使用生成的霍夫曼代码压缩文本文件,然后将所有字符转换为0和1的字符串。 Wrote them in file using following code. 使用以下代码将它们写在文件中。 (Input was 1011001110010011 ) (输入为1011001110010011

public static void writeToFile(String binaryString, BufferedWriter writer) throws IOException{
    int pos = 0;
    while(pos < binaryString.length()){
        byte nextByte = 0x00;
        for(int i=0;i<8 && pos+i < binaryString.length(); i++){
            nextByte = (byte) (nextByte << 1);
            nextByte += binaryString.charAt(pos+i)=='0'?0x0:0x1;
        }
        writer.write(nextByte);
        pos+=8;
    }
}

Then I tried to regenerate previous binary string 1011001110010011 from the file I just created, using following code 然后,我尝试使用以下代码从刚创建的文件中重新生成以前的二进制字符串1011001110010011

data = Files.readAllBytes(path);
for(int i=0;i<data.length;i++){
    byte nextByte = data[i];
    String tempString = "";
    for(int j=0;j<8; j++){
        byte temp = (byte) (0x1 & nextByte);
        if(temp==0x1){
            tempString="1".concat(tempString);
        }else if(temp==0x0){
            tempString="0".concat(tempString);
        }
        nextByte = (byte) (nextByte >> 1);
    }
    binary=binary.concat(tempString);  
}

But I got 111011111011111010110011111011111011111010010011 in output, I was just expecting some attached 0's. 但是我的输出是111011111011111010110011111011111011111010010011 ,我只是期待一些附加的0。

Edit: made change in from string to binary code, now its adding 0's at end to complete byte. 编辑:从字符串更改为二进制代码,现在将其末尾添加0表示完整的字节。

public static void writeToFile(String binaryString, BufferedWriter writer) throws IOException{
    int pos = 0;
    while(pos < binaryString.length()){
        byte nextByte = 0x00;
        for(int i=0;i<8; i++){
            nextByte = (byte) (nextByte << 1);
            if(pos+i < binaryString.length())
                nextByte += binaryString.charAt(pos+i)=='0'?0x0:0x1;
        }
        writer.write(nextByte);
        pos+=8;
    }
}

The problem is that BufferedWriter.write() writes a char , not a byte . 问题在于BufferedWriter.write()写入一个char ,而不是一个byte Whenever you're writing to the file, you're writing a variable-sized unicode character, not a single byte , so you're ending up with much more stored in your file than you were expecting. 每当您写入文件时,您都在写一个可变大小的unicode字符,而不是一个byte ,因此最终存储在文件中的存储量超出了您的预期。

You want to use 你想用

new BufferedOutputStream(new FileOutputStream("filename"))

instead, and change the signature of your method to take an OutputStream . 而是将方法的签名更改为采用OutputStream

(You might notice that OutputStream.write() takes an int rather than a byte , but that is just there to confuse you... it actually writes only the low-order byte, rather than the whole int , so it does what you want.) (您可能会注意到OutputStream.write()接受一个int而不是一个byte ,但这只是让您感到困惑...它实际上只写低位字节,而不是整个int ,因此它可以执行您的操作想。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM