文件输入输出流的问题
尝试将一个文本文件复制到另一个文件 代码如下:
package test;
import java.io.*;
public class Into {
/**
* @param args
*/
public static void main(String[] args) {
try {
FileReader fr = new FileReader( "c:\\StaticMethod.java ");
BufferedReader br = new BufferedReader(fr);
String s;
while((s = br.readLine())!= null){
System.out.print(s);
}
FileWriter fw = new FileWriter( "c:\\static.java ");
BufferedWriter bw = new BufferedWriter(fw);
bw.write(s);
bw.flush();
br.close();
bw.close();
fr.close();
fw.close();
} catch (
FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (
IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
能都出来 但写不到另外一文件去 问题出在哪 请高人指点
------解决方案--------------------逻辑问题, 每读一行就应写一行, 而不是读完后再写
package test;
import java.io.*;
public class Into {
/**
* @param args
*/
public static void main(String[] args) {
try {
FileReader fr = new FileReader( "c:\\StaticMethod.java ");
BufferedReader br = new BufferedReader(fr);
String s;
FileWriter fw = new FileWriter( "c:\\static.java ");
BufferedWriter bw = new BufferedWriter(fw);
while((s = br.readLine())!= null){
bw.write(s);
bw.newLine();
System.out.print(s);
}
bw.flush();
br.close();
bw.close();
} catch (File
NotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}