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

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

问题描述

参考我的上一个问题.

我使用以下方法制作了程序:
程序首先从文件中读取 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.

代码在这里:

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());
}

程序运行没有任何错误,但文件未正确创建.要么方法错误,要么代码错误.

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.

对于二进制文件,我建议您使用带有 BufferedOutputStream 的普通 DataOutputStream,这将更高效并执行您想要的操作.

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

我还建议您在生成数据时写入数据,而不是使用 ArrayList.这将使用更少的内存,使代码更简单,速度更快.

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.

我会更像这样编写代码

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天全站免登陆