import java.io.*;
public class T12{
public static void main(String[] args) throws Exception{
File fDir = new File("D:\\java\\");
if(!fDir.exists()){
fDir.mkdirs();
}
File f=new File(fDir,"123.txt");
if(!f.exists()){
f.createNewFile();
}
String[] str=new String[10];
for(int i=0;i<10;i++){
FileWriter fw=new FileWriter(f);
BufferedWriter bw=new BufferedWriter(fw);
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
str[i]=br.readLine();
bw.write(str[i]);
bw.newLine();
bw.flush();
bw.close();
fw.close();
}
}
}
我本来是这样写的,想通过字符数组来逐行写进文本里,结果发现每一行写进去之后,都会覆盖前一行的内容……
Java String BufferedWriter
------解决方案--------------------
FileWriter fw=new FileWriter(f,true);