I have a simple log text file with the extension of .txt with a white space line at the end of that text file every time the log file is generated from a 3rd party program.<
How about something like :
var lines = System.IO.File.ReadAllLines("...");
System.IO.File.WriteAllLines("...", lines.Take(lines.Length - 1).ToArray());
Explanation:
Technically, you don't delete a line from a file. You read the contents of a file and write them back excluding the content you want deleted.
What this code does is read all the lines into an array, and write these lines back to the file excluding only the last line. (Take() method (Part of LINQ) takes number of lines specified which in our case, is length - 1). Here, var lines
can be read as String[] lines
.
I just find a solution from other website, and this is the url https://stackoverrun.com/cn/q/627722 .
With a small number of lines, you could easily use something like this
string filename = @"C:\Temp\junk.txt";
string[] lines = File.ReadAllLines(filename);
However, as the files get larger, you may want to stream the data in and out with something like this
string filename = @"C:\Temp\junk.txt";
string tempfile = @"C:\Temp\junk_temp.txt";
using (StreamReader reader = new StreamReader(filename))
{
using (StreamWriter writer = new StreamWriter(tempfile))
{
string line = reader.ReadLine();
while (!reader.EndOfStream)
{
writer.WriteLine(line);
line = reader.ReadLine();
} // by reading ahead, will not write last line to file
}
}
File.Delete(filename);
File.Move(tempfile, filename);
That should be the best option for using with big files.
using (StreamReader source = new StreamReader(sourceFileName))
{
using (StreamWriter sw = new StreamWriter(newFileName))
{
do
{
line = source.ReadLine();
if (source.Peek() > -1)
{
sw.WriteLine(line);
}
else
{
File.AppendAllText("RemovedLastLine.txt", line);
}
}
while (line != null);
}
}
If you want to delete last N lines from a file without loading all into memory:
int numLastLinesToIgnore = 10;
string line = null;
Queue<string> deferredLines = new Queue<string>();
using (TextReader inputReader = new StreamReader(inputStream))
using (TextWriter outputReader = new StreamWriter(outputStream))
{
while ((line = inputReader.ReadLine()) != null)
{
if (deferredLines.Count() == numLastLinesToIgnore)
{
outputReader.WriteLine(deferredLines.Dequeue());
}
deferredLines.Enqueue(line);
}
// At this point, lines still in Queue get lost and won't be written
}
What happens is that you buffer each new line in a Queue with dimension numLastLinesToIgnore
and pop from it a line to write only when Queue is full. You actually read-ahead the file and you are able to stop numLastLinesToIgnore
lines before the end of file is reached, without knowing the total number of lines in advance.
Note that if text has less than numLastLinesToIgnore
, result is empty.
I came up with it as a mirror solution to this: Delete specific line from a text file?
Use this method to remove the last line of the file:
public static void DeleteLastLine(string filepath)
{
List<string> lines = File.ReadAllLines(filepath).ToList();
File.WriteAllLines(filepath, lines.GetRange(0, lines.Count - 1).ToArray());
}
Edit: Realized the lines variable didn't exist previously, so I updated the code.
You can't delete the line end, as File.WriteAllLines automatically adds it, however, you can use this method:
public static void WriteAllLinesBetter(string path, params string[] lines)
{
if (path == null)
throw new ArgumentNullException("path");
if (lines == null)
throw new ArgumentNullException("lines");
using (var stream = File.OpenWrite(path))
using (StreamWriter writer = new StreamWriter(stream))
{
if (lines.Length > 0)
{
for (int i = 0; i < lines.Length - 1; i++)
{
writer.WriteLine(lines[i]);
}
writer.Write(lines[lines.Length - 1]);
}
}
}
This is not mine, I found it at .NET File.WriteAllLines leaves empty line at the end of file