• 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 "; // 换行:

    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(" ");//windows:  linux:  mac:

    //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();//刷新缓冲区+释放资源

    }

  • 相关阅读:
    【华为云技术分享】使用keil5打开GD32F450i的MDK项目出现的问题以及J-Link无法烧录程序对应的解决方案
    【华为云技术分享】不为人知的稠密特征加入CTR预估模型的方法
    205. 判断两个字符串的模式是否相同 Isomorphic Strings
    541. 反转字符串2 Reverse String II
    插入排序,二分查找插入排序,使用二叉树的插入排序
    二分查找,求mid值的类型溢出问题
    二叉搜索树类的C#实现
    二叉搜索树,删除节点
    二叉搜索树的前驱节点和后继节点
    438. 匹配字符串(顺序不同但个数相同的字符串) Find All Anagrams in a String
  • 原文地址:https://www.cnblogs.com/javscr/p/10247936.html
Copyright © 2020-2023  润新知