当前位置: 技术问答>java相关
写文件的问题.
来源: 互联网 发布时间:2015-07-25
本文导语: 如何把一个字符串添加到文件的末尾并且新建一行.谢谢! | try: StringBuffer txtConcent = new StringBuffer() ; String sSrcName="c:\test.txt"; txtConcent.append("abcn") ; try { PrintWriter pw = new PrintWriter(new Fil...
如何把一个字符串添加到文件的末尾并且新建一行.谢谢!
|
try:
StringBuffer txtConcent = new StringBuffer() ;
String sSrcName="c:\test.txt";
txtConcent.append("abcn") ;
try
{
PrintWriter pw = new PrintWriter(new FileWriter(sSrcName, true), true);
pw.println(txtConcent.toString());
}
catch (Exception e)
{
System.out.println("error");
}
}
StringBuffer txtConcent = new StringBuffer() ;
String sSrcName="c:\test.txt";
txtConcent.append("abcn") ;
try
{
PrintWriter pw = new PrintWriter(new FileWriter(sSrcName, true), true);
pw.println(txtConcent.toString());
}
catch (Exception e)
{
System.out.println("error");
}
}
|
打开文件是可以用添加模式
FileWriter writer =new FileWriter("file.txt",true)
~~~~~
最后的 true 表示写入的内容将被添加在原有文件之后
FileWriter writer =new FileWriter("file.txt",true)
~~~~~
最后的 true 表示写入的内容将被添加在原有文件之后
|
public void writeToFile(String filename, String msg, boolean append){
PrintWriter out;
//String filename = filename;
try{
out = new PrintWriter(new BufferedWriter(new FileWriter(filename, true)), append);
out.println(msg);
}catch(IOException ioe){
System.out.println(ioe);
}
}
writeToFile("Hello World!n", "c:\out.txt", true);