How to delete the last line in a text file?

I have a simple text log file with the extension .txt with a space at the end of this text file every time the log file is created from a third-party program.

So are there any methods or codes that I can use to remove the last line of a text file?

Example text log file:

Sun Jul 22 2001 02:37:46,73882,...b,r/rrwxrwxrwx,0,0,516-128-3,C:/WINDOWS/Help/digiras.chm Sun Jul 22 2001 02:44:18,10483,...b,r/rrwxrwxrwx,0,0,480-128-3,C:/WINDOWS/Help/cyycoins.chm Sun Jul 22 2001 02:45:32,10743,...b,r/rrwxrwxrwx,0,0,482-128-3,C:/WINDOWS/Help/cyzcoins.chm Sun Jul 22 2001 04:26:14,174020,...b,r/rrwxrwxrwx,0,0,798-128-3,C:/WINDOWS/system32/spool/drivers/color/kodak_dc.icm 
+6
c # line
source share
4 answers

How about something like:

 var lines = System.IO.File.ReadAllLines("..."); System.IO.File.WriteAllLines("...", lines.Take(lines.Length - 1).ToArray()); 

Explanation:

Technically, you are not deleting a line from a file. You read the contents of the file and write it back, except for the content you want to delete.

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. (The Take () method (part of LINQ) takes the number of lines specified in our case, length - 1). Here, var lines can be read as String[] lines .

+18
source share

Use this method to delete the last line of a 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: The implemented string variable did not exist before, so I updated the code.

+10
source share

If you want to delete the 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 the queue with the size numLastLinesToIgnore and pop the line for writing out of it only when the queue is full. You really read the file forward, and you can stop the numLastLinesToIgnore lines before reaching the end of the file , without knowing the total number of lines in advance .

Note that if the text is less than numLastLinesToIgnore , the result is empty.

I came up with this as a mirror solution for this: Delete a specific line from a text file?

+1
source share

You cannot delete the end of the line, 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 in a .NET File.WriteAllLines leaves an empty line at the end of the file

+1
source share

All Articles