The question sound dumb but it's like that
I had a binary string which contains a series of "0" and "1". If I store its as textfile it'll be huge since each character take about 8 bytes. But actually it should be much lesser since "0" or "1" is just actually 1 bit (1 byte/8). My question is how could to do that in Java?
Edited to avoid confuse
I've used an algorithm to encode all the data down to "0" and "1" sequence string. So 1 chunk of data could become quite big ... a few A4 paper of 1 string contains "0" and "1".
I'm stuck at the step to write down that string (which just "0" and "1") to an actual binary data file ... and it should be much lesser than just store down the text file which contains "0" and "1" as I assumed
I've done more googling and below is the solution in my case... I've put down the solution there incase that someone would have the same question as mine :)
After read convert a binary string representation to a byte array in C#. I've decided to implement its Java version
int numOfBytes = binarytring.length() / 8;
byte[] bytes = new byte[numOfBytes];
//store it down as 1 byte (8bits) each
for(int i = 0; i < numOfBytes; ++i) {
// thanks https://stackoverflow.com/questions/6658388/why-java-throws-a-numberformatexception for help me out of the exception.
bytes[i] = (byte) (Integer.parseInt(encoded.substring(8 * i, (8 * i) + 8), 2) & 0xFF);
}
FileOutputStream fos = new FileOutputStream("outputfilename");
fos.write(bytes);
fos.close();