static void testStrSerial()
{
String s = "Hello,中国!";
try {
FileOutputStream fos = new FileOutputStream("D:\\idea\\CZKMD\\files\\txt1.dat");
DataOutputStream dos = new DataOutputStream(fos);
// dos.writeBytes(s); //一个字节表示一个字符
// dos.writeUTF(s); //有中文时,但是前边加了两个字节记录长度
// dos.write(s.getBytes()); //有中文时
// dos.write(s.getBytes("GBK"));
dos.write(s.getBytes("UTF-8"));
dos.close();
fos.close();
//读,并显示每个字节
FileInputStream fis = new FileInputStream("D:\\idea\\CZKMD\\files\\txt1.dat");
int n = fis.available();
for (int i = 0; i < n; i++) {
System.out.println(fis.read());
}
} catch (IOException e) {
e.printStackTrace();
}
}
72
101
108
108
111
44
228
184
173
229
155
189
33
228 = 128 + 64 + 32 + 4
184 = 123 + 32 + 16 + 8
173 = 128 + 32 + 8 + 4 + 1
11100100 10111000 10101101
0100 111000 101101
01001110 00101101
4e2d = 20013 = '中'