我是c#初学者,向大家请教一下C#分割文件
我的文件比较大,格式是这样的
line 10
    1111  2222  3333   3333   3333
    2222  3333  3333   3333   3333
    .....
line 11
    1111  2222  3333   3333   3333
    2222  3333  3333   3333   3333
    ......
...
怎样将文件按线号分割成多个单独的文件

解决方案 »

  1.   

    先将 文件 用 STREAM 读出来,然后 用正则 分成你想要的块就可以了
      

  2.   


    如果文件较大,不建议完全读到内存中再分析。如果是顺序分块的话,可以一行一行读using System;
    using System.IO;class Test 
    {
        public static void Main() 
        {
            try 
            {
                // Create an instance of StreamReader to read from a file.
                // The using statement also closes the StreamReader.
                using (StreamReader sr = new StreamReader("TestFile.txt")) 
                {
                    String line;
                    // Read and display lines from the file until the end of 
                    // the file is reached.
                    while ((line = sr.ReadLine()) != null) 
                    {
                        //判读是不是新的块,如果是则保存前面的块。
                        Console.WriteLine(line);
                    }
                }
            }
            catch (Exception e) 
            {
                // Let the user know what went wrong.
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(e.Message);
            }
        }
    }代码主要部分来自msdn:
    http://msdn.microsoft.com/zh-cn/library/db5x7c0d(v=vs.80).aspx
      

  3.   

    这是我自己魔兽的启动config修改的,跟你的要求差不多,按你的要求改了下,你看能不能用
    如果你的文件太大可能性能方面不太好
    StreamReader sr = new StreamReader(Path);
    string[] s = new string[]{"\r\n"};
    string[] strs = sr.ReadToEnd.Split(s, StringSplitOptions.RemoveEmptyEntries);
    sr.Close();
    string strPath = string.Empty;foreach(string str in strs)
    {
    if ( str.Length < 8)
    {
    string newStr = str.Substring(0, 4);
    if (newStr.Equals("Line"))
    {
    strPath = Directory + "\\" + strs[i] + ".txt"; //这里根据需要改动地址和后缀

    using (System.IO.File.Create(strPath)) { }
    }
    }
    else
    {
    if (!string.IsNullOrEmpty(strPath))
    {
    StreamWriter sw = new StreamWriter(strPath, false);
    sw.WriteLine(str, true); sw.Close();
    }
    }
    }
      

  4.   

    <pre class="csharp" name="code">
    StreamReader sr = new StreamReader(Path);
    string[] s = new string[]{"\r\n"};
    string[] strs = sr.ReadToEnd.Split(s, StringSplitOptions.RemoveEmptyEntries);
    sr.Close();
    string strPath = string.Empty;foreach(string str in strs)
    {
    if ( str.Length < 8)
    {
    string newStr = str.Substring(0, 4);
    if (newStr.Equals("Line"))
    {
    strPath = Directory + "\\" + strs[i] + ".txt"; //这里根据需要改动地址和后缀

    using (System.IO.File.Create(strPath)) { }
    }
    }
    else
    {
    if (!string.IsNullOrEmpty(strPath))
    {
    StreamWriter sw = new StreamWriter(strPath, false);
    sw.WriteLine(str, true); sw.Close();
    }
    }
    }
    </pre>
      

  5.   

    代码显示??StreamReader sr = new StreamReader(Path);
    string[] s = new string[]{"\r\n"};
    string[] strs = sr.ReadToEnd.Split(s, StringSplitOptions.RemoveEmptyEntries);
    sr.Close();
    string strPath = string.Empty;foreach(string str in strs)
    {
    if ( str.Length < 8)
    {
    string newStr = str.Substring(0, 4);
    if (newStr.Equals("Line"))
    {
    strPath = Directory + "\\" + strs[i] + ".txt"; //这里根据需要改动地址和后缀using (System.IO.File.Create(strPath)) { } 
    }
    }
    else
    {
    if (!string.IsNullOrEmpty(strPath))
    {
    StreamWriter sw = new StreamWriter(strPath, false);
    sw.WriteLine(str, true);sw.Close();
    }
    }
    }
      

  6.   


    还是不要一次性全部读出来的好,
    循环读取,每读到"line 数字"另存为一个文件.
      

  7.   

    首先谢谢各位的回答
    我的文件比较大,如果用 sr.ReadToEnd(),会报错,说是内存不足什么的,所以还是感觉循环读取比较好点,我不知道怎么读到“line 数字”的时候另存为一个文件,再次请教
      

  8.   

    已经回复,再次贴一下.
    System.IO.StreamReader sr = new System.IO.StreamReader("c:\\1.txt");
    string strTemp = sr.ReadLine();
    while (!sr.EndOfStream) {
    if (strTemp.StartsWith("line")) {
    System.IO.StreamWriter sw = new System.IO.StreamWriter("c:\\" + strTemp.Replace(" ", "_") + ".txt", true);
    sw.WriteLine(strTemp);
    strTemp = sr.ReadLine();
    while (!sr.EndOfStream) {
    if (strTemp.StartsWith("line")) {
    break; // TODO: might not be correct. Was : Exit While
    } else {
    sw.WriteLine(strTemp);
    }
    strTemp = sr.ReadLine();
    }
    sw.Close();
    }
    }
    sr.Close();
    MessageBox.Show("ok");