当前位置: 代码迷 >> 综合 >> 字节字符流转换类
  详细解决方案

字节字符流转换类

热度:49   发布时间:2023-09-20 23:23:17.0
package com.mipo.file;import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;/*** InputStreamReader用于将字节流中读取到的字节按指定字符集解码成字符。其需要和InputStream “套接”。* OutputStreamWriter用于将要写入到字节流中字符按指定的字符集编码成字节。其需要和OutputStream “套接”。* 这两个转换流方便了字节流的读取和写入* @author Administrator**/
public class InputStreamReaderAndOutputStreamWriter {public static void main(String[] args) {System.out.println("请输入信息(退出输入e或exit):");//把标准输入流(键盘输入)这个字节流包装成字符流,再包装成缓冲流BufferedReader br = new BufferedReader(new InputStreamReader(System.in));//BufferedReader br = new BufferedReader(new FileInputStream("D:\\Personal\\Desktop\\IO\\File\\demo\\readme2.txt"));String str = null;try {while (null != (str = br.readLine())) {//读取用户输入的一行数据if ("e".equalsIgnoreCase(str) || "exit".equalsIgnoreCase(str)) {System.out.println("安全退出!!!");break;}//将读取的整行字符串转成大写输出System.out.println(">--"+str.toUpperCase());System.out.println("继续输入信息");}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally {if (null != br) {try {br.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}	}}

  相关解决方案