zoukankan      html  css  js  c++  java
  • java文件末尾追加内容的两种方式

    java 开发中,偶尔会遇到在文件末尾对文件内容进行追加,实际上有多种方式可以实现,简单介绍两种:

    一种是通过RandomAccessFile类实现,另一种是通过FileWriter类来实现。

    实现方法很简单,没什么可多说的,直接看代码:

     1     public static void main(String[] args) throws Exception {
     2         File file=new File(filename);
     3         RandomAccessFile raf=new RandomAccessFile(file, "rw");
     4          //将写文件指针移到文件尾。
     5          raf.seek(raf.length());
     6          raf.writeBytes("first test RandomAccessFile append 
    ");
     7          raf.close();
     8          FileWriter writer = new FileWriter(file, true);
     9          writer.write("test RandomAccessFile append 
    ");
    10          writer.close();
    11          raf=new RandomAccessFile(file, "rw");
    12          //将写文件指针移到文件尾。
    13          raf.seek(raf.length());
    14          raf.writeBytes("second test RandomAccessFile append 
    ");
    15          raf.close();
    16     }

    最后查看文件内容:

    first test RandomAccessFile append 
    test RandomAccessFile append 
    second test RandomAccessFile append 

    就这么简单。。。。。

  • 相关阅读:
    可执行程序的装载
    stdafx.h的作用
    AI调色板
    3ds max输出图片
    3ds max移除几何体的线段
    3ds max删除了对象后,还是将原来所有对象输出的原因
    vs win32 & MFC 指针默认位置
    3ds max 分离对象
    PDF
    endnote设置文献第二行悬挂缩进办法
  • 原文地址:https://www.cnblogs.com/jessezeng/p/5666716.html
Copyright © 2011-2022 走看看