C#如何将文本文件拆分为多个文件

发布于 2024-12-02 15:18:56 字数 1024 浏览 0 评论 0原文

如何将一个 1000 行的文本文件拆分为多个较小的文件,例如每个 300 行?请记住,原始文件可能多于或少于一千行。

file1.txt 300 lines -> rest
file2.txt 300 lines -> rest
file3.txt 300 lines -> rest
file4.txt 100 lines 

我尝试了以下方法,但它不起作用。

int counter = 0;
string line;

string lineoutput = (current_dir + "\\" + DateTime.Now.ToString("HHmmss") + ".txt");

System.IO.StreamReader inputfile;

inputfile = new System.IO.StreamReader(new_path);
while ((line = inputfile.ReadLine()) != null)
{
    System.IO.StreamWriter file = new System.IO.StreamWriter(current_dir + "\\" + DateTime.Now.ToString("HHmmss") + ".txt", true);

    string _replaceBackspace = ReplaceBackspace(read_file.ReadLine().ToLower());

    using (StreamWriter writer = new StreamWriter(lineoutput, true))
    {
        if (counter == 5000)
        {
            counter = 0;
            lineoutput = (current_dir + "\\" + DateTime.Now.ToString("HHmmss") + ".txt");
        }
        writer.WriteLine(line.ToLower());
    }
    counter++;
}

How do I split a single text file with 1000 lines into multiple smaller files of, for example, 300 lines apiece? Please keep in mind that the original file may have more or less than a thousand lines.

file1.txt 300 lines -> rest
file2.txt 300 lines -> rest
file3.txt 300 lines -> rest
file4.txt 100 lines 

I tried the following but it's not working.

int counter = 0;
string line;

string lineoutput = (current_dir + "\\" + DateTime.Now.ToString("HHmmss") + ".txt");

System.IO.StreamReader inputfile;

