请教各位,怎样把数据(这是要写入"1"即可)写入文本文件中的指定位置.
谢谢!

解决方案 »

  1.   

    [C#] 
    using System;
    using System.IO;
    using System.Text;class Test 
    {
        
        public static void Main() 
        {
            string path = @"c:\temp\MyTest.txt";        // Delete the file if it exists.
            if (File.Exists(path)) 
            {
                File.Delete(path);
            }        //Create the file.
            using (FileStream fs = File.Create(path)) 
            {
                AddText(fs, "This is some text");
                AddText(fs, "This is some more text,");
                AddText(fs, "\r\nand this is on a new line");
                AddText(fs, "\r\n\r\nThe following is a subset of characters:\r\n");            for (int i=1;i < 120;i++) 
                {
                    AddText(fs, Convert.ToChar(i).ToString());                //Split the output at every 10th character.
                    if (Math.IEEERemainder(Convert.ToDouble(i), 10) == 0) 
                    {
                        AddText(fs, "\r\n");
                    }
                }
            }        //Open the stream and read it back.
            using (FileStream fs = File.OpenRead(path)) 
            {
                byte[] b = new byte[1024];
                UTF8Encoding temp = new UTF8Encoding(true);
                while (fs.Read(b,0,b.Length) > 0) 
                {
                    Console.WriteLine(temp.GetString(b));
                }
            }
        }    private static void AddText(FileStream fs, string value) 
        {
            byte[] info = new UTF8Encoding(true).GetBytes(value);
            fs.Write(info, 0, info.Length);
        }
    }
      

  2.   

    [C#]
    public override void Write(
       byte[] array,
       int offset,
       int count
    );参数
    array 
    字节所写入的数组。 
    offset 
    array 中的字节偏移量,从此处开始写入。 
    count 
    最多写入的字节数。 
      

  3.   

    全部取出来后处理再写回去。
    StreamReader sr = new StreamReader(@"C:\b.txt");
    System.Text.StringBuilder s = new System.Text.StringBuilder(sr.ReadToEnd());
    sr.Close(); int index = 0; if (index <= s.Length)
    s.Insert(index, "abc"); index = 2; if (index <= s.Length)
    s.Insert(index, "ddd"); StreamWriter sw = new StreamWriter(@"C:\b.txt");
    sw.Write(s.ToString());
    sw.Close();
      

  4.   

    楼上的
    offset 的字节偏移量
    指的是从待写入字节数组的索引,并不是文件流的索引,
    文件流指定位置可以用FileStream的
    Seek方法定位,不过FileStream的write方法是覆盖式的写法
    比如
    在  abcdefg的第三位置插入ddd
    变成abdddfg
      

  5.   

    /// <summary>
    /// 把Mac地址与客户数连在一起的20个字符放在10000个随机码中。
    /// </summary>
    /// <param name="amount">Mac地址与客户数连在一起的20个字符在10000个随机码中的位置</param>
    /// <param name="str">Mac地址与客户数连在一起的20个字符</param>
    /// <param name="oldstr">10000个随机码</param>
    private void RepaleString(int[] amount,char[] str,ref string oldstr)
    {
    string front = "",back = "";
    for(int i=0;i<amount.Length;i++)
    {
    front = oldstr.Substring(0,amount[i]-1);
    back = str[i].ToString() + oldstr.Substring(amount[i],oldstr.Length-amount[i]);
    oldstr = front + back;
    }
    }
      

  6.   

    public override long Seek(
       long offset,
       SeekOrigin origin
    );
    SeekOrigin origin 是指的什么?
      

  7.   

    SeekOrigin  指偏移的模式参数
      

  8.   

    那好像有三个值对吧.. begin,end, current?  那这个参数怎么用啊.?
    能不能说一下具体的实例啊.谢谢
    这个参数我也用了,不过就是不能定位,对已存在数据进行改写.
      

  9.   

    f.Seek(100,SeekOrigin.Begin);//移到文件从头开始计算偏移100处。