62,614
社区成员
发帖
与我相关
我的任务
分享
String s = new String(buf);
如果你的源文件是GBK编码的,上面这句应该写成:
String s = new String(buf, 0, c, "GBK");;
否则的话,程序是按utf-8的方式生成字符串。FileInputStream in=new FileInputStream(new File("GBK.txt"));
byte[] buf=new byte[512];
int c;
while((c=in.read(buf, 0, 512))!=-1){
for (byte b : buf) {
if(b!=0){
System.out.print( b );
}
}
String s=new String(buf);
System.out.println(" "+s);
for (byte b : s.getBytes()) {
if(b!=0){
System.out.print( b );
}
}
Writer out=new OutputStreamWriter(new FileOutputStream(new File("utf-8.txt")));
out.write(s.trim());
out.close();
-50-46 ��
-17-65-67-17-65-67
也就是说在UTF-8下��这两个乱码怎么有2种字节(2位,6位)表达
// 别人发送给你时用 GBK 编码:
String data = "这是一句中文";
byte[] GBK = data.getBytes("GBK");
socketOutputStream.write(GBK);
// 你收到后:
byte[] received = socketInputStream....();
String decoded = new String(received, "GBK"); // 用对方的编码方式来解码
byte[] encoded = decoded.getBytes("UTF-8"); // 再编码成 UTF-8 转发给第3方。