求大神赐教编码问题?
import java.io.FileInputStream;--------------------编程问答-------------------- 一样才怪呢,编码不同生成的字节数组当然也有不同。。。。。。 --------------------编程问答-------------------- str.getBytes()设置不同编码格式得到的数组是不一样的 --------------------编程问答-------------------- 一个汉字是两个字节 --------------------编程问答-------------------- 没有回答我要问的问题啊,我说的是写入的utf-8编码和读出来的utf-8怎么不一致??? --------------------编程问答--------------------
import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
public class Demo8 {
public static void main(String [] args) throws IOException {
String str = "中国";
System.out.print("默认:"+Arrays.toString(str.getBytes())+"\r\n");
System.out.println("gbk2312码表:"+Arrays.toString(str.getBytes("gb2312")));
System.out.println("utf-8码表:"+Arrays.toString(str.getBytes("utf-8")));
System.out.println("unicode码表:"+Arrays.toString(str.getBytes("unicode")));
FileWriter writer = new FileWriter("d:\\1.txt");
writer.write(str);
writer.flush();
FileInputStream in = new FileInputStream("d:\\1.txt");
System.out.println("我一个一个字节的读回来怎么不一样了");
System.out.print(in.read()+"-");
System.out.print(in.read()+"-");
System.out.print(in.read()+"-");
System.out.print(in.read()+"-");
System.out.print(in.read()+"-");
System.out.print(in.read()+"-");
}
}
把输出改成byte型,回=会和“默认”时输出的结果一样。
System.out.print((byte)(in.read()&0xff) +", ");
System.out.print((byte)(in.read()&0xff) +", ");
System.out.print((byte)(in.read()&0xff) +", ");
System.out.print((byte)(in.read()&0xff) +", ");
System.out.print((byte)(in.read()&0xff) +", ");
System.out.print((byte)(in.read()&0xff));
补充:Java , Java SE