Reputation: 531
I want to write the file as byte only and not the character.
String binaryString = "10110101"
I am using
byte mybyte = Byte.parseByte(binaryString,2);
I want to write this converted byte into the file as a byte
and not the character.
I am using :
FileOutputStream fos1 = new FileOutputStream(new File("output"));
fos.write(mybyte);
But after write, when I see the files the byte is actually written as the characters.
Is I am doing something wrong in conversion ? How to make it write as byte and not char ?
Edit:
Like for the String 101101010111001011111000 (taking 8 bits at a time and then writing to the file) : it is converted to "Z9|".
Upvotes: 0
Views: 521
Reputation: 3279
You actually write the binary data 10110101 to the file, but when you open that file in a text editor it will be displayed as a character.
If you want to write text that represent the given number (e.g. in decimal form), use a Writer:
FileOutputStream fos1 = new FileOutputStream(new File("output"));
OutputStreamWriter w = new OutputStreamWriter(fos1, StandardCharsets.UTF_8);
w.write(""+mybyte); // ""+mybyte creates a string with the decimal representation of mybyte
Upvotes: 1