Link to home
Start Free TrialLog in
Avatar of KK
KK

asked on

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.

User generated image
User generated image
Avatar of Pawan Kumar
Pawan Kumar
Flag of India image

You can use IsNullOrEmpty

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

or

you can also use

IsNullOrWhiteSpace
Avatar of Ryan Chong
the problem is the WriteLine statement in your loop which generate the extra line at the last row.
Avatar of KK
KK

ASKER

I have googled about the same but where i can write that in my code.
Avatar of KK

ASKER

@Ryan Chong:I know it but i need the solution.
Send us the solution in text format. Image is there - I cannot change. :)
Avatar of KK

ASKER

Please find the code file attachment
Code123.txt
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

ASKER CERTIFIED SOLUTION
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of KK

ASKER

@Pawan Kumar:I have tried by your approach.but still has the new line at the end,
Avatar of KK

ASKER

Thanks @Ryan for your quick response.