栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java——字符串和字符流的编码解码、字符流读写、字符缓冲流

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java——字符串和字符流的编码解码、字符流读写、字符缓冲流

Java——字符串和字符流的编码解码、字符流读写、字符缓冲流
  • 一、字符串中的编码解码
    • 1、编码
    • 2、解码
  • 二、字符流中的编码解码
    • 1、字符流抽象基类
    • 2、字符流中编码解码相关的两个类
    • 3、示例
  • 三、字符流中写数据的5种方式
  • 四、字符流读数据的2种方式
  • 五、字符缓冲流
    • 1、概述
    • 2、构造方法
    • 3、字符缓冲流读取数据示例
    • 4、字符缓冲流特有功能

一、字符串中的编码解码 1、编码
  • byte[] getBytes():使用平台的默认字符集将该String编码为一系列字节,将结果存储在新的字节数组中
  • byte[] getBytes(String charsetName):使用指定的字符集将该String编码为一系列字节,将结果存储在新的字节数组中
2、解码
  • String(byte[] bytes):通过平台的默认字符集解码指定的字节数组来构造新的String
  • String(byte[] bytes, String charsetName):通过指定的字符集解码指定的字节数组来构造新的String
import java.io.IOException;
import java.util.Arrays;

public class Demo1 {
    public static void main(String[] args) throws IOException {
        String str = "中国";
        //使用默认字符集编码
        byte[] bys = str.getBytes();
        System.out.println(Arrays.toString(bys));
        //使用默认字符集解码
        String s = new String(bys);
        System.out.println(s);
        System.out.println("------------------");

        //使用UTF-8字符集编码
        byte[] bys1 = str.getBytes("UTF-8");
        System.out.println(Arrays.toString(bys1));
        //使用UTF-8字符集解码
        String s1 = new String(bys, "UTF-8");
        System.out.println(s1);
        System.out.println("------------------");

        //使用GBK字符集编码
        byte[] bys2 = str.getBytes("GBK");
        System.out.println(Arrays.toString(bys2));
        //使用GBK字符集解码
        String s2 = new String(bys2, "GBK");
        System.out.println(s2);
    }
}

二、字符流中的编码解码 1、字符流抽象基类
  • Reader:字符输入流的抽象类
  • Writer:字符输出流的抽象类
2、字符流中编码解码相关的两个类
  • InputStreameReader
  • OutputStreamWrite
3、示例
import java.io.*;

public class Demo2 {
    public static void main(String[] args) throws IOException {
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("ZiFuLiu\java.txt"));
        osw.write("中国");
        osw.close();

        InputStreamReader isr = new InputStreamReader(new FileInputStream("ZiFuLiu\java.txt"));
        //一次读取一个字符数据
        int ch;
        while ((ch = isr.read()) != -1) {
            System.out.print((char) ch);
        }
        isr.close();
    }
}
三、字符流中写数据的5种方式
  1. void write(int c):写一个字符
    使用时,需要使用flush()刷新流
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

public class Demo3 {
    public static void main(String[] args) throws IOException {
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("ZiFuLiu\java.txt"));
        //void write(int c):写一个字符
        osw.write(97);
        osw.flush();
        osw.write(98);
        osw.flush();
        //释放资源
        osw.close();
    }
}

  1. void write(char[] cbuf):写入一个字符数组

  2. void write(char[] cbuf, int off, int len):写入字符数组的一部分

public class Demo4 {
    public static void main(String[] args) throws IOException {
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("ZiFuLiu\java.txt"));
        //void write(char[] cbuf):写入一个字符数组
        char[] chs = {'a', 'b', 'c', 'd'};
        osw.write(chs);
        osw.write("rn");

        //void write(char[] cbuf, int off, int len):写入字符数组的一部分
        osw.write(chs, 1, 3);
        osw.close();
    }
}

  1. void write(String str):写一个字符串
  2. void write(String str, int off, int len):写一个字符串的一部分
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

public class Demo5 {
    public static void main(String[] args) throws IOException {
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("ZiFuLiu\java.txt"));
        String s = "中国人";
        osw.write(s);
        osw.write("rn");
        osw.write(s,1,1);
        osw.close();
    }
}


flush()和close()区别:

  • flush():只刷新,使用完后还可以继续写入数据
  • close():先刷新,再关闭。使用后不能继续写入数据
四、字符流读数据的2种方式
  1. int read():一次读一个字符数据
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class Demo6 {
    public static void main(String[] args) throws IOException {
        InputStreamReader isr = new InputStreamReader(new FileInputStream("ZiFuLiu\java.txt"));
        //int read():一次读一个字符数据
        int ch;
        while ((ch = isr.read()) != -1) {
            System.out.print((char) ch);
        }
        //释放资源
        isr.close();
    }
}
  1. int read(char[] cbuf):一次读一个字符数组数据
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class Demo7 {
    public static void main(String[] args) throws IOException {
        InputStreamReader isr = new InputStreamReader(new FileInputStream("ZiFuLiu\java.txt"));
        //int read(char[] cbuf):一次读一个字符数组数据
        char[] chs = new char[1024];
        int len;
        while ((len = isr.read(chs)) != -1) {
            System.out.print(new String(chs, 0, len));
        }
        //释放资源
        isr.close();
    }
}
五、字符缓冲流 1、概述
  • BufferedWrite:将文本写入字符输出流,缓冲字符,以提供单个字符、数组和字符串的高效写入,可以指定缓冲区大小,或者接受默认大小。默认值足够大,可用于大多数用途
  • BufferedReader:从字符输入流读取文本,缓冲字符,以提供字符、数组和行的高效读取,可以指定缓冲区大小,或者接受默认大小。默认值足够大,可用于大多数用途
2、构造方法
  • BufferedWrite(Write out)
  • BufferedReader(Reader in)
3、字符缓冲流读取数据示例
import java.io.*;

public class Demo8 {
    public static void main(String[] args) throws IOException {
        //写入
        BufferedWriter bw = new BufferedWriter(new FileWriter("ZiFuLiu\java.txt"));
        bw.write("hellorn");
        bw.write("world");
        bw.close();

        //读取
        BufferedReader br = new BufferedReader(new FileReader("ZiFuLiu\java.txt"));
        //一次读取一个字符数据
        int ch;
        while ((ch = br.read()) != -1) {
            System.out.print((char) ch);
        }

        //一次读取一个字符数组数据
        char[] chs = new char[1024];
        int len;
        while ((len = br.read(chs)) != -1) {
            System.out.print(new String(chs, 0, len));
        }
        br.close();
    }
}
4、字符缓冲流特有功能
  • BufferedWriter:
    void newLine():写一行行分隔符,行分隔符字符串由系统属性定义
  • BufferedReader:
    public String readLine():读一行文字,结果包含行的内容的字符串,不包含任何行终止字符,如果流的结尾已经到达,则为null
import java.io.*;

public class Demo9 {
    public static void main(String[] args) throws IOException {
        BufferedWriter bw = new BufferedWriter(new FileWriter("ZiFuLiu\java.txt"));
        //写数据
        for (int i = 0; i < 10; i++) {
            bw.write("hello");
            bw.newLine();
            bw.flush();
        }

        //读数据
        BufferedReader br = new BufferedReader(new FileReader("ZiFuLiu\java.txt"));
        String line;
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }

        //释放资源
        bw.close();
        br.close();
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/820073.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号