I used two methods to remove all blank lines in a text file but either one leave one blank line at the end.
Question 1: What is difference between FileStream and StreamWriter? which one below is better or the same?
method one:
private void CleanFile(string file)
{
string[] lines;
try
{
if (File.Exists(file))
{
lines = File.ReadAllLines(file);
using (FileStream fs = new FileStream(file, FileMode.Create))
{
using(StreamWriter sw = new StreamWriter(fs))
{
foreach (string line in lines)
{
if (!String.IsNullOrEmpty(line))
sw.WriteLine(line);
}
}
}
}
}
catch
{
}
}
Method two:
private void CleanFile(string file)
{
string[] lines;
try
{
if (File.Exists(file))
{
lines = File.ReadAllLines(file);
using (StreamWriter sw = new StreamWriter(file))
{
foreach (string ...
Go to the complete details ...