File file=new File("f:/a.txt");
BufferedWriter bw=null;
try {
bw=new BufferedWriter(new FileWriter(file,true));
bw.write("efg");
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
这里关键代码bw=new BufferedWriter(new FileWriter(file,true));
后面参数的true,就代表即使a.txt里面有内容,也不会替换。
/**
* @param args
*/
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = null;
PrintWriter printWriter = null;
try{
bufferedReader = new BufferedReader(new FileReader("text1.txt"));
printWriter = new PrintWriter(new FileWriter("text.txt", true));
String l;
while ((l = bufferedReader.readLine()) != null) {
printWriter.print(l);
}
} finally {
if (bufferedReader != null) {
bufferedReader.close();
}
if (printWriter != null) {
printWriter.close();
}
}
}
希望对你有所帮助
FileOutputStream(File file, boolean append)
Creates a file output stream to write to the file represented by the specified File object.