简体   繁体   English

使用BitSet和FileOutputStream将位写入文件

[英]Writing Bits to a file using BitSet & FileOutputStream

I've run into a bit of a problem when it comes to writing specific bits to a file. 在将特定位写入文件时,我遇到了一个问题。 I apologise if this is a duplicate of anything but I could not find a reasonable answer with the searches I ran. 如果这是任何重复的内容,我深表歉意,但是我进行的搜索找不到合理的答案。

I have a number of difficulties with the following: 我在以下方面遇到许多困难:

  1. Writing a header (Long) bit by bit (converted to a byte array so the FileOutputStream can utilise it) to the file. 将文件头(长整数)一点一点写入(转换为字节数组,以便FileOutputStream可以利用它)到文件中。

  2. Writing single bits to the file. 将单个位写入文件。 For example, at one stage I am required to write a single bit set to 0 to the file so my initial thought would be to use a BitSet but Java seems to treat this as a null? 例如,在某个阶段,我需要将一个设置为0的位写入文件,因此我最初的想法是使用BitSet,但是Java似乎将此视为null?

     BitSet initialPadding = new BitSet(); initialPadding.set(0, false); fileOutputStream.write(initialPadding.toByteArray()); 

1) 1)

I create a FileOutputStream as shown below with the necessary file name: 我使用所需的文件名创建一个FileOutputStream,如下所示:

FileOutputStream fileOutputStream = new FileOutputStream(file.getAbsolutePath());

I am attempting to create an ".amr" file so the first step before I perform any bit manipulation is to write a header to the beginning of the file. 我试图创建一个“ .amr”文件,因此在执行任何位操作之前的第一步是将标头写入文件的开头。 This has the following value: 具有以下值:

Long defaultHeader = 0x2321414d520aL;

I've tried writing this to the file using the following method but I am pretty sure it does not write the correct result: 我尝试使用以下方法将其写入文件,但我很确定它不会写入正确的结果:

fileOutputStream.write(defaultHeader.byteValue()); 

Am I using the correct streams? 我使用的是正确的流吗? Are my convertions completely wrong? 我的convert依完全错误吗?


2) 2)

I have a public BitSet fileBitSet; 我有一个public BitSet fileBitSet; which has bits read in from a ".raw" file as the input. 从“ .raw”文件中读取的位作为输入。 I need to be able to extract certain bits from the BitSet in order to write them to the file later. 我需要能够从BitSet中提取某些位,以便稍后将其写入文件。 I do this using the following method: 我使用以下方法执行此操作:

public int getOctetPayloadHeader(int startPoint) {

    int readLength = 0;

    octetCMR = fileBitSet.get(0, 3);
    octetRES = fileBitSet.get(4, 7);

    if (octetRES.get(0, 3).isEmpty()) {

        /* Keep constructing the payload header. */

        octetFBit    = fileBitSet.get(8, 8);
        octetMode    = fileBitSet.get(9, 12);
        octetQuality = fileBitSet.get(13, 13);
        octetPadding = fileBitSet.get(14, 15);
        ... }

What would be the best way to go for writing these bits to a file bearing in mind that I may be required to sometimes write a single bit or 81 bits at a particular offset in the fileBitSet ? 考虑到有时可能需要我在fileBitSet中的特定偏移量写入单个位或81位,将这些位写入文件的最佳方法是什么?

There is only one thing you can write to an OutputStream: bytes. 您只能写一件事到OutputStream:字节。 You have to do the composing of your bits into bytes yourself; 您必须自己将位组成字节。 only you know the rules how the bits are to be put together into bytes. 只有您知道规则如何将位组合为字节。

As for stuff like: 至于这样的东西:

Long defaultHeader = 0x2321414d520aL;
fileOutputStream.write(defaultHeader.byteValue()); 

You should take a close look at the javadocs for the methods you are using. 您应该仔细查看所使用方法的javadocs byteValue() returns a single byte; byteValue()返回一个字节; so of course its not doing what you expect. 所以它当然没有按照您的期望做。 Working with streams is well explained in oracles tutorials: http://docs.oracle.com/javase/tutorial/essential/io/streams.html 在Oracle教程中对使用流进行了很好的解释: http : //docs.oracle.com/javase/tutorial/essential/io/streams.html

For writing single bits or groups of bits, you will need a custom OutputStream that handles grouping the bits into bytes to be written. 为了写入单个位或一组位,您将需要一个自定义OutputStream,该处理将这些位分组为要写入的字节。 Thats commonly called a BitStream (there is no such class in the JDK); 那通常称为BitStream (JDK中没有此类); you have to either write it yourself (which I highly recommend, its a very good excercise to teach you about bits and bytes) or find one on the web. 您必须自己编写(我强烈建议这样做,它是一个很好的练习来教您有关位和字节的知识),或者在网上找到一个。

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

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