在c#中拆分文本文件无法正常工作

时间:2017-04-25 11:29:30

标签: c# asp.net file console-application

我要求写入文本文件。 如果文件大小超过700MB,请创建新文件&写信给它。 我目前正在编写数据,其中“|”从数据库分隔到文件&之后检查文件大小&分成多个文件,但文件splits位于行的中间。 它应该写到行尾或在新文件中启动该特定行。

我需要在新拆分文件的第一行写下列名。

我是c#的新手,请您使用示例代码向我推荐解决方案。

请找到以下代码来分割文件

 private static void ReadWriteToFile(string fileNames)
 {
     string sourceFileName = fileNames;
     string destFileLocation = Path.GetDirectoryName(fileNames);
     int index = 0;
     long maxFileSize = 700 * 1024 * 1024;
     byte[] buffer = new byte[65536];

     using (Stream source = File.OpenRead(sourceFileName))
     {
         while (source.Position < source.Length)
         {
             index++;

             string newFileName = Path.Combine(destFileLocation, Path.GetFileNameWithoutExtension(sourceFileName));
             newFileName += index.ToString() + Path.GetExtension(sourceFileName);
             using (Stream destination = File.OpenWrite(newFileName))
             {
                 while (destination.Position < maxFileSize)
                 {
                     int bytes = source.Read(buffer, 0, (int)Math.Min(maxFileSize, buffer.Length));
                     destination.Write(buffer, 0, bytes);

                     if (bytes < Math.Min(maxFileSize, buffer.Length))
                     {
                         break;
                     }
                }
            }
        }
    }
 }

提前致谢。

如果有其他最佳方法可以告诉我,

1 个答案:

答案 0 :(得分:0)

试试这个,重写一下我在c​​#times开头写的行文件分割器。 (您只需在新文件的开头将列标题添加为字符串。)

private static void SplitAfterMBytes(int splitAfterMBytes, string filename)
{
    // Variable for max. file size.
    var maxFileSize = splitAfterMBytes * 1048576;
    int fileCount = 0;
    long byteCount = 0;

    StreamWriter writer = null;

    try
    {
        var inputFile = new FileInfo(filename);
        var index = filename.LastIndexOf('.');
        //get only the name of the file.
        var fileStart = filename.Substring(0, index);
        // get the file extension
        var fileExtension = inputFile.Extension;

        // generate a new file name.
        var outputFile = fileStart + '_' + fileCount++ + fileExtension;
        // file format is like: QS_201101_321.txt.

        writer = new StreamWriter(outputFile);

        using (var reader = new StreamReader(filename))
        {
            for (string str; (str = reader.ReadLine()) != null;)
            {
                byteCount = byteCount + System.Text.Encoding.Unicode.GetByteCount(str);

                if (byteCount >= maxFileSize)
                {
                    // max number of bytes reached
                    // write into the old file, without Newline,
                    // so that no extra line is written. 
                    writer.Write(str);
                    // 1. close the actual file.
                    writer.Close();
                    // 2. open a new file with number incresed by 1.
                    outputFile = fileStart + '_' + fileCount++ + fileExtension;

                    writer = new StreamWriter(outputFile);
                    byteCount = 0; //reset the counter.
                }
                else
                {
                    // Write into the old file.
                    // Use a  Linefeed, because Write works without LF.
                    // like Java ;)
                    writer.Write(str);
                    writer.Write(writer.NewLine);
                }
            }
        }
    }
    catch (Exception ex)
    {
        // do something useful, like: Console.WriteLine(ex.Message);
    }
    finally
    {
        writer.Dispose();
    }
}