我是c#的新手,我正在研究一個應用程序,它顯示文本文件上最後兩行的兩個日期的時差。從文本中讀取最後2行
我想從文件文本中讀取最後一行,我已經知道如何讀取最後一行,但我需要閱讀最後一行。
這是我的代碼:
var lastLine = File.ReadAllLines("C:\\test.log").Last();
richTextBox1.Text = lastLine.ToString();
我是c#的新手,我正在研究一個應用程序,它顯示文本文件上最後兩行的兩個日期的時差。從文本中讀取最後2行
我想從文件文本中讀取最後一行,我已經知道如何讀取最後一行,但我需要閱讀最後一行。
這是我的代碼:
var lastLine = File.ReadAllLines("C:\\test.log").Last();
richTextBox1.Text = lastLine.ToString();
由於
File.ReadAllLines("C:\\test.log");
返回陣列你可以把數組的最後兩個項目:
var data = File.ReadAllLines("C:\\test.log");
string last = data[data.Length - 1];
string lastButOne = data[data.Length - 2];
在一般情況下與長文件(這就是爲什麼ReadAllLines
是一個不錯的選擇)你可以實現
public static partial class EnumerableExtensions {
public static IEnumerable<T> Tail<T>(this IEnumerable<T> source, int count) {
if (null == source)
throw new ArgumentNullException("source");
else if (count < 0)
throw new ArgumentOutOfRangeException("count");
else if (0 == count)
yield break;
Queue<T> queue = new Queue<T>(count + 1);
foreach (var item in source) {
queue.Enqueue(item);
if (queue.Count > count)
queue.Dequeue();
}
foreach (var item in queue)
yield return item;
}
}
...
var lastTwolines = File
.ReadLines("C:\\test.log") // Not all lines
.Tail(2);
你可以嘗試這樣做
var lastLines = File.ReadAllLines("C:\\test.log").Reverse().Take(2).Reverse();
但是這取決於你的文件有多大有可能是更有效的方法來處理這個比讀取所有行立刻。見Get last 10 lines of very large text file > 10GB和How to read last 「n」 lines of log file
反轉while文件,取兩個元素並將其反轉一次?對我來說似乎很奇怪,但應該工作。 – HimBromBeere
只有當你關心線條的順序時,才需要第二個反轉,從她的問題描述中猜測它可能不是必需的,但只是給出一個包含它的完整答案。 – Staeff
簡單的ReadAllLines
結果存儲到一個變量,不是拿兩個最後的:
var allText = File.ReadAllLines("C:\\test.log");
var lastLines = allText.Skip(allText.Length - 2);
您可以使用StreamReader
結合使用Queue<string>
,因爲您必須以任何方式讀取整個文件。
// if you want to read more lines change this to the ammount of lines you want
const int LINES_KEPT = 2;
Queue<string> meQueue = new Queue<string>();
using (StreamReader reader = new StreamReader(File.OpenRead("C:\\test.log")))
{
string line = string.Empty;
while ((line = reader.ReadLine()) != null)
{
if (meQueue.Count == LINES_KEPT )
meQueue.Dequeue();
meQueue.Enqueue(line);
}
}
現在你可以用這兩條線像這樣:
string line1 = meQueue.Dequeue();
string line2 = meQueue.Dequeue(); // <-- this is the last line.
或者把它添加到RichTextBox
:
使用File.ReadAllLines
將讀取整個文本,然後使用Linq
將迭代已經有紅線。此方法在一次運行中完成所有操作。
以前所有的答案返回所請求的最後幾行之前熱切加載所有的文件在內存中。如果文件很大,這可能是個問題。幸運的是,它很容易避免。
public static IEnumerable<string> ReadLastLines(string path, int count)
{
if (count < 1)
return Enumerable.Empty<string>();
var queue = new Queue<string>(count);
foreach (var line in File.ReadLines(path))
{
if (queue.Count == count)
queue.Dequeue();
queue.Enqueue(line);
}
return queue;
}
這隻會保存在內存中的最後n
讀取線避免使用大文件存儲問題。
string line;
string[] lines = new string[]{"",""};
int index = 0;
using (StreamReader reader = new StreamReader(File.OpenRead("C:\\test.log")))
{
while ((line = reader.ReadLine()) != null)
{
lines[index] = line;
index = 1-index;
}
}
// Last Line -1 = lines[index]
// Last line = lines[1-index]
ty此工作完美 –