Antonio King
asked on
Remove blank lines from .txt file
How do I remove empty lines from a txt file using c#.
I had an idea that it could read all lines and copy each line that contains text to a new txt file.
Remove the original and rename the new one to the original ones name?
But not too sure on how to do this.
I had an idea that it could read all lines and copy each line that contains text to a new txt file.
Remove the original and rename the new one to the original ones name?
But not too sure on how to do this.
Simple:
string data = File.ReadAllText("test.txt ").Replace ("\r\n\r\n ", "\r\n");
File.Delete("test.txt");
File.WriteAllText("test.tx t", data);
string data = File.ReadAllText("test.txt
File.Delete("test.txt");
File.WriteAllText("test.tx
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The above examples have one major flaw: if you have, say, three blank lines one after other the code will convert it to two lines, four - to two, five - to thre, etc. So you may need to run the procedure again - not very clever...
I use different pproach - I read file to a list and add only non-blank lines (you may also add trim() method to remove lines of spaces). Code is simple enough:
private void button2_Click(object sender, EventArgs e)
{
List<string> lines = new List<string>();
// Read file to list
StreamReader sr = File.OpenText("C:\\atest.t xt");
string input = null;
while ((input = sr.ReadLine()) != null)
{
if (input.Length > 0)
{
lines.Add(input);
}
Console.WriteLine(input);
}
sr.Close();
// Write file
StreamWriter sw = new StreamWriter("C:\\atest2.t xt");
foreach (string line in lines)
{
sw.WriteLine(line);
}
sw.Close();
}
I use different pproach - I read file to a list and add only non-blank lines (you may also add trim() method to remove lines of spaces). Code is simple enough:
private void button2_Click(object sender, EventArgs e)
{
List<string> lines = new List<string>();
// Read file to list
StreamReader sr = File.OpenText("C:\\atest.t
string input = null;
while ((input = sr.ReadLine()) != null)
{
if (input.Length > 0)
{
lines.Add(input);
}
Console.WriteLine(input);
}
sr.Close();
// Write file
StreamWriter sw = new StreamWriter("C:\\atest2.t
foreach (string line in lines)
{
sw.WriteLine(line);
}
sw.Close();
}
By the way, my test file (atest.txt):
"
aaa
bbb
ccc
ddd
eee
fff
gggg
.
"
Run my code and compare to result of running previous code.
"
aaa
bbb
ccc
ddd
eee
fff
gggg
.
"
Run my code and compare to result of running previous code.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
anarki_jimbel, i dint find any flaw in my code... i do know what u r trying to do... After testing only i used to post my code.... It works fine
The goal is to remove blank lines, isn't it?
Your code (accepted one) gives the following result with my test file"
"
aaa
bbb
ccc
ddd
eee
fff
gggg
.
"
So your code removes some blank lines but not all! That's why I posted my test file content - for you to test. So my improvement (assisted solution) does it's job much better.Try to prove opposite. I tried the last version of your code only, just in case.
I don't like my solution from the point of performance. By the way, for Alan, To make my code more efficient for big files (if you need) use StringBuffer class to build output, and returm stringbuffer object. toString().
Your code (accepted one) gives the following result with my test file"
"
aaa
bbb
ccc
ddd
eee
fff
gggg
.
"
So your code removes some blank lines but not all! That's why I posted my test file content - for you to test. So my improvement (assisted solution) does it's job much better.Try to prove opposite. I tried the last version of your code only, just in case.
I don't like my solution from the point of performance. By the way, for Alan, To make my code more efficient for big files (if you need) use StringBuffer class to build output, and returm stringbuffer object. toString().
StreamReader sr = new StreamReader(fs);
string data = sr.ReadToEnd().Replace("\r
sr.Close();
fs.Close();
File.Delete("test.txt");
FileStream fs1 = new FileStream("test.txt", FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs1);
sw.Write(data);
sw.Close();
fs1.Close();