代码如下:


  1. import java.io.FileWriter;
  2. import java.io.IOException;
  3. import java.io.RandomAccessFile;
  4. public class AppendToFile {
  5. /**
  6. * A方法追加文件:使用RandomAccessFile
  7. */
  8. public static void appendMethodA(String fileName, String content) {
  9. try {
  10. // 打开一个随机访问文件流,按读写方式
  11. RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");
  12. // 文件长度,字节数
  13. long fileLength = randomFile.length();
  14. //将写文件指针移到文件尾。在该位置发生下一个读取或写入操作。
  15. randomFile.seek(fileLength);
  16. //按字节序列将该字符串写入该文件。
  17. randomFile.writeBytes(content);
  18. //关闭此随机访问文件流并释放与该流关联的所有系统资源。
  19. randomFile.close();
  20. } catch (IOException e) {
  21. e.printStackTrace();
  22. }
  23. }
  24. /**
  25. * B方法追加文件:使用FileWriter
  26. */
  27. public static void appendMethodB(String fileName, String content) {
  28. try {
  29. //打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件,如果为 true,则将字节写入文件末尾处,而不是写入文件开始处
  30. FileWriter writer = new FileWriter(fileName, true);
  31. writer.write(content);
  32. writer.close();
  33. } catch (IOException e) {
  34. e.printStackTrace();
  35. }
  36. }
  37. public static void main(String[] args) {
  38. String fileName = "C:/Temp.txt";
  39. String content = "new append!";
  40. //按方法A追加文件
  41. AppendToFile.appendMethodA(fileName, content);
  42. AppendToFile.appendMethodA(fileName, "append end. \n");
  43. //显示文件内容
  44. ReadFromFile.readFileByLines(fileName);
  45. //按方法B追加文件
  46. AppendToFile.appendMethodB(fileName, content);
  47. AppendToFile.appendMethodB(fileName, "append end. \n");
  48. //显示文件内容
  49. ReadFromFile.readFileByLines(fileName);
  50. }
  51. }

java控制台输出结果如下:


  1. ++++++readFileByLines:++++++
  2. 以行为单位读取文件内容,一次读一整行:
  3. line 1: Sun Yat-sen(November 12, 1866–March 12, 1925) was a Chinese revolutionary and political leader who is often referred to as the "father of modern China". Sun played an instrumental and leadership role in the eventual overthrow of the Qing Dynasty in 1911. He was the first provisional president when the Republic of China was founded in 1912. He later co-founded the Kuomintang (KMT) where he served as its first leader. new append!append end.
  4. ++++++readFileByLines:++++++
  5. 以行为单位读取文件内容,一次读一整行:
  6. line 1: Sun Yat-sen(November 12, 1866–March 12, 1925) was a Chinese revolutionary and political leader who is often referred to as the "father of modern China". Sun played an instrumental and leadership role in the eventual overthrow of the Qing Dynasty in 1911. He was the first provisional president when the Republic of China was founded in 1912. He later co-founded the Kuomintang (KMT) where he served as its first leader. new append!append end.
  7. line 2: new append!append end.

