import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
/**
* 文件写入
* @author Administrator
*
*/
public class TestReadAndWrite { public static void writerText(String path, String content) { File dirFile = new File(path); if (!dirFile.exists()) {//判断目录是否存在,不存在创建
dirFile.mkdir();
} try {
//new FileWriter(path + "config.log", true) 设置true 在不覆盖以前文件的基础上继续写
BufferedWriter writer = new BufferedWriter(new FileWriter(path + "config.log", true));
writer.write(content+"\r\n");//写入文件
writer.flush();//清空缓冲区数据
writer.close();//关闭读写流
System.out.println("写入成功");
} catch (IOException e) {
e.printStackTrace();
}
} public static void main(String[] args) {
writerText("D:/", "2014-07-14 23:43:00 服务器IP:192.123.123.243 日志信息:后台登录验证码出现错误,错误代码500");
}
}

写入文件换行方法:

一、windows和linux/unix平台中没有通用的换行符的,针对常用的系统,可以使用如下的转义符实现换行:
windows下的文本文件换行符:\r\n

linux/unix下的文本文件换行符:\r

Mac下的文本文件换行符:\n

二、BufferedWriter的newline()方法,可通用:

BufferedWriter writer = new BufferedWriter(new FileWriter(path + "config.log", true));
writer.write(content);//写入文件
writer.newLine();//换行
writer.flush();//清空缓冲区数据
writer.close();//关闭读写流

三、System.getProperty()方法

BufferedWriter writer = new BufferedWriter(new FileWriter(path + "config.log", true));
writer.write(content+System.getProperty("line.separator"));//写入文件***System.getProperty("line.separator")<--换行
writer.flush();//清空缓冲区数据
writer.close();//关闭读写流

最新文章

  1. UWP消息通知
  2. POJ 3233 Matrix Power Series(构造矩阵求等比)
  3. HDU 3790 最短路径问题(SPFA || Dijkstra )
  4. xml处理相关文章收藏
  5. bzoj 1196: [HNOI2006]公路修建问题 二分+并查集
  6. Android线程之基本用法
  7. org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException
  8. 每个java初学者都应该搞懂的问题
  9. HIVE的常用操作(HQL)语句
  10. 字典---python
  11. 平衡树Treap
  12. sha256加密
  13. python 简单的串口收发数据
  14. Cross-origin plugin content from must have a visible size larger than 400 x 300 pixels, or it will be blocked. Invisible content is always blocked.
  15. MySQL最优配置模板( 5.6&amp;5.7转)
  16. 2.1 uml序言
  17. js 键盘事件
  18. 什么是EPEL 及 Centos上安装EPEL
  19. 求解1^2+2^2+3^2+4^2+...+n^2的方法(求解1平方加2平方加3平方...加n平方的和)
  20. access&#160;join形式删除数据

热门文章

  1. JDK1.3安装出现/lib/ld-linux.so.2: bad ELF interpreter: No such file or directory Done.
  2. python之数据库操作
  3. WinForm:DataGridViewButtonColumn的使用
  4. 问题解决——MFC Ribbon 响应函数 错乱 执行其他函数
  5. [Java入门笔记] 面向对象三大特征之:继承
  6. 转载:SqlServer数据库性能优化详解
  7. BOM浏览器对象模型
  8. C/C++实践笔记 005
  9. angularjs + fis +modJS 对于支持amd规范的组建处理(PhotoSwipe 支持,百度webUpload支持)
  10. 使用PhpDocumentor生成文档