c# - 如何将字符串输出到多个文件

标签 c# .net

我正在将每行只有一个电子邮件地址的 master_email_list.txt 输入到字符串 fileContent 中

我的表单询问每个文件有多少封电子邮件,并且是 splitNum
int splitNum = int.Parse(numToSplit.Text);

它还从 folderBroswerDialog1 请求目录 saveFolder。

我想获取字符串 fineContent 并将每个文件的 splitNum 电子邮件输出到 saveFolder

因此它会将文件内容拆分为每个文件 100 封电子邮件,并在 saveFolder 中自动生成文件名 001.txt 002.txt 等。最后一个文件将只剩下剩下的。

我正朝着 if 循环的方向发展,(也许是最好的方式?)这样做的最佳方式是什么?

编辑

这是我的新代码:

public partial class Form2 : Form
{
    public string startfiledir { get; private set; }
    public string[] fileContent { get; private set; }
    public string saveFolder { get; private set; }
    public string filePath { get; private set; }

    public string writers { get; private set; }

    OpenFileDialog openFileDialog = new OpenFileDialog();

    private void Button1_Click(object sender, EventArgs e)
    {
            this.button1.Enabled = false;
            Refresh();

            openFileDialog.InitialDirectory = startfiledir;
            openFileDialog.Filter = "txt files (*.txt)|*.txt";
            openFileDialog.FilterIndex = 2;
            openFileDialog.RestoreDirectory = true;

            openFileDialog.ShowDialog();

            //Get the path of specified file
            filePath = openFileDialog.FileName;        

            string[] fileContent = File.ReadAllLines(filePath);

            //show the button again
            this.button1.Enabled = Enabled;
            Refresh();   
    }

    private void SplitDatabutton_Click(object sender, EventArgs e)
    {
            //float splitNum = Int32.Parse(numToSplit.Text);
            float splitNum = 100000;

            ConcurrentDictionary<string, StreamWriter> writers = new ConcurrentDictionary<string, StreamWriter>();

            var Tasks = System.Threading.Tasks.Parallel.For(0, fileContent.Length, (i) =>
            {
                string MyFile = Path.Combine(saveFolder, ((int)(i / ((float)splitNum))).ToString("0000") + ".txt");
                writers.GetOrAdd(MyFile, File.AppendText(MyFile)).WriteLine(fileContent[i]);
            });

            foreach (var writer in writers)
            {
                writer.Value.Close();
            }
    }
}

最佳答案

您的案例很适合并行处理。以下是如何使用 System.Tasks 完成这项工作的粗略概念:

ConcurrentDictionary<string, StreamWriter> writers = new ConcurrentDictionary<string, StreamWriter>();
string[] fileContent = File.ReadAllLines("MAIN_FILE_PATH");

var Tasks = System.Threading.Tasks.Parallel.For(0, fileContent.Length, (i) =>
{
  string MyFile = ((int)(i / 100f)).ToString("0000") + ".txt";
  writers.GetOrAdd(MyFile, File.AppendText(MyFile)).WriteLine(fileContent[i]);
});

foreach (var writer in writers)
  writer.Value.Close();

编辑

在评论中回答您的问题,更改此代码中的以下内容:

  1. 看起来您已使用 File.ReadAllText() 读取您的主文件。由于您的主文件每行有一封电子邮件,我建议您改为使用 ReadAllLines(),这样您以后就不必拆分 fileContent。因此,将 ReadAllText() 行替换为以下行:

    string[] fileContent = File.ReadAllLines("MAIN_FILE_PATH");
    

    上面的行还会为您提供主文件中电子邮件数量的正确计数。

  2. 在下一行中使用 saveFoldernumToSplit 构建输出路径:

    string MyFile = Path.Combine(saveFolder, ((int)(i / ((float)numToSplit))).ToString("0000") + ".txt");
    

    这将正确命名最多 10000 个文件的 block 文件。如果您需要更多,可以增加 ToString("0000") 部分中的零个数。

编辑2

以下是您需要进行的更改:

using System.Collections.Concurrent;
using System.IO;

public partial class Form2 : Form
{
  public string startfiledir { get; private set; }
  public string[] fileContent { get; private set; }
  public string saveFolder { get; private set; }
  public string filePath { get; private set; }

  private ConcurrentDictionary<string, StreamWriter> writers = new ConcurrentDictionary<string, StreamWriter>();

  OpenFileDialog openFileDialog = new OpenFileDialog();

  private void Button1_Click(object sender, EventArgs e)
  {

    this.button1.Enabled = false;
    Refresh();

    openFileDialog.InitialDirectory = startfiledir;
    openFileDialog.Filter = "txt files (*.txt)|*.txt";
    openFileDialog.FilterIndex = 2;
    openFileDialog.RestoreDirectory = true;

    openFileDialog.ShowDialog();

    //Get the path of specified file
    filePath = openFileDialog.FileName;

    fileContent = File.ReadAllLines(filePath);

    //show the button again
    this.button1.Enabled = Enabled;
    Refresh();
  }

  private void SplitDatabutton_Click(object sender, EventArgs e)
  {
    int splitNum = 100;
    int chunks = (int)(fileContent.Length / (float)splitNum);

    var Tasks = Parallel.For(0, chunks, (i) =>
    {
      string MyFile = Path.Combine(savePath, i.ToString("0000") + ".txt");
      using (var W = File.AppendText(MyFile))
      {
        for (int j = i * splitNum; j < (i + 1) * splitNum; j++)
          W.WriteLine(fileContent[j]);
      }       
    });
  }
}

关于c# - 如何将字符串输出到多个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58056505/

相关文章:

.net - 创建泛型函数的委托(delegate)

.net - Entity Framework .Include()导航另一个导航属性

c# - 如何确定数据库中哪些字段被指定为 NOT NULL?

c# - 这个通用继承是如何完成的(内部)?

c# - 在 WPF 中将 ResourceDictionary 与其他样式一起使用

.net - 正则表达式开头或结尾没有空格,但中间允许有空格,但也只允许一个字符输入 :

.net - 调试和编辑项目中作为 NuGet 包引用的内部库的最佳工作流程是什么?

c# - System.Net.Http.HttpClient 与 Windows.Web.Http.HttpClient - 主要区别是什么?

c# - 不能从零开始的正则表达式范围

.net - 如果我将控件传递给不同的类以进行事件处理,是否会发生内存泄漏?