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?
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
0
Ryan ChongCommented:
the problem is the WriteLine statement in your loop which generate the extra line at the last row.
0
KKAuthor Commented:
I have googled about the same but where i can write that in my code.
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

KKAuthor Commented:
@Ryan Chong:I know it but i need the solution.
0
Pawan KumarDatabase ExpertCommented:
Send us the solution in text format. Image is there - I cannot change. :)
0
KKAuthor Commented:
Please find the code file attachment
Code123.txt
0
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

0
Ryan ChongCommented:
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

1

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,
0
KKAuthor Commented:
Thanks @Ryan for your quick response.
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.