Microsoft Developer Network > Página Inicial dos Fóruns > Visual C# General > How to Split text file into multiple text files?
Fazer uma PerguntaFazer uma Pergunta
 

PerguntaHow to Split text file into multiple text files?

  • sexta-feira, 23 de fevereiro de 2007 3:55My Vizai Medalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     

    Hi experts

    I'm looking for help to split a large text file into multiple text files based on size of the File.

    Can anybody help me in this?

     

     

Todas as Respostas

  • sexta-feira, 23 de fevereiro de 2007 5:16Sean Hederman Medalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     

    Some more information about your requirements would help. What exactly do you mean when you say "based on size of the File"? If you wanted each file to be a maximum of a certain size you could do something like:

    string sourceFileName = @"C:\VS2005 SP1.exe";

    string destFileLocation = @"C:\";

    int index = 0;

    long maxFileSize = 52428800;

    byte[] buffer = new byte[65536];

     

    using (Stream source = File.OpenRead(sourceFileName))

    {

        while (source.Position < source.Length)

        {

            index++;

     

            // Create a new sub File, and read into t

            string newFileName = Path.Combine(destFileLocation, Path.GetFileNameWithoutExtension(sourceFileName));

            newFileName += index.ToString() + Path.GetExtension(sourceFileName);

            using (Stream destination = File.OpenWrite(newFileName))

            {

                while (destination.Position < maxFileSize)

                {

                    // Work out how many bytes to read

                    int bytes = source.Read(buffer, 0, (int) Math.Min(maxFileSize, buffer.Length));

                    destination.Write(buffer, 0, bytes);

     

                    // Are we at the end of the file?

                    if (bytes < Math.Min(maxFileSize, buffer.Length))

                    {

                        break;

                    }

                }

            }

        }

    }

  • sexta-feira, 23 de fevereiro de 2007 5:21My Vizai Medalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     

    Thanks for the reply  Sean Hederman

    Suppose if I've text(.txt) file more than 500KB , I want to split it into muliple files.

    Ex :

    temp.txt is file with 1209KB size

    Now the result should be

    temp1.txt

    temp2.txt

    temp3.txt

  • sexta-feira, 23 de fevereiro de 2007 5:37Sean Hederman Medalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     

    But what size should each file be? Whatever that size is, set the maxFileSize to that, and run my code, and it will automatically split the file for you into the directory specified in destFileLocation.

  • sexta-feira, 23 de fevereiro de 2007 8:14My Vizai Medalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     
    Thank u , I'll try that.
  • sexta-feira, 23 de fevereiro de 2007 8:55My Vizai Medalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     

     

    can u tell the maxSize field here, what it is,

    I want to split at 500KB each file.

    If the main file exceeds 500KB then I want to split it.

  • sexta-feira, 23 de fevereiro de 2007 10:19Sean Hederman Medalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     

    Well, 500KB is 500x1024 bytes which means the maximum file size should be 512000.

  • sexta-feira, 23 de fevereiro de 2007 10:56My Vizai Medalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     

     

    Thankq  Got

    But I'm reading lines from the text file

    This method , splitting the line and place the truncated one in other file

     

     

  • sexta-feira, 23 de fevereiro de 2007 11:12Sean Hederman Medalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     

    It would have been useful to know that you needed intact lines upfront.

    Well, basically you'd rewrite it to use StreamReader and ReadLine, then for each line you'd have to decide if the line would take your file beyond it's max size, and if it didn't write it to the file, and if it did, start a new file.