1. import java.io.BufferedWriter;
  2. import java.io.FileOutputStream;
  3. import java.io.FileWriter;
  4. import java.io.IOException;
  5. import java.io.OutputStreamWriter;
  6. import java.io.RandomAccessFile;
  7. /**
  8. * 描述:追加内容到文件末尾
  9. * @author Administrator
  10. *
  11. */
  12. public class WriteStreamAppend {
  13. /**
  14. * 追加文件:使用FileOutputStream,在构造FileOutputStream时,把第二个参数设为true
  15. *
  16. * @param fileName
  17. * @param content
  18. */
  19. public static void method1(String file, String conent) {
  20. BufferedWriter out = null;
  21. try {
  22. out = new BufferedWriter(new OutputStreamWriter(
  23. new FileOutputStream(file, true)));
  24. out.write(conent);
  25. } catch (Exception e) {
  26. e.printStackTrace();
  27. } finally {
  28. try {
  29. out.close();
  30. } catch (IOException e) {
  31. e.printStackTrace();
  32. }
  33. }
  34. }
  35. /**
  36. * 追加文件:使用FileWriter
  37. *
  38. * @param fileName
  39. * @param content
  40. */
  41. public static void method2(String fileName, String content) {
  42. try {
  43. // 打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
  44. FileWriter writer = new FileWriter(fileName, true);
  45. writer.write(content);
  46. writer.close();
  47. } catch (IOException e) {
  48. e.printStackTrace();
  49. }
  50. }
  51. /**
  52. * 追加文件:使用RandomAccessFile
  53. *
  54. * @param fileName
  55. *            文件名
  56. * @param content
  57. *            追加的内容
  58. */
  59. public static void method3(String fileName, String content) {
  60. try {
  61. // 打开一个随机访问文件流,按读写方式
  62. RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");
  63. // 文件长度,字节数
  64. long fileLength = randomFile.length();
  65. // 将写文件指针移到文件尾。
  66. randomFile.seek(fileLength);
  67. randomFile.writeBytes(content);
  68. randomFile.close();
  69. } catch (IOException e) {
  70. e.printStackTrace();
  71. }
  72. }
  73. public static void main(String[] args) {
  74. System.out.println("start");
  75. method1("c:/test.txt", "追加到文件的末尾");
  76. System.out.println("end");
  77. }

java追加文件的几种方式的更多相关文章

  1. 【文件下载】Java下载文件的几种方式

    [文件下载]Java下载文件的几种方式  摘自:https://www.cnblogs.com/sunny3096/p/8204291.html 1.以流的方式下载. public HttpServl ...

  2. Java读写文件的几种方式

    自工作以后好久没有整理Java的基础知识了.趁有时间,整理一下Java文件操作的几种方式.无论哪种编程语言,文件读写操作时避免不了的一件事情,Java也不例外.Java读写文件一般是通过字节.字符和行 ...

  3. java复制文件的4种方式

    尽管Java提供了一个可以处理文件的IO操作类.但是没有一个复制文件的方法.复制文件是一个重要的操作,当你的程序必须处理很多文件相关的时候.然而有几种方法可以进行Java文件复制操作,下面列举出4中最 ...

  4. [JAVA]java复制文件的4种方式

    尽管Java提供了一个可以处理文件的IO操作类. 但是没有一个复制文件的方法. 复制文件是一个重要的操作,当你的程序必须处理很多文件相关的时候. 然而有几种方法可以进行Java文件复制操作,下面列举出 ...

  5. Java读取文件的几种方式

    package com.mesopotamia.test; import java.io.BufferedReader; import java.io.ByteArrayInputStream; im ...

  6. Java下载文件的几种方式

    转发自博客园Sunny的文章 1.以流的方式下载 public HttpServletResponse download(String path, HttpServletResponse respon ...

  7. java 下载文件的两种方式和java文件的上传

    一:以网络的方式下载文件 try { // path是指欲下载的文件的路径. File file = new File(path); // 以流的形式下载文件. InputStream fis = n ...

  8. java遍历文件夹两种方式

    1:非递归方式(有点类似二叉树的非递归遍历,采用链表来存储遍历到的文件夹,如果是文件就直接输出) public void traverseFile(String path){ File[] files ...

  9. java读取文件的几种方式性能比较

    //普通输入流读取文件内容 public static long checksumInputStream(Path filename) { try(InputStream in= Files.newI ...

随机推荐

  1. 黑马程序员——经典C语言程序设计100例

    1.数字排列 2.奖金分配问题 3.已知条件求解整数 4.输入日期判断第几天 5.输入整数进行排序 6.用*号显示字母C的图案 7.显示特殊图案 8.打印九九口诀 9.输出国际象棋棋盘 10.打印楼梯 ...

  2. 第一个自定义HTML网页

    前言 已经好几天没玩LOL了,实在手痒,下载了游戏,又卸载了,坦言:毛爷爷说的”好好天天向上“,真不容易.但还是回到学习个状态了,开始写,就“根本停不下来”,我也慢慢感受到代码的快乐了,并且想总结出一 ...

  3. 分布式文件系统--GFS

    分布式文件系统 分布式文件系统:当数据集的大小超过一台独立物理计算机的存储能力时,就有必要对它进行分区(partition)并存储到若干台单独的计算机上.管理网络中夸多台计算机存储的文件系统.这种系统 ...

  4. show index 之Cardinality (mysql)

    show index  之Cardinality 官方文档的解释: Cardinality An estimate of the number of unique values in the inde ...

  5. C++ 之关联容器 map

    标准库定义了四种关联容器:map是其中之一(另外还有set.multimap.multiset).map的元素以键-值(key-value),在学了顺序容器之后,再学习关联容器,就比较比较好理解了. ...

  6. hdu 5311 Hidden String (BestCoder 1st Anniversary ($))(深搜)

    http://acm.hdu.edu.cn/showproblem.php?pid=5311 Hidden String Time Limit: 2000/1000 MS (Java/Others)  ...

  7. Cisco Router WEB管理

    目前市场上很多思科路由器或者交换机都可以通过WEB方式配置.尽管很多功能还是只能通过CLI配置,但是一些功能还是很有用的,例如端口的流量监控功能 前期准备: 一.设备的IOS要支持WEB管理功能   ...

  8. LeetCode292:Nim Game

    You are playing the following Nim Game with your friend: There is a heap of stones on the table, eac ...

  9. CodeForces 711B Chris and Magic Square (暴力,水题)

    题意:给定n*n个矩阵,其中只有一个格子是0,让你填上一个数,使得所有的行列的对角线的和都相等. 析:首先n为1,就随便填,然后就是除了0这一行或者这一列,那么一定有其他的行列是完整的,所以,先把其他 ...

  10. Max retries exceeded with url

    78 Traceback (most recent call last):   File "thread072413.py", line 163, in <module> ...