Java追加写入文件方法
2015-12-31 18:28
441 查看
方法一:
方法二:(建议)
方法三:
public void method1() { FileWriter fw = null; try { //如果文件存在,则追加内容;如果文件不存在,则创建文件 File f=new File("E:\\dd.txt"); fw = new FileWriter(f, true); } catch (IOException e) { e.printStackTrace(); } PrintWriter pw = new PrintWriter(fw); pw.println("追加内容"); pw.flush(); try { fw.flush(); pw.close(); fw.close(); } catch (IOException e) { e.printStackTrace(); } }
方法二:(建议)
public static void method2(String file, String conent) { BufferedWriter out = null; try { out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true))); out.write(conent+"\r\n"); } catch (Exception e) { e.printStackTrace(); } finally { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } }
方法三:
public static void method3(String fileName, String content) { try { // 打开一个随机访问文件流,按读写方式 RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw"); // 文件长度,字节数 long fileLength = randomFile.length(); // 将写文件指针移到文件尾。 randomFile.seek(fileLength); randomFile.writeBytes(content+"\r\n"); randomFile.close(); } catch (IOException e) { e.printStackTrace(); } } }
相关文章推荐
- 记JAVA的二三事(4)——线程的同步和互斥
- Java四舍五入保留两位小数方法
- spring 加载bean过程源码简易解剖(转载)
- java 自定义单向链表实现“反序”输出
- java 定时器的几种实现方式以及 配置参数的说明
- java的synchronized总结
- java成神之路
- java正则匹配字符串中不包含一个或几个特定的字符串
- Spring Boot学习笔记-错误处理及自定义
- eclipse提交项目到github
- /** * Describe: * User: fanweixiao * Date: ${date} * Time: ${time} */
- MyEclipse 搭建webservice (axis1.4)
- Java中List集合的遍历
- 【Java学习笔记】——验证码
- java中Date与String的相互转化
- java设计模式----备忘录模式
- Struts2 标签库讲解
- struts2常用标签详解
- JVM学习笔记(二)------Java代码编译和执行的整个过程
- springMVC dubbo zookeeper整合