学习OutputStreamWriter

OutputStreamWriter是字符流通向字节流的桥梁:可使用指定的 charset 将要写入流中的字符编码成字节。它使用的字符集可以由名称指定或显式给定,否则将接受平台默认的字符集。[源自JDK 文档]

下面是OutputStreamWriter类的简单使用:

package com.itheima.two;
import java.io.*;
public class OutputStreamWriterDemo {

	/**
	 * OutputStreamWriter是字符流通向字节流的桥梁
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		InputStream in =System.in;//字节流
		//字节流 转 字符流 
		InputStreamReader isr = new InputStreamReader(in);
		//缓冲区
		BufferedReader bufr = new BufferedReader(isr);
		//读取字符串数据
		String line = bufr.readLine();
		//输出流
		OutputStream out =System.out;
		OutputStreamWriter osw = new OutputStreamWriter(out);
		//缓冲区
		BufferedWriter bufw = new BufferedWriter(osw);
		//写到缓冲区
		bufw.write(line.toUpperCase());
		 
		//上面代码的简写
		BufferedReader bufr1=new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bufw1=new BufferedWriter(new OutputStreamWriter(System.out));
		
		String line1=null;
		while((line1 = bufr1.readLine())!=null){
			if("over".equals(line))break;
			bufw1.write(line1.toUpperCase());
			bufw1.newLine();
			bufw1.flush();
		}
		bufr1.close();
	}

}

注:上述代码是参考传智视频来写的.博文虽短.知识是重要的


展开阅读全文

没有更多推荐了,返回首页