2012-11-01 215 views
1

可能重复:
How to append text to an existing file in Java如何将文本添加到java中的文本文件?

我想将数据添加到一个文本文件中。所以这是一个又一个......所以像:

1 
2 
3 
<add more here> 

但我不希望从文件中的文本在所有被删除。这是我使用atm的代码,但它取代了文件中的任何内容。有人可以告诉我怎么做我问的。谢谢。

FileWriter fstream = new FileWriter("thefile.txt"); 
    BufferedWriter out = new BufferedWriter(fstream); 
    out.write("blabla"); 
    out.close(); 
     }catch (Exception e){//Catch exception if any 
    System.err.println("Error: " + e.getMessage()); 
    } 
} 
+1

http://stackoverflow.com/questions/1625234/how-to-append-text-to-an-existing-file-in-java – anon

回答

6

使用本

FileWriter fstream = new FileWriter("thefile.txt",true); 

解释

public FileWriter(String fileName, boolean append) throws IOException 

构造给出一个布尔值,指示是否要写入的数据附加文件名的FileWriter对象。

+0

修复了删除问题,谢谢。我可以通过执行out.write(“\ n”+“blabla”)来做新的事情。 – Butterflycode

相关问题