将一个txt文件分割成多个文件,每个文件的行数可以由用户设置

split a txt file into multiple files with the number of lines in each file being able to be set by a user

我正在用 C# 编写一些东西,我需要找到一种方法将文本文件拆分为多个文件,文件中的行数等于用户输入的行数。

示例:文件 a 中有 1000 行,我希望代码向用户询问一个数字,然后使用该数字创建更多这样的文件 a = 1000 行 .

然后代码有运行输入300

a = 300 行

b = 300 行

c = 300 行

d = 300 行

e = 300 行

重复该操作,直到原始文件被拆分成更多的文件,所有文件均有 300 行。

这是我目前所拥有的

            var file = File.ReadAllLines(ofd.FileName);

            Console.Write("> ");
            int userlinestosplit = int.Parse(Console.ReadLine());

            ArrayList fileA = new ArrayList();
            for (int i = 0; i < userlinestosplit; i++)
            {
                string line = file[i];
                fileA.Add(line);
            }
            int linesleft = file.Length - userlinestosplit;

            ArrayList fileB = new ArrayList();
            for (int i = linesleft; i < file.Length; i++)
            {
                string line = file[i];
                fileB.Add(line);
            }

            string[] fileAArr = (string[])fileA.ToArray(typeof(string));
            string[] fileBArr = (string[])fileB.ToArray(typeof(string));

            string resdir = "results";
            string modir = "splited";

            Directory.CreateDirectory(resdir);
            Directory.SetCurrentDirectory(resdir);

            Directory.CreateDirectory(modir);
            Directory.SetCurrentDirectory(modir);

            File.WriteAllLines("FA.txt", fileAArr);
            File.WriteAllLines("FB.txt", fileBArr);
            Console.ReadKey();

如有任何帮助,我们将不胜感激

这是一种使用流来实现的方法。这样做的好处是不需要一次将其全部读入内存,从而可以处理非常大的文件。

Console.Write("> ");
var maxLines = int.Parse(Console.ReadLine());

var filename = ofd.FileName;
var fileStream = File.OpenRead(filename);
var readStream = new StreamReader(fileStream);

var nameBase = filename[0..^4]; //strip .txt

var parts = 1;
var notfinished = true;
while (notfinished)
{
    var part = File.OpenWrite($"{nameBase}-{parts}.txt");
    var writer =  new StreamWriter(part);
    for (int i = 0; i < maxLines; i++)
    {
        writer.WriteLine(readStream.ReadLine());
        if (readStream.EndOfStream)
        {
            notfinished = false;
            break;
        }
    }
    writer.Close();
    parts++;
}

Console.WriteLine($"Done splitting the file into {parts} parts.");

将文本文件拆分为多个部分

        public void SplitFile(string inputFile, int size, string path)
        {
            int index = 0;
            string s = string.Empty;
            using (StreamReader sr = File.OpenText(inputFile))
            {
                while (true)
                {
                    if (sr.EndOfStream) break;
                    using (StreamWriter output = new StreamWriter($"{path}\part{index}.txt", false, Encoding.UTF8))
                    {
                        int linesRead = 0;
                        while ((s = sr.ReadLine()) != null && linesRead < size)
                        {
                            output.WriteLine(s);
                            linesRead++;
                        }                        
                    }
                    index++;
                }
            }
        }

使用方法:

  
var inputFile = "test.txt";
int size =300;
SplitFile(inputFile, size, "c:\data");