java IO--字符流的输入输出

/**
 * ClassName: CharStreamDemo 
 * 字符流:
 * 字符输出流:Writer,对文件的操作 使用子类:FileWriter
 * 字符输入流:Reader,对文件的操作使用子类:FileReader
 * 每次操作的单位是一个字符
 * 内部实现还是字节流
 * 文件字符操作流会自带缓存,默认大小为1024字节,在缓存满后,或手动刷新缓存,或关闭流时会把数据写入文件
 * 
 * 操作非文本文件时使用字节流,操作文本文件时使用字符流方便一些
 * @author cai
 * @date 2018年10月16日 
 */
public class CharStreamDemo {

	private static void out() {
		File file = new File("C:\\Users\\Desktop\\需要被删的\\ffff\\新建文本文档.txt");
		try {
			Writer out = new FileWriter(file,true);
			out.write("\\n明天上证指数100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private static void in() {
		File file1 = new File("C:\\Users\\Desktop\\需要被删的\\ffff\\新建文本文档.txt");
		try {
			Reader in = new FileReader(file1);
			char[] cs = new char[1];
			int len = -1;
			StringBuilder buf= new StringBuilder();
			while((len = in.read(cs))!=-1) {
				buf.append(new String(cs,0,len));
			}
			in.close();
			System.out.println(buf);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public static void main(String[] args) {
		out();
		in();
	}
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值