IO转换流

InputStreamReader/OutputStreamWriter用来实现将字节流转化成字符流。

通过转换流解决乱码
public class TestInputStreamReader {
    public static void main(String[] args){
        //创建文件字节流输入流对象
        try(FileInputStream fis = new FileInputStream("d:/a.txt");
            //创建转换流(字节到字符的转换)流对象,并在流对象中指定编码
            InputStreamReader isr = new InputStreamReader(fis,"gbk")
        ){
            StringBuilder sb = new StringBuilder();
            //操作流对象
            int temp = 0;
            while((temp = isr.read()) != -1){
                sb.append((char)temp);
            }   
            System.out.println();
        }catch(IOException e){
            e.printStackTrace();
        }
    }
}
通过转换流实现键盘输入屏幕输出
import java.io.*;

public class TestConvertStream {
    public static void main(String[] args){
        //创建字符输入和输出流:使用转换流将字节流转换成字符流
        BufferedReader br = null;
        BufferedWriter bw = null;
        try{
            br = new BufferedReader(new InputStreamReader(System.in));
            bw = new BufferedWriter(new OutputStreamWriter(System.out));
            //使用字符输入和输出流
            String str = br.readLine();
            //一直读取,直到用户输入了exit为止
            while(!"exit".equals(str)){
                //写到控制台
                bw.write(str);
                bw.newLine();  //写一行后换行
                bw.flush();  //手动刷新
                //再读一行
                str = br.readLine();
            }
        }catch(IOException e){
            e.printStackTrace()
        }finally{
            //关闭字符输入和输出流
            if(br != null){
                try{
                    br.close();
                }catch(IOException e){
                    e.printStackTrace();
                }
            }
            if(bw != null){
                try{
                    bw.close();
                }catch(IOException e){
                    e.printStackTrace();
                }
            }
        }
    }
}

相关推荐

  1. IO转换

    2024-06-06 08:52:03       27 阅读
  2. IO

    2024-06-06 08:52:03       69 阅读
  3. IO——其他

    2024-06-06 08:52:03       32 阅读
  4. IO-字符

    2024-06-06 08:52:03       35 阅读
  5. IO(字符)

    2024-06-06 08:52:03       32 阅读
  6. ios

    2024-06-06 08:52:03       73 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-06 08:52:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-06 08:52:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-06-06 08:52:03       82 阅读
  4. Python语言-面向对象

    2024-06-06 08:52:03       91 阅读

热门阅读

  1. springboot项目Redis统计在线用户

    2024-06-06 08:52:03       29 阅读
  2. 怎么排查native层的bug

    2024-06-06 08:52:03       24 阅读
  3. 【k8s的三种探针】

    2024-06-06 08:52:03       25 阅读
  4. Scala学习笔记7: 对象

    2024-06-06 08:52:03       30 阅读
  5. 小程序真题合集

    2024-06-06 08:52:03       24 阅读
  6. HW面试应急响应之场景题

    2024-06-06 08:52:03       28 阅读
  7. IDEA 开发中一些好用的插件

    2024-06-06 08:52:03       29 阅读
  8. 微信小程序中实现录音功能及其功效

    2024-06-06 08:52:03       30 阅读
  9. 【AI】DeepStream(09):deepstream-app源码详解

    2024-06-06 08:52:03       24 阅读
  10. 从零开始精通Onvif之设备发现

    2024-06-06 08:52:03       55 阅读