如何在原有的文件上追加

解决方案 »

  1.   

    FileWriter fos = new FileWriter("FileOutputStream.dat",true);
      

  2.   

    C++里我会这样写:#include <fstream>ofstream writeFile("aaa.txt");
    ifstream readFile("aaa.txt",ios::binary);
    int count=0;
    char * buf= new char[100]; //这是将来写入文件末尾的内容if(readFile.good())
    {
     while(!readFile.eof())
     {
      count++;
      readFile.get();
     } writeFile.seekp(count-1); //这一句是将文件的指针移动到文件最后
     for(int i=0; i<100; i++)
     {
      writeFile<<buf[i];
      writeFile.seekp(++count);
     }
    }else
     cout<<"The File may be crushed or not exist";以上程序最近写过,记得比较清楚;以前用java也写过完全一样的,不过不记得了,原理基本一样吧,都是移动文件指针,然后在输出。 
      

  3.   

    import java.io.*;
    public class MyTest
    {
    public static void main(String []args)
    {
    try
    {
     RandomAccessFile raf=new RandomAccessFile("1.txt","rw");
     long length=raf.length();
     raf.seek(length-1);
     raf.write("您好,认识你很高兴".getBytes());
     raf.close();
    }
    catch(Exception e)
    {
    System.out.println(e);
    }
    }
    }