字符流的简单例子(输入与输出)

这里向文本(.text)文件中输入数据和读取文本中的数据
写入:FileWriter

package c20190430;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class TestFileWriter {
	public static void main(String[] args) {
		String path = "E:\\新建文件夹\\test.txt";
		File file = new File(path);
		//输出流
		FileWriter fw=null;
		//缓冲流
		BufferedWriter bw=null;
		try {
			// 构造方法的true 表示不覆盖内容   默认是false 会覆盖原来的内容
			fw= new FileWriter(file,true);
			bw=new BufferedWriter(fw);
			//创建新行
			bw.newLine();
			bw.write("缓冲流00");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				//冲刷
				bw.flush();
				//关闭通道
				bw.close();
				fw.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
	}
}

写入文本数据如下
在这里插入图片描述

读取:FileReader

package c20190430;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class TestFileReader {
	public static void main(String[] args) {
		String path = "E:\\新建文件夹\\test.txt";
		File file = new File(path);
		FileReader fr = null;
		//缓冲流 Buffered
		//缓冲字符流
		BufferedReader br = null;
		try {
			//创建字符流对象
			fr =new FileReader(file);
			//将字符串转换成缓冲流对象
			br = new BufferedReader(fr);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			//读取一行
			String str = br.readLine();
			while(str!=null) {
				System.out.println(str);
				str = br.readLine();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}

读出文本内容如下
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

微微笑再加油

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值