简体   繁体   English

如何将文本写入文件。 有什么办法可以打开文件写入文件?

[英]How to write text into file. And is there any way to open file file writing?

I have a foreach statement which executes 20,000 times, in each iteration I have to write a line to a file. 我有一个foreach语句,该语句执行20,000次,在每次迭代中,我都必须向文件写入一行。 The question is, Is there any way to open a file while writing to a file. 问题是,在写入文件时是否可以打开文件? Until and unless I close the stream I can't see the text in textfile. 直到并且除非关闭流,否则我看不到文本文件中的文本。 But I want to see the text without closing stream. 但是我想在不关闭流的情况下查看文本。 Please anybody can help me..... 请任何人都可以帮助我.....

thanq in advance 提前thanq

var fileName = Path.GetTempPath();
streamWriter sw=new streamWriter(fileName + "\\sampleFile.txt",true);
for (int i = 0; i <= 20000; i++)
{                    
   sw.WriteLine("vinod" + i);                   
}

After each write to the file, call .Flush() on the stream. 每次写入文件后,在流上调用.Flush()。 This will push the text to the file so you can see it during program execution. 这会将文本推送到文件,以便您在程序执行期间可以看到它。 Should be noted that this is probably quite a performance hog, so don't do it unless you have to for some reason. 应该注意的是,这可能只是性能上的浪费,因此除非出于某些原因,否则不要这样做。

If you can compute the contents of the file at once and don't have to continuously log information to it, write the text to the file when the content is ready. 如果您可以一次计算文件的内容并且不必连续记录信息,则在准备好内容后将文本写入文件中。

For example: 例如:

var fileName = Path.GetTempPath();
List<string> lines = new List<string>();
for (int i = 0; i <= 20000; i++)
{                    
   lines.Add("vinod" + i);                   
}

File.WriteAllLines(fileName, lines);

Otherwise go with flush for your stream or use File.AppendAllText (see MSDN ). 否则,请为您的流使用flush或使用File.AppendAllText (请参见MSDN )。

I wouldn't use file as a source for the viewer. 我不会将文件用作查看器的源。 If you are trying to do logger and viewer , then think about changing design: 如果您想做loggerviewer ,那么请考虑更改设计:

  1. Make some sort of source (but different than file, it can be file, but based the given conditions it is not optimal, as you going to update way too often) and make it accessible for both: logger and viewer. 制作某种源(但不同于文件,它可以是文件,但根据给定的条件,它不是最佳的,因为您将以太频繁的方式进行更新),并使其对记录器和查看器均可用。 To example, use a string , which both can access. 例如,使用一个都可以访问的string

  2. Make event and make viewer in a way, that it will form his own source to view. 制作事件并以某种方式制作查看器,以形成自己的查看源。

If viewer is a ListBox (to example), then 20000 items is pretty ok with second scenario. 如果查看器是一个ListBox (例如),那么在第二种情况下,可以使用20000个项目。 Add a line to file and send an event to ListBox at the same time, to update its content. 在文件中添加一行,并同时将事件发送到ListBox ,以更新其内容。 No need to Flush() or something. 无需Flush()东西。

Whenever it gets bigger or if you what to provide advanced features for the viewer: filtering, reloading and such - the first scenario is better (virtual mode will be perfect for ListBox ). 每当它变大时,或者如果您要为查看器提供高级功能:过滤,重新加载等-第一种情况就更好(虚拟模式对于ListBox是完美的)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM