打破二进制文件 [英] Breaking binary files

查看:115
本文介绍了打破二进制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

参照我的<一个href=\"http://stackoverflow.com/questions/12296295/splitting-binary-files-and-adding-data-to-each-chunk\">$p$pvious问题。

我已经作出了计划与以下办法:结果
程序首先读取该文件,并将其存储到一个字节数组数据的2K。结果
然后被添加到每个分组中的数据也被存储在一个阵列和都被添加到一个数组列表。
然后将数组列表被写入该文件的输出流。

I have made the program with the following approach:
The program first reads 2k of data from the file and stores it into a byte array.
Then the data to be added to each packet is also stored in an array and both are added to an array list. The array list is then written to an output stream for the file.

在code是在这里:结果

The Code is here:

File bin=chooser.getSelectedFile();
int filesize=(int)bin.length();
int pcount=filesize/2048; 
byte[] file=new byte[filesize];
byte[] meta=new byte[12];
int arraysize=pcount*12+filesize;
byte[] rootfile=new byte[46];
ArrayList al = new ArrayList();
String root;
prbar.setVisible(true);
int mark=0;
String metas;
try{
    FileInputStream fis=new FileInputStream(bin);
    FileOutputStream fos=new FileOutputStream(bin.getName().replace(".bin", ".xyz"));
    ObjectOutputStream os=new ObjectOutputStream(fos);
    root="46kb"+"5678"+"0000"+pcount+"MYBOX"+"13"+"S208";
    rootfile=root.getBytes();
    for(int n=0;n<=pcount;n++)
    {
        fis.read(file, 0, 2048);
        mark=mark+2048;
        int v=(mark/filesize)*100;
        prbar.setValue(v);
        metas="02KB"+"1234"+n;
        meta=metas.getBytes();

        al.add(rootfile);
        al.add(meta);
        al.add(file);
    }
    os.writeObject(al.toArray());
}
catch(Exception ex){
    erlabel.setText(ex.getMessage());
}

程序运行没有任何错误,但没有正确创建的文件。
无论是做法是错误的或code。

The program runs without any errors but the file is not created correctly. Either the approach is wrong or the code.

请帮助

推荐答案

您似乎是写自己的二进制格式,但您使用ObjectOutputStream它有它自己的头。的writeObject的方式,可以让一个Java进程反序列化对象例如写对象不是数据与它的类层次结构和字段名。

You appear to be writing your own binary format but you are using ObjectOutputStream which has it own header. writeObject write an Object not data in a manner that lets a Java process deserialize that object e.g. with it class hierarchy and field names.

有关二进制,我建议你使用一个普通的DataOutputStream类与将更有效率,做你想要什么的BufferedOutputStream。

For binary, I suggest you use a plain DataOutputStream with a BufferedOutputStream which will be more efficient and do what you want.

我也建议你写的数据生成它,而不是使用一个ArrayList。这将使用较少的内存,使code更简单,更快。

I also suggest you write the data as you generate it rather than using an ArrayList. This will use less memory, make the code simpler and be faster.

我会写code更是这样。

I would write the code more like this

File bin = chooser.getSelectedFile();
int filesize = (int) bin.length();
int pcount = (filesize + 2048 - 1) / 2048;
byte[] file = new byte[2048];

FileInputStream fis = new FileInputStream(bin);
String name2 = bin.getName().replace(".bin", ".xyz");
OutputStream os = new BufferedOutputStream(new FileOutputStream(name2));
byte[] rootfile = ("46kb" + "5678" + "0000" + pcount + "MYBOX" + "13" + "S208").getBytes("UTF-8");

for (int n = 0; n < pcount; n++) {
    os.write(rootfile);
    byte[] metas = ("02KB" + "1234" + n).getBytes("UTF-8");
    os.write(metas);

    int len = fis.read(file);

    os.write(file, 0, len);
    int percent = 100 * n / pcount;
    prbar.setValue(percent);
}
ow.close();

这篇关于打破二进制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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