package m1;
import java.io.*;
import java.util.zip.InflaterInputStream;
/**编码转换流
* OutputStreamWriter、InputStreamReader
* @author Yzt
* @version 1.0
* @date 2021/10/26 10:45
*/
public class encoding {
public static void main(String[] args) {
// f1();
f2();
}
private static void f2() {
InputStreamReader in=null;
try {
in=new InputStreamReader(new FileInputStream("1.txt"),"utf-8");//码表不同,则打印出的编码不同
System.out.println(in.read());//打印的是编码
/**
* 转中文
*/
char[] chars = new char[8192];
int read = in.read(chars);
System.out.println(new String(chars,0,read));
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static void f1() {
OutputStreamWriter out=null;
try {
/**
* 保证打开和使用时用的是同一张码表就不会出现乱码问题
*/
out=new OutputStreamWriter(new FileOutputStream("1.txt"),"utf-8");
out.write("哈比");
System.out.println("执行成功");
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交