Is there something like the "opposite" of the crop?

I understand that I cannot store my complete data in memory, so I want to transfer parts to memory and work with them, and then write them back.

The output is a very useful keyword, it saves the entire collection of counter usage and stores the index, ...

But when I want to shift IEnumerable with yield around and write them back to the collection / file, do I need to use the enumerator concept or is there something similar like the opposite of yield? I head RX, but I don’t understand if it solves my problem?

  public static IEnumerable<string> ReadFile() { string line; var reader = new System.IO.StreamReader(@"c:\\temp\\test.txt"); while ((line = reader.ReadLine()) != null) { yield return line; } reader.Close(); } public static void StreamFile() { foreach (string line in ReadFile()) { WriteFile(line); } } public static void WriteFile(string line) { // how to save the state, of observe an collection/stream??? var writer = new System.IO.StreamWriter("c:\\temp\\test.txt"); writer.WriteLine(line); writer.Close(); } 
+4
source share
1 answer

In your case, you can pass an IEnumerable<string> directly to WriteFile:

 public static void WriteFile(IEnumerable<string> lines) { // how to save the state, of observe an collection/stream??? using(var writer = new System.IO.StreamWriter("c:\\temp\\test.txt")) { foreach(var line in lines) writer.WriteLine(line); } } 

Since the input is passed through IEnumerable<T> , the data will never be stored in memory.

Note that in this case, you can simply use File.ReadLines to do the reading, as it already passes the results back through IEnumerable<string> . With File.WriteAllLines your code can be executed as (although you can also just use File.Copy ):

 File.WriteAllLines(outputFile, File.ReadLines(inputFile)); 
+4
source

Source: https://habr.com/ru/post/1500822/


All Articles