码迷,mamicode.com
首页 > 编程语言 > 详细

Java 写数据到文件

时间:2018-08-17 17:12:33      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:logger   文件中   private   false   bean   webroot   cat   lse   debug   

 

private boolean writeToFile(BusGpsBean gpsBean) {

String dataStr = DateUtil.date2String(new Date(), DateUtil.YMD);
String filePath = PathKit.getWebRootPath() + File.separatorChar + "writeFile" + File.separatorChar + dataStr + ".txt"; // 文件保存绝对路径
logger.debug("dataStr, {}", dataStr);
logger.debug("filePath, {}", filePath);

File file = null;
FileWriter fw = null;
file = new File(filePath);
try {
if (!file.exists()) {
//file.getParentFile().mkdirs();
file.createNewFile();
}
fw = new FileWriter(file, true); // true表示追加
fw.write(gpsBean.toString());//向文件中写内容
fw.write("\r\n");//换行
fw.flush();
logger.debug("写数据到文件成功, {}", gpsBean.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}finally{
if(fw != null){
try {
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

return true;
}

Java 写数据到文件

标签:logger   文件中   private   false   bean   webroot   cat   lse   debug   

原文地址:https://www.cnblogs.com/panchanggui/p/9494226.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!