JAVA_IO_字符流操作文本文件

1.纯文本读取
package test.chario;

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

/**
 * 纯文本读取
 * @author WL20180732
 *
 */
public class Demo01 {
	public static void main(String[] args) {
		// 创建源
		File src = new File("E:/xp/test/a.txt");
		// 选择流
		Reader reader = null;
		try {
			reader = new FileReader(src);
			// 读取操作
			char[] flush = new char[1024];
			int len = 0;
			while (-1 != (len = reader.read(flush))) {
				// 字符数组转成 字符串
				String str = new String(flush, 0, len);
				System.out.println(str);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			System.out.println("源文件不存在");
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println("文件读取失败");
		} finally {
			try {
				if (null != reader) {
					reader.close();
				}
			} catch (Exception e2) {
			}
		}
	}
}

2.写出文件
package test.chario;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

/**
 * 写出文件
 * @author WL20180732
 *
 */
public class Demo02 {
	public static void main(String[] args) {
		// 创建源
		File dest = new File("e:/xp/test/char.txt");
		// 选择流
		Writer wr = null;
		try {
			// 追加文件,而不是覆盖文件
			wr = new FileWriter(dest);
			// 写出
			String msg = "追加.....锄禾日当午\r\n码农真辛苦\r\n一本小破书\r\n一读一上午";
			wr.write(msg);
			wr.append("倒萨发了看电视剧 ");

			wr.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (null != wr) {
					wr.close();
				}
			} catch (Exception e2) {
			}
		}
	}
}

3.使用字符流实现文件复制
package test.chario;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;

/**
 * 纯文本拷贝
 * @author WL20180732
 *
 */
public class CopyFileDemo {
	public static void main(String[] args) {
		// 创建源 仅限于 字符的纯文本
		File src = new File("E:/xp/test/Demo03.java");
		File dest = new File("e:/xp/test/char.txt");
		// 选择流
		Reader reader = null;
		Writer wr = null;
		try {
			reader = new FileReader(src);
			wr = new FileWriter(dest);
			// 读取操作
			char[] flush = new char[1024];
			int len = 0;
			while (-1 != (len = reader.read(flush))) {
				wr.write(flush, 0, len);
			}
			wr.flush();// 强制刷出
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			System.out.println("源文件不存在");
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println("文件读取失败");
		} finally {
			try {
				if (null != wr) {
					wr.close();
				}
			} catch (Exception e2) {
			}
			try {
				if (null != reader) {
					reader.close();
				}
			} catch (Exception e2) {
			}
		}

	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值