Removing blank line at the end of the Text file.

I have a simple log text file with the extension of .txt with a white space line at the end of that text file.

Text File
Code
KKAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Pawan KumarDatabase ExpertCommented:
You can use IsNullOrEmpty

if Not String.IsNullOrEmpty(line) then write into the file.

or

you can also use

IsNullOrWhiteSpace
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
the problem is the WriteLine statement in your loop which generate the extra line at the last row.
KKAuthor Commented:
I have googled about the same but where i can write that in my code.
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

KKAuthor Commented:
@Ryan Chong:I know it but i need the solution.
Pawan KumarDatabase ExpertCommented:
Send us the solution in text format. Image is there - I cannot change. :)
KKAuthor Commented:
Please find the code file attachment
Code123.txt
Pawan KumarDatabase ExpertCommented:
Pls try this -

temPath = Path.ChangeExtension(sFilePath, ".txt");
using (var output = new StreamWriter(temPath))
{
   foreach(var path in filePaths)
	{
		using (var input = new StreamReader(path))
		{
			string line = string.Empty;
			while ((line = input.ReadLine()) != null)
			{
			   
				line = line.Replace("\"\"", "$#@$").Replace("\"", "").Replace("\t", "");
				if (line.Contains("$#@$"))
					 flag = true;
					 
				if Not String.IsNullOrEmpty(line) AND NOT String.IsNullOrWhiteSpace(line)
					output.WriteLine(line);
			}
		}
	}

Open in new window

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
something like this should works

Boolean isFirstLine = true;
temPath = Path.ChangeExtension(sFilePath, ".txt");
                using (var output = new StreamWriter(temPath))
                {
                   foreach(var path in filePaths)
                    {
                        using (var input = new StreamReader(path))
                        {
                            string line = string.Empty;
                            while ((line = input.ReadLine()) != null)
                            {
                               
                                line = line.Replace("\"\"", "$#@$").Replace("\"", "").Replace("\t", "");
                                if (line.Contains("$#@$"))
                                     flag = true;
                                line = Convert.ToString(line);
                                   
								if (isFirstLine)
								{
									isFirstLine = false;
								}
								else
								{
									output.Write(output.NewLine);
								}
								output.Write(line);
                            
                            }
                        }
                    }

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
KKAuthor Commented:
@Pawan Kumar:I have tried by your approach.but still has the new line at the end,
KKAuthor Commented:
Thanks @Ryan for your quick response.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.