问题陈述-我有一个文件逐行连续写入。我想要一个C#解决方案,该解决方案将在流程编写后立即逐行打印。
解决方案-我想使用Reactive
扩展名C#,在其中订阅流。
我尝试了下面的代码,但是如何打印每一行,
stream.Subscribe(e => Console.WriteLine(e.//how to print each line));
这里是完整代码,
using System;
using System.Collections.Generic;
using System.IO;
using System.Reactive.Concurrency;
using System.Reactive.Linq;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
using (FileStream fs = new FileStream(@"C:\Files\test_Copy.txt", FileMode.Open))
{
var stream = ObserveLines(fs);
stream.Subscribe(e => Console.WriteLine(e.//how to print each line));
}
}
public static IObservable<string> ObserveLines(Stream inputStream)
{
return ReadLines(inputStream).ToObservable(Scheduler.ThreadPool);
}
public static IEnumerable<string> ReadLines(Stream stream)
{
using (StreamReader reader = new StreamReader(stream))
{
while (!reader.EndOfStream)
yield return reader.ReadLine();
}
}
}
}