Below code does the same job of head command in Unix which is widely used to get the top n lines from a text file.
namespace PartialFileCopier
{
class Program
{
static void Main(string[] args)
{
String sLine = String.Empty;
Int32 iLineCounter = 0;
Int32 iLineCount = Int32.Parse(ConfigurationSettings.AppSettings["LINE_COUNT"]);
try
{
using (StreamWriter writer = new StreamWriter(ConfigurationSettings.AppSettings["OUTPUT_FILE_NAME"]))
{
using (StreamReader reader = new StreamReader(ConfigurationSettings.AppSettings["INPUT_FILE_NAME"]))
{
while (!reader.EndOfStream )
{
sLine = reader.ReadLine();
iLineCounter++;
Console.WriteLine(sLine);
writer.WriteLine(sLine);
if (iLineCounter == iLineCount)
break;
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.Read();
}
}
}
The configuration entries
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="INPUT_FILE_NAME" value="\\Servername\InputFileName.txt"/>
<add key="OUTPUT_FILE_NAME" value="\\Servername\OutputFileName.txt"/>
<add key="LINE_COUNT" value="1000"/>
</appSettings>
</configuration>
No comments:
Post a Comment