B
B
bezvozni2020-11-30 19:57:52
Java
bezvozni, 2020-11-30 19:57:52

How to write the sequence of 8 bits "11111111" to a file?

"11111111" is in a String type variable.
I try to write to a file through the write method of the FileOutputStream class.
You need to write not only text files.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
bezvozni, 2020-12-01
@bezvozni

https://metanit.com/java/tutorial/6.3.php

String stringBits = "11111111";
byte oneByte = (byte) Integer.parseInt(stringBits , 2);

byte[] byteArray = new byte[1];
byteArray[0] = oneByte;

try (FileOutputStream fos = new FileOutputStream(PATH_OUT)) {
    fos.write(byteArray, 0, byteArray.length);
} catch (IOException ex) {
    System.out.println(ex.getMessage());
}

thanks for the conversion chela from the comments in the previous question

A
Alexey Cheremisin, 2020-11-30
@leahch

Take java.util.BitSet, it has toByteArray() method, and write via ByteArrayOutputStream
- https://metanit.com/java/tutorial/6.4.php
PS. And don't forget about BigEndian/LittleEndian - https://en.wikipedia.org/wiki/%D0%9F%D0%BE%D1%80%D...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question