.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> C# >> Post New QuestionBookmark and Share Subscribe to Forum

One Text File into Multiple Text File in c#

Posted By: Narayanan     Posted Date: December 03, 2012    Points:5   Category :C#
Hi,
How to Split One Text File into Multiple Text File(s) in C#.Net. My Text File Contains like this:
One Text File:
Line1
Line2
Line3
Line4
Line5
Line1
Line2
Line3
Line4
Line5
Line1
Line2
Line3
Line4
Line5


Result
Text1.Txt
Line1
Line2
Line3
Line4
Line5
Text2.Txt
Line1
Line2
Line3
Line4
Line5
Text3.Txt
Line1
Line2
Line3
Line4
Line5

Please Help me..



Responses
Author: Creative13             
Posted Date: December 04, 2012     Points: 20   

You can do this.....Narayan


just follows these steps....
1. Read Input File using StreamReader Class.
2. If you know the FINITE no. of line should be in Each Separate File, Then using any loop method (like FOR, While loop etc.) set Last no. of line should be read from Input file.
3. Then After Last No. of Line ...Start writing the content to NEW File..using StreamWriter Class..


Happy Programming...........


Let me Know, if you get proper result...

Author: Narayanan             
Posted Date: December 05, 2012     Points: 20   

Hi ,
Thanks for your reply.Can you give me with an Example.

Regards, Lakshmi Naraayanan.S
http://dotnettechrocks.blogspot.in/
Author: Baimey Rajesh             
Posted Date: December 05, 2012     Points: 20   

Please try this
public void SplitFile()
{
int nr = 1;
int package = 300;
DateTime date2 = DateTime.Now;
int packtester = 0;
using (var freader = new StreamReader("bigfile.txt"))
{
StreamWriter pak = null;
try
{
pak = new StreamWriter(GetPackFilename(package, nr, date2), false);
string line;

while ((line = freader.ReadLine()) != null)
{
if (packtester < package)
{
pak.WriteLine(line); //writing line to small file
packtester++; //increasing the lines of small file
}
else
{
pak.Flush();
pak.Close(); //closing the file
packtester = 0;
nr++; //nr++ -> just for file name to be Pack-2;
pak = new StreamWriter(GetPackFilename(package, nr, date2), false);
}
}
}
finally
{
if(pak != null)
{
pak.Dispose();
}
}
}
}

private string GetPackFilename(int package, int nr, DateTime date2)
{
return string.Format("{0}Pack-{1}+_{2}.txt", package, nr, date2);
}

From http://stackoverflow.com/questions/11177489/how-can-i-split-a-big-text-file-into-smaller-file

Author: Baimey Rajesh             
Posted Date: December 05, 2012     Points: 20   

If the above dint help please check this tutorial

www.youtube.com/watch?v=wwDNtCuhr7Y

Author: Creative13             
Posted Date: December 07, 2012     Points: 20   

Void getContent()
{

using (StreamReader rdr = new StreamReader(FilePath))
{
for(int i=0; i<=2 ; i++) //Assign No. of File Should get written
{
StreaWriter wrt= new StreamWriter(newFilePath+i.ToString()); //Assign NewFile(s) name

for(int j =0 ; j<=4 ; j++)
{
wrt.WriteLine(rdr.Readline()); //Write first 5 lines from current record into new file
}
wrt.Close();
}//End of NewFile(1,2,3,...)
}
}

Author: Narayanan             
Posted Date: January 03, 2013     Points: 20   

Hi,
Thanks

Regards, Lakshmi Naraayanan.S
http://dotnettechrocks.blogspot.in/


Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend