Java 字符码、字符流、字节流

字符编码表

Ascii: 0-xxxxxxx正数

iso-8859-1: 拉丁码表1-xxxxxxx  负数。

GB2312: 简体中文码表

GBK: 最常用的中文码表 String字符串默认

GB18030 最新的中文码表

unicode 国际标准码表 char字符默认 每个字符两字节

UTF-8: 基于unicode 每个字符一个或两个字节

能识别中文的码表:GBKUTF-8

常见的编码 GBK  UTF-8  ISO-8859-1

文字--->(数字) :编码 “abc.getBytes()  byte[]

(数字)--->文字  : 解码 byte[] b={97,98,99}  new String(b,0,len)

ANSI:系统编码

      字节流 In           字符流  要刷新Reader

抽象类   OutputStream           Writer

        |            /                  \

实现类   FileOutputStream   +   OutputStreamWriter = FileWriter

        |       \     编码         |

缓冲流   BufferedOutputStream  PrintStream        BufferedWriter

字节流(图片)

FileOutputStream  FileInputStream

public static void method01() throws IOException{

//FileOutputStream fos=new FileOutputStream(file);//传入File对象

//FileOutputStream fos=new FileOutputStream("F:\\a.txt");//创建/覆盖

FileOutputStream fos=new FileOutputStream("F:\\java\\a.txt",true);/追加

fos.write(97); //写入字节   正数:Asclla 负数:半个汉字 ?

byte[] bytes={97,98,99,100};

fos.write(bytes); // 写入一个  字节 数组

fos.write(bytes,1,1); // 1开始  长度为1

String str="你好,中国2\r\n"; // 换行:\r\n

fos.write(str.getBytes()); // 字符串 转 字节数组  写入

fos.close();

}

字符流(文本,只能是gbk格式)

public static void main(String[] args) throws IOException {

//FileWriter fw = new FileWriter("d:\\a.txt");//创建输出流对象

//FileWriter fw = new FileWriter("b.txt");//项目路径下 创建文件

FileWriter fw = new FileWriter("c.txt",true); //追加写入=true

fw.write("helloworld");//写入字符串

fw.write("\r\n");//windows:\r\n  linux:\n  mac:\r

//fw.flush();//字符需要 刷新缓冲区

fw.write("abcde",0,5);//写入字符串 起始索引 长度

fw.write('a');//写入字符char      a

fw.write(97);//写入字符 对应的Ascll码    a

char[] chs = {'a','b','c','d','e'};

fw.write(chs); //abcde

fw.write(chs,2,3);//写入字符数组 起始索引 长度

fw.close();//刷新缓冲区+释放资源

}

猜你喜欢

转载自www.cnblogs.com/javscr/p/10247936.html