`
JenniferAmanda
  • 浏览: 144699 次
  • 性别: Icon_minigender_2
  • 来自: 西安
社区版块
存档分类
最新评论

写入文本文件

 
阅读更多
/**
* 写入文件
*
* @param sb
* @param fileName
*/
public static void saveToText(StringBuffer sb, String fileName) {
if (sb == null || fileName == null) {
return;
}
File batFile = new File(fileName);
if (!batFile.exists()) {
try {
batFile.createNewFile();
} catch (IOException e) {
Log.getlogger().error("创建文件失败,路径:" + fileName);
Log.exception(e);
return;
}
}
FileOutputStream fos = null;
PrintWriter pw = null;
try {
fos = new FileOutputStream(batFile);
pw = new PrintWriter(fos);
pw.write(sb.toString());
} catch (FileNotFoundException e) {
Log.getlogger().error("写文件失败,路径:" + fileName);
Log.exception(e);
return;
} finally {
if (pw != null) {
pw.flush();
pw.close();
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
Log.getlogger().error("写文件关闭流失败,路径:" + fileName);
Log.exception(e);
return;
}
}

}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics