放入ByteBuffer然后将其写入文件比写入单个字段更有效 [英] Is put-ing to a ByteBuffer then writing it to a file more efficient than writing the individual field

查看:79
本文介绍了放入ByteBuffer然后将其写入文件比写入单个字段更有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想将对象的数据成员的值写到文件中,所以在这里我不能使用序列化,因为它写了很多东西我不需要的其他信息.这是我用两种方法实现的.一种使用字节缓冲区,另一种不使用字节缓冲区.

I want to write ONLY the values of the data members of an object into a file, so here I can can't use serialization since it writes a whole lot other information which i don't need. Here's is what I have implemented in two ways. One using byte buffer and other without using it.

不使用ByteBuffer: 第一种方法

Without using ByteBuffer: 1st method

public class DemoSecond {

    byte characterData;
    byte shortData;
    byte[] integerData;
    byte[] stringData;

    public DemoSecond(byte characterData, byte shortData, byte[] integerData,
        byte[] stringData) {
        super();
        this.characterData = characterData;
        this.shortData = shortData;
        this.integerData = integerData;
        this.stringData = stringData;
    }

    public static void main(String[] args) {
        DemoSecond dClass= new DemoSecond((byte)'c', (byte)0x7, new byte[]{3,4},
            new byte[]{(byte)'p',(byte)'e',(byte)'n'});

        File checking= new File("c:/objectByteArray.dat");
        try {
            if (!checking.exists()) {
                checking.createNewFile();
            }
            // POINT A
            FileOutputStream bo = new FileOutputStream(checking);
            bo.write(dClass.characterData);
            bo.write(dClass.shortData);
            bo.write(dClass.integerData);
            bo.write(dClass.stringData);
            // POINT B
            bo.close();
        } catch (FileNotFoundException e) {
                System.out.println("FNF");
                e.printStackTrace();
        } catch (IOException e) {
                System.out.println("IOE");
                e.printStackTrace();
        }
    }
}

使用字节缓冲区:另一件事是数据成员的大小将始终保持不变,即characterData = 1byte,shortData = 1byte,integerData = 2byte和stringData = 3byte.因此,此类的总大小为7byte 始终

Using byte buffer: One more thing is that the size of the data members will always remain fixed i.e. characterData= 1byte, shortData= 1byte, integerData= 2byte and stringData= 3byte. So the total size of this class is 7byte ALWAYS

第二种方法

            // POINT A
            FileOutputStream bo = new FileOutputStream(checking);
            ByteBuffer buff= ByteBuffer.allocate(7);
            buff.put(dClass.characterData);
            buff.put(dClass.shortData);
            buff.put(dClass.integerData);
            buff.put(dClass.stringData);
            bo.write(buff.array());
            // POINT B

我想知道两种方法中哪一种更优化?并请给出原因.

I want know which one of the two methods is more optimized? And kindly give the reason also.

上面的类 DemoSecond 只是一个示例类.

The above class DemoSecond is just a sample class.

我的原始课程的大小为 5到50个字节.我不认为这里的大小可能是问题. 但是我的每个课程都是固定大小的,例如 DemoSecond

My original classes will be of size 5 to 50 bytes. I don't think here size might be the issue. But each of my classes is of fixed size like the DemoSecond

还有很多这种类型的文件,我要在二进制文件中写入.

PS

如果我使用序列化,它还会写入单词"characterData","shortData","integerData","stringData"以及其他我不想在文件中写入的信息.我在这里担心的只是其价值.在此示例中,其为:'c',7、3、4'p','e','n'.我只想将这7个字节写入文件,而不要写入其他对我无用的信息.

if I use serialization it also writes the word "characterData", "shortData", "integerData","stringData" also and other information which I don't want to write in the file. What I am corcern here is about THEIR VALUES ONLY. In case of this example its:'c', 7, 3,4'p','e','n'. I want to write only this 7bytes into the file, NOT the other informations which is USELESS to me.

推荐答案

在执行文件I/O时,应记住,I/O操作可能会比文件I/O进行的任何工作都要慢得多.您的输出代码中的CPU.初步估算,I/O的成本与您要写入的数据量成正比,再加上为执行I/O而进行的每个操作系统调用的固定成本.

As you are doing file I/O, you should bear in mind that the I/O operations are likely to be very much slower than any work done by the CPU in your output code. To a first approximation, the cost of I/O is an amount proportional to the amount of data you are writing, plus a fixed cost for each operating system call made to do the I/O.

因此,在您的情况下,您希望最大程度地减少编写操作的操作系统调用次数.这是通过在应用程序中缓冲数据来完成的,因此应用程序几乎不执行放置较大的操作系统调用.

So in your case you want to minimise the number of operating system calls to do the writing. This is done by buffering data in the application, so the application performs few put larger operating system calls.

使用字节缓冲区是这样做的一种方法,因此您的ByteBuffer代码将比FileOutputStream代码更有效.

Using a byte buffer, as you have done, is one way of doing this, so your ByteBuffer code will be more efficient than your FileOutputStream code.

但是还有其他考虑.您的示例执行的写入次数不多.因此无论如何它都可能非常快.任何优化都可能是过早的优化.优化往往会使代码更加复杂且难以理解.要了解您的ByteBuffer代码,读者需要了解>的工作方式 ,以及他们需要了解的FileOutputStream代码的所有内容.而且,如果您更改文件格式,则更有可能使用ByteBuffer代码引入错误(例如,缓冲区太小).

But there are other considerations. Your example is not performing many writes. So it is likely to be very fast anyway. Any optimisation is likely to be a premature optimisation. Optimisations tend to make code more complicated and harder to understand. To understand your ByteBuffer code a reader needs to understand how a ByteBuffer works in addition to everything they need to understand for the FileOutputStream code. And if you ever change the file format, you are more likely to introduce a bug with the ByteBuffer code (for example, by having a too small a buffer).

通常对输出进行缓冲.因此,Java已经提供了可帮助您的代码也就不足为奇了.该代码将由专家编写,测试和调试.除非您有特殊要求,否则应始终使用此类代码,而不要编写自己的代码.我要引用的代码是BufferedOutputStream类.

Buffering of output is commonly done. So it should not surprise you that Java already provides code to help you. That code will have been written by experts, tested and debugged. Unless you have special requirements you should always use such code rather than writing your own. The code I am referring to is the BufferedOutputStream class.

要使用它,只需更改不使用ByteBuffer的代码,只需将打开文件的代码行更改为

To use it simply adapt your code that does not use the ByteBuffer, by changing the line of your code that opens the file to

 OutputStream bo = new BufferedOutputStream(new FileOutputStream(checking));

这篇关于放入ByteBuffer然后将其写入文件比写入单个字段更有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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