java写文件时往末尾追加文件(而不是覆盖原文件),的两种方法总结的更多相关文章

  1. 用java写一个servlet,可以将放在tomcat项目根目录下的文件进行下载

    用java写一个servlet,可以将放在tomcat项目根目录下的文件进行下载,将一个完整的项目进行展示,主要有以下几个部分: 1.servlet部分   Export 2.工具类:TxtFileU ...

  2. 安装Ruby、Sass在WebStrom添加Watcher实现编辑scss文件时自动生成.map和压缩后的.css文件

    前言 这段时间一直在看Bootstrap,V3官方直接提供了Less版本的源码,就先将Less学完了,很简单的语法,学习写Demo都是在Webstorm里写的,配置了Watcher自动编译(详见< ...

  3. 合并BIN文件的两种方法(转)

    源:http://blog.chinaunix.net/uid-20745340-id-1878803.html 合并BIN文件的两种方法 在单片机的开发过程中,经常需要将两个单独的BIN文件合并成一 ...

  4. Python修改文件的两种方法

    目录: 一.以占用内存的方式修改文件 二.以占用硬盘的方式修改文件 引言 文件修改的方法从操作方式上大致可以分为两类,一种是以占用电脑内存的方式,将文件读取到内存中修改再存回硬盘:第二种方法是分别打开 ...

  5. [转载]C#读写txt文件的两种方法介绍

    C#读写txt文件的两种方法介绍 by 大龙哥 1.添加命名空间 System.IO; System.Text; 2.文件的读取 (1).使用FileStream类进行文件的读取,并将它转换成char ...

  6. MySQL命令执行sql文件的两种方法

    MySQL命令执行sql文件的两种方法 摘要:和其他数据库一样,MySQL也提供了命令执行sql脚本文件,方便地进行数据库.表以及数据等各种操作.下面笔者讲解MySQL执行sql文件命令的两种方法,希 ...

  7. C#读写txt文件的两种方法介绍

    C#读写txt文件的两种方法介绍 1.添加命名空间 System.IO; System.Text; 2.文件的读取 (1).使用FileStream类进行文件的读取,并将它转换成char数组,然后输出 ...

  8. 自制按钮图标的两种方法: image sprite和svg字体文件

    用image sprite和svg字体文件这两种方法,都能够极大地减少小图形文件的数量, 从而减少服务器请求和带宽需求.提高网页的响应速度. 一.建立SVG字体文件 iconmoon 是一个在线工具, ...

  9. elf格式转换为hex格式文件的两种方法

    这周工作终于不太忙了,可以写点笔记总结一下了. 之前的文章如何在Keil-MDK开发环境生成Bin格式文件,介绍了如何在Keil开发环境使用fromelf软件,将生成的axf文件转换为bin文件,这次 ...

随机推荐

  1. 第二次作业:对Github的初步学习应用(四则运算的自动生成C#实现)

    GIT地址  https://github.com/Anzerl?tab=repositories GIT用户名  Anzerl 学号后五位  062426 博客地址  https://www.cnb ...

  2. Searching with regular sentences will only get you so far – if you need to find something a bit tricky turn to these advanced yet simple methods--转

    原文地址:http://www.theguardian.com/technology/2016/jan/15/how-to-use-search-like-a-pro-10-tips-and-tric ...

  3. 【AtCoder ABC 075 C】Bridge

    [链接] 我是链接,点我呀:) [题意] 让你求出桥的个数 [题解] 删掉这条边,然后看看1能不能到达其他所有的点就可以了 [代码] #include <bits/stdc++.h> us ...

  4. [RxJS] Subject: an Observable and Observer hybrid

    This lesson teaches you how a Subject is simply a hybrid of Observable and Observer which can act as ...

  5. Apache与weblogic整合实战(独家研究)

    用apache来处理外界的请求,再把请求转发给wls,这样就行突破wls express版本号的5用户限制 详细配置例如以下 copy ${WLS_Server}/server/lib下的mod_wl ...

  6. 调试 之gdb thread命令 与 ltrace/strace

    我们可以通过  1)  gdb prog_name -> r               用在逐步调试自己的程序时 2)  gdb -> attach process_id       正 ...

  7. GDB(十)--调试正在运行的进程

    我编写了一个循环: long i;    for (i = 0; i < 999999; i++) {        mt.a += 1;        sleep(1);    }把它编译成a ...

  8. JavaEE 技术选型建议,server配置,部署策略

    基础设施环境 # 总体採用 centos6.5 + nginx + tomcat7.0 负载均衡:nginx 配置,使用 nginx 作为负载均衡.权重配置. 在web层做到水平扩展. 以及配置日志格 ...

  9. Android中的动画详解系列【4】——Activity之间切换动画

    前面介绍了Android中的逐帧动画和补间动画,并实现了简单的自定义动画,这一篇我们来看看如何将Android中的动画运用到实际开发中的一个场景--Activity之间跳转动画. 一.定义动画资源 如 ...

  10. css3背景透明文字不透明

    在 FF/Chrome 等较新的浏览器中可以使用css属性background-color的rgba轻松实现背景透明,而文字保持不透明.而IE6/7/8浏览器不支持rgba,只有使用IE的专属滤镜fi ...