package MONA.demo06_编码;
import java.io.FileInputStream;
import java.io.FileOutputStream;
/**
* 写入:gbk
* 读取(解析):utf-8
*
* 乱码出现的原因:
* 写入时的编码和读取时的编码不一致
* */
public class Dem01 {
public static void main(String[] args) throws Exception {
//写中文到文件中
FileOutputStream fos = new FileOutputStream("5.txt");
//使用UTF-8编码解析“你好”
fos.write("你好".getBytes("gbk"));
fos.close();
System.out.println("程序结束");
FileInputStream fis = new FileInputStream("5.txt");
byte[] bytes = new byte[6];
fis.read(bytes);
//默认的编码是utf-8
String s = new String(bytes,"gbk");
System.out.println("读取到的内容:"+s);
}
}
乱码问题.
最新推荐文章于 2024-08-31 10:44:02 发布