IO流练习

public class Test1 {

	/**
	 * @param args
	 * 复制一个文本文件
	 * 明确源:FileReader,明确目的:FileWriter
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		FileReader fr=new FileReader("E:\\test.txt");
		FileWriter fw=new FileWriter("E:\\test_copy1.txt");
		char[] ch=new char[1024];
		while((fr.read(ch))!=-1){
			fw.write(new String(ch));
			fw.flush();
		}
		fw.close();
		fr.close();
		
	}

}



public class Test2 {

	/**
	 * @param args
	 * 读取键盘录入信息,并写入到一个文件中
	 * 明确源:System.in 明确目的:FileWriter
	 * 输入时,需要将字节转成字符
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		FileWriter fw=new FileWriter("E:\\test2.txt");
		String line=null;
		while((line=br.readLine())!=null){
			if("over".equals(line))
				break;
			fw.write(line);
			fw.write("\r\n");
			fw.flush();
		}
	}


public class Test3 {

	/**
	 * @param args
	 * 将一个文本数据显示在控制台
	 * 明确源:FileReader, 明确目的:System.out
	 * 需要将字符流转成字节流
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		FileReader fr=new FileReader("E:\\test2.txt");
		BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out));
		StringBuilder sb=new StringBuilder();
		int ch=-1;
		while((ch=fr.read())!=-1){
			if(ch=='\r'){
				continue;
			}
			if(ch=='\n'){
				bw.write(new String(sb.toString()));
				bw.newLine();
				bw.flush();
				sb.delete(0, sb.length());
			}
			else{
				sb.append((char)ch);
			}
		}
	}

}


public class Test4 {

	/**
	 * @param args
	 * 读取键盘录入数据,显示在控制台
	 * 明确源System.in;明确目的System.out
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out));
		String line=null;
		while((line=br.readLine())!=null){
			if("over".equals(line)){
				break;
			}
			bw.write(line);
			bw.newLine();
			bw.flush();
		}
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值