写入二进制文件
程序员文章站
2022-06-12 17:33:26
...
public static void getBitFromString(String string) {
try {
FileWriter fw = new FileWriter("c:\\a.txt");
byte[] byts = string.getBytes();
for (int i = 0; i < byts.length; i++) {
fw.write(byts[i]);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void getBitFromByte(byte[] byts) {
try {
FileWriter fw = new FileWriter("c:\\a.txt");
String result = "";
for (int i = 0; i < byts.length; i++) {
result += Integer.toBinaryString(byts[i]);
}
fw.write(result);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}