c# 从一个大文本文件中复制其中的一些行,到另一个文件,

    /// <summary>
    /// 复制指定行到另一文件,1650104到1709401之间的行复制
    /// </summary>
    public void copyLines()
    {
        string file1 = @"E:\tmp\0021\2694583_all.sql";
        string file2 = @"E:\tmp\0021\members.sql";
        System.IO.StreamReader sr = new System.IO.StreamReader(file1);
        var fileShare = System.IO.FileShare.Write;
        
        using(var fswriter = new System.IO.FileStream(path: file2, share: fileShare, access: System.IO.FileAccess.Write, mode: System.IO.FileMode.Append))
        {
            int i = 1;
            while (!sr.EndOfStream)
            {
                var line = sr.ReadLine();
                if (i < 1650104) { i++; continue; }
                if (i > 1709401) { break; }
                var bys = System.Text.Encoding.UTF8.GetBytes(line + "\n");
                fswriter.Write(bys, 0, bys.Length);
                i++;
            }
        }
        sr.Close();
    }

  

posted @ 2019-11-22 14:51  以函  阅读(449)  评论(0编辑  收藏  举报