java将字符串写入到文本里

来源:互联网 发布:c语言中函数和 编辑:程序博客网 时间:2024/05/22 00:51
public void writefile(String path,String content,boolean append)
{
BufferedWriter bw;
File writefile;
boolean addStr = append;
writefile = new File(path);
if(writefile.exists()==false)
{
try {
writefile.createNewFile();
writefile = new File(path);
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}


}
else
{
writefile.delete();
try {
writefile.createNewFile();
writefile = new File(path);
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
try {
FileWriter fw = new FileWriter(writefile,addStr);
bw = new BufferedWriter(fw);
fw.write(content);
fw.flush();
fw.close();

} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}


}


}