Writing to binary file in Java

179 Views Asked by At

here is my task: Save the calculated data in a binary file according to the following format:

  • the first 8 bytes in each line represent how many prime numbers are saved in that line;
  • after 8 bytes, the unsupervised prime numbers are written according to the assumption that the first line contains only numbers that require exactly one byte to be saved, the second line contains only numbers that require exactly 2 bytes to be saved, in the third line 3 bytes are required, etc.

So I need to write prime numbers to a binary file. The first number of each line of the file will start with an 8-byte number, which indicates how many numbers will be written to that line. For example, in the first line we write all the numbers that need 1 byte, in the second line 2 bytes, etc. I seem to have done everything correctly, but it still writes it wrong...

class Write {
    public static final int[] Byte_Sizes  = {1,2,3,4};
    public  void write(String filename, ArrayList<Integer> data) {
        try( OutputStream os = new FileOutputStream(filename)) {
            int[] arr = countNumbersInEachLine(data);
            int index = 0;

            for(int j=0; j<arr.length; j++) {
                for (int i = 7; i >= 0; --i) {
                    os.write( (arr[j] >> i * 8) );
                }
                while (index<data.size() && index< arr[j]){
                    int size = getByteSize(data.get(index));
                    for (int b = size -1; b >= 0 ; --b) {
                        os.write( (data.get(index) >> b * 8) );
                    }index++;
                }os.write('\n');
            }

        }catch (IOException e) {
            e.printStackTrace();
            System.exit(1);
        }
    }
    public static int getByteSize(int value) {
        int size =1;
        while (value >= Math.pow(2, 8*size)) {
            size++;
        }
        return size;
    }
    public int[] countNumbersInEachLine(ArrayList<Integer> primes){
        int[] amount_in_each_line = new int[Byte_Sizes.length];
        for(int j =0; j< Byte_Sizes.length; j++) {
            int counter =0;
            for (int i = 0; i < primes.size(); i++) {
                if(getByteSize(primes.get(i)) == Byte_Sizes[j]){
                    counter++;
                }
            }
            amount_in_each_line[j] = counter;
        }
        return amount_in_each_line;
    }
}

I have method getByteSize to determine how manu bytes is needed to write number, method countNumbersInEachLine to count how many 1,2,3...-byte numbers exist and them add them on the begining of each line. I expect to have 54 as the number of numbers on the first line, then 54 single-byte numbers, then 6488 on the next line and 6488 numbers and so on. I also know that if everything is stored correctly, the file size should be 2,242kb, and I have a different one. When I try to read the file and output it to the screen I get a completely different result. Maybe someone can point to problems with my solution(

0

There are 0 best solutions below