如何在一个文本文件中指定的位置中写入数据~
比如
有这样一个文本数据
Nptsrc
SRCID(1), PTX(1), PTY(1), STKheight(1), STKdiam(1), STKbase(1)
SRCID(2), PTX(1), PTY(2), STKheight(2), STKdiam(2), STKbase(2)
Nspec
SPEC(1), WEIG(1)
Npttime
IBYR,IBDY,IBHR,IBSEC - IEYR,IEDY,IEHR,IESEC
TEMPK(1),VEXIT(1),QEMIT(1,2)创建如上的文本数据,现在想在这个文件的基础上再添加一些数据比如
Nptsrc
SRCID(1), PTX(1), PTY(1), STKheight(1), STKdiam(1), STKbase(1)
SRCID(2), PTX(1), PTY(2), STKheight(2), STKdiam(2), STKbase(2)
SRCID(3), PTX(1), PTY(3), STKheight(3), STKdiam(3), STKbase(3)//新增的文本数据
...............................................
这个如何实现?思路 或是类似的案例代码??
谢过咯~~

解决方案 »

  1.   

    你可以把该txt的内容全部读入内存,然后在内存中添加数据,在把修改好的数据写回原来的txt,就是覆盖掉。
      

  2.   

    获取插入位置再用以前数据加现在数据用append添加

    string strFilePath = @"F:\a.txt";         
        FileStream fs = new FileStream(strFilePath, FileMode.OpenOrCreate);
        byte[] byteArray = new byte[1024];
        fs.Write(byteArray, 0, byteArray.Length);
        fs.Close();
        FileStream fsA = new FileStream(strFilePath, FileMode.Open);
        byte[] byteA = System.Text.Encoding.Default.GetBytes("");
        fsA.Seek(150, SeekOrigin.Current);
        fsA.Write(byteA, 0, byteA.Length);
        fsA.Close();    FileStream fsB = new FileStream(strFilePath, FileMode.Open);
        byte[] byteB = System.Text.Encoding.Default.GetBytes("");
        fsB.Seek(10, SeekOrigin.Current);
        fsB.Write(byteB, 0, byteB.Length);
        fsB.Close();
      

  3.   

    用StringBuilder写几个函数控制,实现显示和数据的分离。每次添加只对自定义的数据操作,这样也行。