One Text File into Multiple Text File in C#

Posted by Naraayanan under C# on 12/4/2012 | Points: 10 | Views : 1248 | Status : [Member] | Replies : 3
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..

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



Responses

Posted by: Krv on: 12/4/2012 [Member] Starter | Points: 25

Up
0
Down
1. Read the textfile using stream reader
2. Loop the stream reader till the file EOD
3. Read the stream reader. readLine and write it into another file as you required

Thanks,
R.Vasanth
MCTS(SQL Server & BI)
http://bigdatatech.blogspot.com

Naraayanan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Naraayanan on: 12/5/2012 [Member] Starter | Points: 25

Up
0
Down
Hi ,
Thanks for your reply .Please give an example

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

Naraayanan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Pavanandey on: 12/5/2012 [Member] Bronze | Points: 25

Up
0
Down
StreamReader Sr = new StreamReader("C:\\Users\\Pavan\\Desktop\\1.txt");
string Line = string.Empty; ;
int CountLine = 0;
int countfile = 1;
string[] lines = new string[100];
while ((Line = Sr.ReadLine()) != null)
{
for (CountLine = 0; CountLine < 5; CountLine++)
{
lines[CountLine] = Line;
Line = Sr.ReadLine();
}
string newpath = "C:\\Users\\Pavan\\Desktop\\Hi\\" + countfile + ".txt";
System.IO.File.WriteAllLines(@newpath, lines);
countfile++;
}
Sr.Close();

Thanks
Pavan Kumar
Mark Answer if this fits the need

Naraayanan, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response