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

本文详细讲解了Java中字符串的编码解码,包括getBytes()和String构造方法的应用,以及字符流的编码解码,重点介绍了Reader和Writer抽象基类,InputSteamReader和OutputStreamWriter的使用。此外,还探讨了字符流读写数据的不同方式,如write()的各种形式,以及flush()和close()的区别。最后,阐述了字符缓冲流BufferedWriter和BufferedReader的功能,包括newLine()和readLine()方法的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、字符串中的编码解码

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("\r\n");

        //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("\r\n");
        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();
    }
}

五、转换流对应的便捷子类

  • FileReader(String fileName):用于读取字符文件的便捷类
  • FileWriter(String fileName):用于写入字符文件的便捷类
//1. 根据数据源创建字符输入流对象
FileReader fr = new FileReader("ZiFuLiu\\abc.java");
//2. 根据目的地创建字符输出流对象
FileWriter fw = new FileWriter("ZiFuLiu\\copy.java");

六、字符缓冲流

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("hello\r\n");
        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();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值