how to change my file into binary data using java

Solutions on MaxInterview for how to change my file into binary data using java by the best coders in the world

showing results for - "how to change my file into binary data using java"
Matthew
18 Jul 2020
1File file = new File("filename.bin");
2byte[] fileData = new byte[file.length()];
3FileInputStream in = new FileInputStream(file);
4in.read(fileData):
5in.close();
6// now fileData contains the bytes of the file
7
Erika
26 Jul 2016
1String content = "";
2for(byte b : fileData)
3    content += getBits(b);
4// content now contains your bits.
5
Isabel
05 Jan 2021
1String getBits(byte b)
2{
3    String result = "";
4    for(int i = 0; i < 8; i++)
5        result += (b & (1 << i)) == 0 ? "0" : "1";
6    return result;
7}
8