java中IO流中的转换流




java中IO流中的转换流




OutputStreamWriter:将字节输出流转换为字符输出流

    package com.itheima_05;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;

/*
 * 需求:读取项目根目录下的SystemInOutDemo.java,并输出到命令行
 * 
 * 数据源:项目根目录下的SystemInOutDemo.java	BufferedReader
 * 目的地:命令行	System.out
 * 
 * 
 * 由于标准输出流是一个字节输出流,所以只能输出字节或者字节数组,但是我们读取到的数据则是字符串,如果想进行输出还需要转换成字节数组
 * 我们要想通过标准输出流输出字符串,把标准输出流转换成一种字符输出流即可,OutputStreamWriter
 * 
 * OutputStreamWriter(OutputStream out) :转换流,把字节输出流转换成字符输出流
 * 
 * 
 */
public class OutputStreamWriterDemo {
	public static void main(String[] args) throws IOException   {
		//method2();
		
		//创建输入流对象
		BufferedReader br = new BufferedReader(new FileReader("SystemInOutDemo.java"));
		//创建输出流对象
		//OutputStream os = System.out;
		//Writer w = new OutputStreamWriter(System.out);//多态,父类型引用指向子类对象
		//BufferedWriter bw = new BufferedWriter(w);
		
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
		
		
		//进行数据的读写
		String line;//用于存储读取到的数据
		while((line = br.readLine()) != null) {
			bw.write(line);
			bw.newLine();
		}
		
	
		
		//释放资源
		bw.close();
		br.close();
		
		
	}

	private static void method2() throws FileNotFoundException, IOException {
		//创建输入流对象
		BufferedReader br = new BufferedReader(new FileReader("SystemInOutDemo.java"));
		//创建输出流对象
		//OutputStream os = System.out;
		Writer w = new OutputStreamWriter(System.out);//多态,父类型引用指向子类对象
		
		//进行数据的读写
		String line;//用于存储读取到的数据
		while((line = br.readLine()) != null) {
			w.write(line);
			w.write("\r\n");
		}
		
	
		
		//释放资源
		w.close();
		br.close();
	}

	private static void method() throws FileNotFoundException, IOException {
		//创建输入流对象
		BufferedReader br = new BufferedReader(new FileReader("SystemInOutDemo.java"));
		//创建输出流对象
		OutputStream os = System.out;
		
		String line;//用于存储读取到的数据
		while((line = br.readLine()) != null) {
			os.write(line.getBytes());
			os.write("\r\n".getBytes());
		}
		
		//释放资源
		os.close();
		br.close();
	}
}






InputStreamReader:将字节输入流转换为字符输入流

package com.itheima_05;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

/*
 * 需求:读取键盘录入的数据,并输出到项目根目录下的a.txt文件中
 * 
 * 数据源:读取键盘录入的数据	System.in
 * 目的地:项目根目录下的a.txt	FileWriter
 * 
 * 
 * 
 * 转换流:需要把字节输入流转换成字符输入流,InputStreamReader
 * InputStreamReader(InputStream in) 
 */
public class InputStreamReaderDemo {
	public static void main(String[] args) throws IOException  {
		//创建输入流对象
		InputStream is = System.in;
		Reader r = new InputStreamReader(is);
		//创建输出流对象
		FileWriter fw = new FileWriter("a.txt");
		
		//读写数据
		char[] chs = new char[1024];
		int len;
		while((len = r.read(chs)) != -1) {
			fw.write(chs,0,len);
			fw.flush();
		}
		
		
		//释放资源
		fw.close();
		is.close();
		
	}

	private static void method() throws IOException {
		//创建输入流对象
		InputStream is = System.in;
		//创建输出流对象
		FileWriter fw = new FileWriter("a.txt");
		
		//读写数据
		byte[] bys = new byte[1024];
		int len;//用于存储读取到的字节个数
		while((len = is.read(bys)) != -1) {
			fw.write(new String(bys,0,len));
			fw.flush();
		}
		
		
		//释放资源
		fw.close();
		is.close();
	}
}






你可能感兴趣的:(java中IO流中的转换流)