课后练习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();
}
}
}
高级特性 第3章
最新推荐文章于 2019-08-17 16:11:48 发布