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

Time:02-05

I'm writing something in C# and I need to find a way to split a text file into more files with the number of lines in the file being equivalent to a user input.

Example : file a had 1000 lines in it and I want the code to ask the user for a number and then use that number to make more files like this a = 1000 lines .

Then after the code has run with the input of 300

a = 300 lines

b = 300 lines

c = 300 lines

d = 300 lines

e = 300 lines

Repeat that until the original file has been split into more files all with 300 lines .

This is what I have so far

            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();

Any help would be greatly appreciated

CodePudding user response:

Here's a way to do it using streams. This has the benefit of not needing to read it all into memory at once, allowing it to work on very large files.

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.");

CodePudding user response:

Splitting text file to multiple 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  ;
                }
            }
        }

How to use:

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