【问题标题】:write/read variable byte encoded string representation to/from file in JAVA在JAVA中向/从文件写入/读取可变字节编码的字符串表示
【发布时间】:2016-11-24 06:15:40
【问题描述】:

大家!我最近了解了可变字节编码。 例如,如果文件包含以下数字序列:824 5 214577 应用可变字节编码此序列将被编码为 000001101011100010000101000011010000110010110001。 现在我想知道如何将其写入另一个文件,以便从原始文件中生成一种压缩文件。以及如何阅读它。我正在使用 JAVA 。

试过这个:

LinkedList<Integer> numbers = new LinkedList<Integer>();
numbers.add(824);
numbers.add(5);
numbers.add(214577);
String code = VBEncoder.encodeToString(numbers);//returns 000001101011100010000101000011010000110010110001 into code
File file = new File("test.compressed");
DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
out.writeBytes(code);
out.flush();

这只是将二进制表示写入文件。这不是我所期望的。

我也试过这个:

LinkedList<Integer> code = VBEncoder.encode(numbers);//returns linked list of Byte(i give its describtion later)
File file = new File("test.compressed");
DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file)));

for(Byte b:code){
        out.write(b.toInt());
        System.out.println(b.toInt());
}
out.flush();
// he goes the describtion of the class Byte
class Byte {
    int[] abyte;
    Byte() {
        abyte = new int[8];
    }
    public void readInt(int n) {
        String bin = Integer.toBinaryString(n);

        for (int i = 0; i < (8 - bin.length()); i++) {
            abyte[i] = 0;
        }
        for (int i = 0; i < bin.length(); i++) {
            abyte[i + (8 - bin.length())] = bin.charAt(i) - 48; 
        }
    }

    public void switchFirst() {
        abyte[0] = 1;
    }

    public int toInt() {
        int res = 0;
        for (int i = 0; i < 8; i++) {
            res += abyte[i] * Math.pow(2, (7 - i));
        }
        return res;
    }

    public static Byte fromString(String codestring) {
        Byte b = new Byte(); 
        for(int i=0; i < 8; i++)
            b.abyte[i] = (codestring.charAt(i)=='0')?0:1;
        return b;
    }

    public String toString() {
        String res = "";
        for (int i = 0; i < 8; i++) {
            res += abyte[i];
        }
        return res;
    }
}

它在控制台中打印:

6
184
133
13
12
177

这第二次尝试似乎有效...输出文件大小为 6 字节,而第一次尝试为 48 字节。 但第二次尝试的问题是我无法成功读回文件。

InputStreamReader inStream = new InputStreamReader(new FileInputStream(file));

        int c = -1;
        while((c = inStream.read()) != -1){
            System.out.println( c );
        }

我明白了:

6
184
8230
13
12
177

..所以也许我做错了:期待从你那里得到一些好的建议。谢谢!

【问题讨论】:

  • 你试过什么?显示你的代码,你有什么问题,人们可能会帮助你。 StackOverflow 不是“为我编写代码”服务(即使没有比“一种压缩文件”更清晰的描述也是不可能的)
  • @ErwinBolwidt 我现在已经提交了部分代码:-)。希望你能帮助我

标签: java file encoding compression multibyte


【解决方案1】:

解决了;我只是没有以正确的方式阅读文件:以下是正确的方式:

DataInputStream inStream = null; 
inStream = new DataInputStream(new BufferedInputStream(newFileInputStream(file)));

int c = -1;
while((c = inStream.read()) != -1){
    Byte b = new Byte();
    b.readInt(c);
    System.out.println( c +":" + b.toString());
}

现在我得到了这个结果:

6:00000110
184:10111000
133:10000101
13:00001101
12:00001100
177:10110001

现在将原始整数序列写入可变编码字节的重要性降低了文件的大小;如果我们通常在文件中写入这个整数序列,它的大小将是 12 字节(3 * 4 字节)。但现在只有 6 个字节。

int c = -1;
LinkedList<Byte> bytestream = new LinkedList<Byte>();
while((c = inStream.read()) != -1){
    Byte b = new Byte();
    b.readInt(c);
    bytestream.add(b);
}
LinkedList<Integer> numbers = VBEncoder.decode(bytestream);
for(Integer number:numbers) System.out.println(number);
//
//here goes the code of VBEncoder.decode
public static LinkedList<Integer> decode(LinkedList<Byte> code) {
    LinkedList<Integer> numbers = new LinkedList<Integer>();
    int n = 0;
    for (int i = 0; !(code.isEmpty()); i++) {
        Byte b = code.poll(); 
        int bi = b.toInt(); 
        if (bi < 128) {
            n = 128 * n + bi;
        } else { 
            n = 128 * n + (bi - 128);
            numbers.add(n); 
            n = 0; 
        }
    }
    return numbers;
}

我找回了序列:

824
5
214577

【讨论】:

    猜你喜欢
    • 2012-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-12
    • 1970-01-01
    • 1970-01-01
    • 2014-07-28
    相关资源
    最近更新 更多