欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

写入二进制文件

程序员文章站 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();
}
}
}