转换流InputStreamReader、OutputStreamWriter

转载请注明出处转换流InputStreamReader、OutputStreamWriter_Mr_Leixiansheng的博客-CSDN博客

1、InputStreamReader

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;


public class Test {


	public static void main(String[] args) {
		// TODO Auto-generated method stub
		FileInputStream fis = null;
		BufferedReader reader = null;
		
		try {
			//设置读取位置
			fis = new FileInputStream("d:\\text2.txt");
			//转换流InputStreamReader
			reader = new BufferedReader(new InputStreamReader(fis));
			//缓存区
			String str = "";
			//读取数据
			while((str = reader.readLine()) != null) {
				System.out.println(str);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally {
			try {
				//关闭流
				reader.close();
				fis.close();
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
	}

}

2、OutputStreamWriter

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;

public class Test {


	public static void main(String[] args) {
		// TODO Auto-generated method stub
	
		FileOutputStream fos = null;
		BufferedWriter writer = null;
		
		try {
			String content = "要写出的内容";
			fos = new FileOutputStream("d:\\text2.txt");
			//转换流OutputStreamWriter
			writer = new BufferedWriter(new OutputStreamWriter(fos));
			writer.write(content);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally {
			try {
				//关闭流
				//注意关闭顺序,最后关闭FileOutputStream流,否则报错
				writer.close();
				fos.close();
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值