java 输入输出IO 转换流-字符编码

编码和其产生的问题:

计算机中储存的信息都是用二进制数表示的,而我们在屏幕上看到的数字、英文、标点符号、汉字等字符是二进制数转换之后的结果。
按照某种规则,将字符存储到计算机中,称为编码 。反之,将存储在计算机中的二进制数按照某种规则解析显示出来,称为解码 。
比如说,按照A规则存储,同样按照A规则解析,那么就能显示正确的文本符号。反之,按照A规则存储,再按照B规则解析,就会导致乱码现象。
字符集和编码的关系参考:https://www.cnblogs.com/zhangmingda/p/9030229.html 理解。
在IDEA中,使用 FileReader 读取项目中的文本文件。由于IDEA的设置,都是默认的 UTF-8 编码,所以没有任何问题。但是,当读取Windows系统中创建的文本文件时,由于Windows系统的默认是GBK编码,就会出现乱码。
如在windows上用记事本打开,随便写入一段中文:
然后,我们用IO流FileReader读出来打印
复制代码
import java.io.*;

/**
 * @ClassName CharSetTransferExample
 * @projectName: object1
 * @author: Zhangmingda
 * @description: XXX
 * date: 2021/4/18.
 */
public class CharSetTransferExample {
    public static void main(String[] args) {
        String textPath = "输入输出文件读写/src/test/output/windows-create-剑门关.txt";
        String tmpLine;
        try(BufferedReader bufferedReader = new BufferedReader(new FileReader(textPath))) {
            while ((tmpLine = bufferedReader.readLine()) != null){
                System.out.println(tmpLine);
            }
        }catch (FileNotFoundException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}
复制代码

输出结果:

 

解决方案:通过转换流 指定字符编码 转换字节流如下↓↓↓

 

 

1、InputStreamReader

java.io.InputStreamReader ,是Reader的子类,是从字节流到字符流的桥梁。它读取字节,并使用指定的字符集将其解码为字符。它的字符集可以由名称指定,也可以接受平台的默认字符集。

语法:

  • InputStreamReader(InputStream in): 创建一个使用默认字符集的字符流。
  • InputStreamReader(InputStream in, String charsetName): 创建一个指定字符集的字符流。
复制代码
import java.io.*;

/**
 * @ClassName CharSetTransferExample
 * @projectName: object1
 * @author: Zhangmingda
 * @description: XXX
 * date: 2021/4/18.
 */
public class CharSetTransferExample {
    public static void main(String[] args) {
        String textPath = "输入输出文件读写/src/test/output/windows-create-剑门关.txt";
        String tmpLine;
//        try(BufferedReader bufferedReader = new BufferedReader(new FileReader(textPath))) { // 打开GBK编码文本,输出乱码
        try(BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(textPath),"GBK"))) {
            while ((tmpLine = bufferedReader.readLine()) != null){
                System.out.println(tmpLine);
                /**
                 * 剑门关-V哥
                 * 北望剑阁一线天,
                 * 遥想翼德战关前。
                 * 纵使王朝风云变,
                 * 雄关笑傲数千年。
                 */
            }
        }catch (FileNotFoundException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}
复制代码

2、OutStreamWriter

复制代码
import java.io.*;

/**
 * @ClassName WriteTextOutputStreamCharSetTransferExample
 * @projectName: object1
 * @author: Zhangmingda
 * @description: XXX
 * date: 2021/4/18.
 */
public class WriteTextOutputStreamCharSetTransferExample {
    public static void main(String[] args) {
        String dstTextPath = "输入输出文件读写/src/test/output/idea-write-gbkString.txt";
        try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(dstTextPath),"GBK"))){
            bw.write("我好爱你,爱爱爱啊!");
            bw.flush();
        }catch (UnsupportedEncodingException e){
            e.printStackTrace();
        }catch (FileNotFoundException e){
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        }
    }
}
复制代码

写入GBK编码字符成功,IDEA打提示当前默认使用UTF-8打开,建议用GBK编码reload

 

 

posted on   zhangmingda  阅读(551)  评论(0编辑  收藏  举报

编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2020-04-18 CSS 常用颜色代号
2020-04-18 原生XMLHTTPResponse,jQuery-Ajax 上传文件;iframe上传图片&预览;图片验证码小案例
2020-04-18 CSS样式笔记
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示