inputfile = new System.IO.StreamReader(new_path);
while ((line = inputfile.ReadLine()) != null)
{
    System.IO.StreamWriter file = new System.IO.StreamWriter(current_dir + "\\" + DateTime.Now.ToString("HHmmss") + ".txt", true);

    string _replaceBackspace = ReplaceBackspace(read_file.ReadLine().ToLower());

    using (StreamWriter writer = new StreamWriter(lineoutput, true))
    {
        if (counter == 5000)
        {
            counter = 0;
            lineoutput = (current_dir + "\\" + DateTime.Now.ToString("HHmmss") + ".txt");
        }
        writer.WriteLine(line.ToLower());
    }
    counter++;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

无需解释 2024-12-09 15:18:56

最简单的情况:

        var reader = File.OpenText(infile);
        string outFileName = "file{0}.txt";
        int outFileNumber = 1;
        const int MAX_LINES = 300;
        while (!reader.EndOfStream)
        {
            var writer = File.CreateText(string.Format(outFileName, outFileNumber++));
            for (int idx = 0; idx < MAX_LINES; idx++)
            {
                writer.WriteLine(reader.ReadLine());
                if (reader.EndOfStream) break;
            }
            writer.Close();
        }
        reader.Close();

Simplest case:

        var reader = File.OpenText(infile);
        string outFileName = "file{0}.txt";
        int outFileNumber = 1;
        const int MAX_LINES = 300;
        while (!reader.EndOfStream)
        {
            var writer = File.CreateText(string.Format(outFileName, outFileNumber++));
            for (int idx = 0; idx < MAX_LINES; idx++)
            {
                writer.WriteLine(reader.ReadLine());
                if (reader.EndOfStream) break;
            }
            writer.Close();
        }
        reader.Close();
寒江雪… 2024-12-09 15:18:56
string baseName = current_dir + "\\" + DateTime.Now.ToString("HHmmss") + ".";

StreamWriter writer = null;
try
{
    using (StreamReader inputfile = new System.IO.StreamReader(new_path))
    {
        int count = 0;
        string line;
        while ((line = inputfile.ReadLine()) != null)
        {

            if (writer == null || count > 300)
            {
                if (writer != null)
                {
                    writer.Close();
                    writer = null;
                }

                writer = new System.IO.StreamWriter(baseName + count.ToString() + ".txt", true);

                count = 0;
            }

            writer.WriteLine(line.ToLower());

            ++count;
        }
    }
}
finally
{
    if (writer != null)
        writer.Close();
}
string baseName = current_dir + "\\" + DateTime.Now.ToString("HHmmss") + ".";

StreamWriter writer = null;
try
{
    using (StreamReader inputfile = new System.IO.StreamReader(new_path))
    {
        int count = 0;
        string line;
        while ((line = inputfile.ReadLine()) != null)
        {

            if (writer == null || count > 300)
            {
                if (writer != null)
                {
                    writer.Close();
                    writer = null;
                }

                writer = new System.IO.StreamWriter(baseName + count.ToString() + ".txt", true);

                count = 0;
            }

            writer.WriteLine(line.ToLower());

            ++count;
        }
    }
}
finally
{
    if (writer != null)
        writer.Close();
}
陪你到最终 2024-12-09 15:18:56

循环 File.ReadLines(path) 并将每一行写入 StreamWriter

保留一个计数器,每次达到 300 时,关闭 StreamWriter 并打开一个新的。

Loop over File.ReadLines(path) and write each line to a StreamWriter.

Keep a counter, and, each time it reaches 300, close the StreamWriter and open a new one.

清秋悲枫 2024-12-09 15:18:56

除了 SLAks 答案之外,您还可以使用 System.Linq 中的扩展方法 SkipTake 来完成此操作

string[] ss = File.ReadAllLines(@"path to the file");

int cycle = 1;
int chunksize = 300;

var chunk = ss.Take(chunksize);
var rem = ss.Skip(chunksize);

while (chunk.Take(1).Count() > 0)
{
    string filename = "file" + cycle.ToString() + ".txt";
    using (StreamWriter sw = new StreamWriter(filename))
    {
        foreach(string line in chunk)
        {
            sw.WriteLine(line);
        }
    }
    chunk = rem.Take(chunksize);
    rem = rem.Skip(chunksize);
    cycle++;
}

As well as SLaks answer, you can also do it using the extension methods Skip and Take in System.Linq

string[] ss = File.ReadAllLines(@"path to the file");

int cycle = 1;
int chunksize = 300;

var chunk = ss.Take(chunksize);
var rem = ss.Skip(chunksize);

while (chunk.Take(1).Count() > 0)
{
    string filename = "file" + cycle.ToString() + ".txt";
    using (StreamWriter sw = new StreamWriter(filename))
    {
        foreach(string line in chunk)
        {
            sw.WriteLine(line);
        }
    }
    chunk = rem.Take(chunksize);
    rem = rem.Skip(chunksize);
    cycle++;
}
如痴如狂 2024-12-09 15:18:56

根据 bigtbl 的答案,我添加了对于生成一系列 CSV 的情况,将第一行保留为每个文件的标题。 MAX_LINES 包含总计数的标题行,这就是 start_idx 的原因。

public static void SplitFil(int rows, string inputFile) {
      int outFileNumber = 1;      
      const int MAX_LINES = 50000;      
      string header = "";
      if (GetFileSize(inputFile) > MAX_LINES) {
        var reader = File.OpenText(inputFile);               
        while (!reader.EndOfStream)
        {
          var start_idx = 0;          
          var writer = File.CreateText($"filename_{outFileNumber}.csv");
          if (outFileNumber > 1) {
            writer.WriteLine(header);
            start_idx = 1;
          }            
          for (int idx = start_idx; idx < MAX_LINES; idx++)
          { 
            var row = reader.ReadLine();
            if (idx == 0 && outFileNumber == 1) header = row;
            writer.WriteLine(row);
            if (reader.EndOfStream) break;
          }
          writer.Close();
          outFileNumber++;
        }
        reader.Close();
      }
    }

Following on the answer from bigtbl, I added, for the case of generating a series of CSVs, preservation of first row as header on each file. MAX_LINES is inclusive of the header row for total count, which is the reason for start_idx.

public static void SplitFil(int rows, string inputFile) {
      int outFileNumber = 1;      
      const int MAX_LINES = 50000;      
      string header = "";
      if (GetFileSize(inputFile) > MAX_LINES) {
        var reader = File.OpenText(inputFile);               
        while (!reader.EndOfStream)
        {
          var start_idx = 0;          
          var writer = File.CreateText($"filename_{outFileNumber}.csv");
          if (outFileNumber > 1) {
            writer.WriteLine(header);
            start_idx = 1;
          }            
          for (int idx = start_idx; idx < MAX_LINES; idx++)
          { 
            var row = reader.ReadLine();
            if (idx == 0 && outFileNumber == 1) header = row;
            writer.WriteLine(row);
            if (reader.EndOfStream) break;
          }
          writer.Close();
          outFileNumber++;
        }
        reader.Close();
      }
    }
时光无声 2024-12-09 15:18:56

完整程序:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SplitTexTfileIntoMultiplefiles
{
    class Program
    {
        static void Main(string[] args)
        {
            string infile = @"C:\MyProj\file.sql";
            var reader = File.OpenText(infile);            
            int outFileNumber = 1;
            Console.WriteLine("Wait...");
            const int MAX_LINES = 20000;
            while (!reader.EndOfStream)
            {
                string outfname = Path.GetDirectoryName(infile) + "\\" + Path.GetFileNameWithoutExtension(infile) + outFileNumber.ToString ("D4") + Path.GetExtension(infile);
                Console.WriteLine(outfname);
                var writer = File.CreateText(outfname);
                for (int idx = 0; idx < MAX_LINES; idx++)
                {
                    writer.WriteLine(reader.ReadLine());
                    if (reader.EndOfStream) break;
                }
                writer.Close();
                outFileNumber++;
            }
            reader.Close();
            Console.WriteLine("Done.");
            Console.ReadKey();
        }
    }
}

Full program:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SplitTexTfileIntoMultiplefiles
{
    class Program
    {
        static void Main(string[] args)
        {
            string infile = @"C:\MyProj\file.sql";
            var reader = File.OpenText(infile);            
            int outFileNumber = 1;
            Console.WriteLine("Wait...");
            const int MAX_LINES = 20000;
            while (!reader.EndOfStream)
            {
                string outfname = Path.GetDirectoryName(infile) + "\\" + Path.GetFileNameWithoutExtension(infile) + outFileNumber.ToString ("D4") + Path.GetExtension(infile);
                Console.WriteLine(outfname);
                var writer = File.CreateText(outfname);
                for (int idx = 0; idx < MAX_LINES; idx++)
                {
                    writer.WriteLine(reader.ReadLine());
                    if (reader.EndOfStream) break;
                }
                writer.Close();
                outFileNumber++;
            }
            reader.Close();
            Console.WriteLine("Done.");
            Console.ReadKey();
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文