首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在c#中无法正常工作的文本文件拆分

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

Stack Overflow用户
提问于 2017-04-25 19:29:30
回答 1查看 127关注 0票数 1

我有写到文本文件的要求。如果文件大小超过700MB,则创建新文件并写入。我目前正在用“|”从数据库写数据到文件&然后检查文件大小&拆分成多个文件,但文件splits在行的中间。它应该写入到行尾或新文件中特定行开始。

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

我是c#的新手,你能用示例代码给我推荐解决方案吗?

请找到下面的代码来拆分文件

代码语言:javascript
运行
复制
 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;
                     }
                }
            }
        }
    }
 }

提前谢谢。

你能告诉我有没有其他最好的办法吗?

EN

回答 1

Stack Overflow用户

发布于 2017-04-25 21:49:22

试试这个,重写我在刚开始的c#时代写的行文件拆分器。(您只需将列标题作为字符串添加到新文件的开头。)

代码语言:javascript
运行
复制
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();
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43609457

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档