第一题:
public class Copy {
public static void main(String[] args) {
File c=new File("C:\\Users\\Computer\\Desktop\\source.txt");
File d=new File("D:\\target.txt");
FileWriter fw=null;
FileReader fr=null;
BufferedWriter bw=null;
BufferedReader br=null;
String a=null;
StringBuffer sb=new StringBuffer();
try {
fr=new FileReader(c);
br=new BufferedReader(fr);
fw=new FileWriter(d);
bw=new BufferedWriter(fw);
while((a=br.readLine())!=null) {
bw.write(a);
//bw.newLine();
sb.append(a);
}
System.out.println(sb);
System.out.println(c.length()+"字节");
} catch (Exception e) {
// TODO: handle exception
System.out.println("没有此文件");
}finally {
try {
bw.close();
br.close();
fr.close();
fw.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(sb);
System.out.println(d.length()+"字节");
}
}
第二题:
public class Reader {
public static void main(String[] args) {
File f=new File("C:\\Windows\\win.ini");
FileReader fr = null;
BufferedReader br = null;
String str = null;
StringBuffer sb=new StringBuffer();
try {
fr = new FileReader(f);
br = new BufferedReader(fr);
while((str=br.readLine())!=null) {
sb.append(str);
}
System.out.println(sb);
System.out.println(f.length()+"字节");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
fr.close();
br.close();
} catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}
}
}
}