字符流复制文件

package com.test1;

import java.io.FileReader;
import java.io.FileWriter;

public class Test2 {
	/*
	 * 使用字符流复制文本文件
	 */
	public static void main(String[] args) throws Exception {
		// 创建字符输入流对象
		FileReader fr = new FileReader("heihei.txt");
		// 创建字符输出流对象
		FileWriter fWriter = new FileWriter("xixi.txt");
		// 一次读写一个字符
		/*
		 * while((ch=fr.read())!=-1){ fw.write(chs,0,len); fw.flush(); }
		 */

		// 一次读写一个字符数组
		int len;// 用于存储读到的字符个数
		char[] chs = new char[1024];
		while ((len = fr.read(chs)) != -1) {
			fWriter.write(chs, 0, len);
			fWriter.flush();
		}
		// 释放资源
		fWriter.close();
		fr.close();
	}

}

猜你喜欢

转载自blog.csdn.net/weixin_42591732/article/details/94282889
今日推荐