字符流

package com.czie.iot1913.lps.IO.InPutStream;

import java.util.Arrays;

/**
* FileName: FileInPutStream03
* Author: lps
* Date: 2022/3/26 11:56
* Sign:刘品水 Q:1944900433
* <p>
* 一个汉字存储如果是gbk编码是两个字节
* 如果utf—8是3个字节
*/
public class FileInPutStream03 {
public static void main(String[] args) throws Exception {
// FileInputStream fis = new FileInputStream("F:\\JavaCode\\fos.txt");
// int by;
// while ((by=fis.read())!=-1){
// System.out.print((char) by);
// }
//hello
//world
//刘品水


// FileInputStream fis = new FileInputStream("F:\\JavaCode\\fos.txt");
// byte[] bys = new byte[1024];
// int len;
// while ((len=fis.read(bys))!=-1){
// System.out.println(new String(bys,0,len));
// }
//hello
//world
//刘品水
//String s="abc";//[97, 98, 99]
String s = "中国";//[-28, -72, -83, -27, -101, -67]
// byte[] bys = s.getBytes();
// byte[] bys = s.getBytes(StandardCharsets.UTF_8);//[-28, -72, -83, -27, -101, -67]
byte[] bys = s.getBytes("GBK");//[-42, -48, -71, -6]
System.out.println(Arrays.toString(bys));

//字符流=字节流+编码表
//UTF-8是三位 GBK是两位
    

//fis.close();

}
}


package com.czie.iot1913.lps.IO.InPutStream;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

/**
* FileName: StringDemo
* Author: lps
* Date: 2022/3/26 12:39
* Sign:刘品水 Q:1944900433
*/
public class StringDemo {
public static void main(String[] args) throws UnsupportedEncodingException {
String s = "中国";
byte[] bys00 = s.getBytes();
System.out.println(Arrays.toString(bys00));

byte[] bys01 = s.getBytes(StandardCharsets.UTF_8);
System.out.println(Arrays.toString(bys01));

byte[] bys02 = s.getBytes("GBK");
System.out.println(Arrays.toString(bys02));

String ss=new String(bys00,"UTF-8");
System.out.println(ss);

String ss00=new String(bys01);
System.out.println(ss00);

String ss01=new String(bys02,"GBK");
//涓浗
System.out.println(ss01);



}
}

 

posted @ 2022-03-26 12:25  刘品水  阅读(67)  评论(0编辑  收藏  举报