高级特性 第3章

课后练习3:
package a3;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

public class C3 {
	public static void main(String[] args) {
		File file = null;
		FileReader reader = null;
		FileWriter writer = null;
		BufferedReader r = null;
		BufferedWriter w = null;
		StringBuffer str = new StringBuffer();
		try {
			file = new File("C:\\source.txt");
			reader = new FileReader(file);
			r = new BufferedReader(reader);
			String line;
			while((line=r.readLine())!=null) {
				str.append(line);
			}
			System.out.println(str);
			System.out.println(file.length());
			file = new File("D:\\target.txt");
			writer = new FileWriter(file);
			w = new BufferedWriter(writer);
			w.write(str.toString());
			while((line=r.readLine())!=null) {
				str.append(line);
			}
			System.out.println(str);
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				w.close();
				r.close();
				writer.close();
				reader.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	}
}

课后练习4:
package a3;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;

public class C4 {
	public static void main(String[] args) {
		FileReader reader = null;
		BufferedReader bf = null;
		StringBuffer sb = new StringBuffer();
		try {
			reader = new FileReader("C:\\Windows\\win.ini");
			bf = new BufferedReader(reader);
			String line;
			while((line=bf.readLine())!=null) {
				sb.append(line);
			}
			System.out.println(sb);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值