Is it possible to write a string of zeros and ones in java to a binary file? But then you can read this binary file, and its contents must be equal to the initial line of zeros and ones. That is, for example, there is a string String OPER = "00100011101"; I write it into a binary file, then I read it, and this should be written on the command line: 00100011101 .

  • Yes, it is possible. What is your specific problem? - a_gura
  • @compl This is not a problem, it is a problem statement. Do you want us to solve the problem for you? Or do you have a more specific question? - a_gura
  • @compl: Then the question is: what should be contained in the file? What are the bytes? For example, for your case the source string is "00100011101" . Expansion and content, as you understand, do not affect each other. - VladD
  • one
    @compl: So you have to decide (well, or check with the teacher, if this is a training task), what bytes you will encode your string (with an eye to what you have to decode later), turn the string into this set of bytes, and write these bytes to a file. I repeat: the file is the set of bytes on the disk. “Binary” means “there is no prescribed format,” so how to map a string to bytes (that is, essentially the format) you will have to assign yourself. - VladD
  • one
    @compl: Of course. You read an array of bytes, turn it into an array of characters (0 -> '0' , 1 -> '1' ), and construct a string from an array of characters. - VladD

1 answer 1

 byte[] bytes = "asdasd".getBytes(); String str = new String(bytes); 

And where to write and read from where you probably figure it out.

  • Not what you need. - compl