StreamWriter.WriteLine()不起作用 [英] StreamWriter.WriteLine() is not working

查看:181
本文介绍了StreamWriter.WriteLine()不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着用StreamWriter.WriteLine(不是静态地)写几行,一次写一个.txt文件。

 常量字符串文件名=BasicTestInfo.txt; 
using(var writer = new StreamWriter(filename,false))
{
writer.WriteLine({0} 350 200 200 10 2 28 20 200 2500 1200 1 1,Player1) ;
writer.WriteLine({0} 300 150 150 4 2 15 18 150 2500 1000 1 0,Player2);
writer.WriteLine({0} 200 140 450 25 14 10 70 4500 2500 750 1 1,Player3);
writer.WriteLine({0} 175 120 400 15 3 8 50 3000 2500 850 1 0,Player4);
writer.WriteLine({0} 300 100 300 8 1 4 30 1000 2500 1200 1 0,Player5);
writer.WriteLine({0} 450 310 450 20 5 5 35 1500 2500 700 1 1,Player6);
}

每个玩家对象都是字符串符号。如果我使用不同的文件名(a.k.a.BasicTestInfo2.txt)运行它,它会在bin.Debug中创建该文件,但是它是空的。我知道我到达了使用块的内部(我把一个Console.WriteLine在那里),我知道我想截断这就是为什么我使用false为append(虽然用真或无参数替换false根本不解决问题)。

基本的问题是,即使文件被创建,文件中也没有文本行。

解决方案

是的,这是真的,在VB.net这(独家冲洗)不需要与默认设置,但与CSharp你需要一个Writer.Flush调用来强制写入。当然,Writer.Close()也会强制刷新。或者我们可以设置StreamWriter实例的AutoFlush属性:

  sw.AutoFlush = true; 
//获取或设置一个值,指示StreamWriter
//在每
// //调用StreamWriter.Write之后是否将其缓冲区刷新到基础流。

发件人: http://msdn.microsoft.com/en-us/library/system.io.streamwriter.autoflush(v = vs.110).aspx

I am trying to write several lines, one at a time, to a .txt file using StreamWriter.WriteLine (Not statically).

const string filename = "BasicTestInfo.txt";
        using (var writer = new StreamWriter(filename, false))
        {
            writer.WriteLine("{0} 350 200 200 10 2 28 20 200 2500 1200 1 1", Player1);
            writer.WriteLine("{0} 300 150 150 4 2 15 18 150 2500 1000 1 0", Player2);
            writer.WriteLine("{0} 200 140 450 25 14 10 70 4500 2500 750 1 1", Player3);
            writer.WriteLine("{0} 175 120 400 15 3 8 50 3000 2500 850 1 0", Player4);
            writer.WriteLine("{0} 300 100 300 8 1 4 30 1000 2500 1200 1 0", Player5);
            writer.WriteLine("{0} 450 310 450 20 5 5 35 1500 2500 700 1 1", Player6);
        }

Each of the player objects are string cosntants. If I run this with a different filename (a.k.a. BasicTestInfo2.txt) it creates that file in the bin.Debug, but it's empty. I know that I am reaching the inside of the using block (I put a Console.WriteLine in there), and I know that I want to Truncate which is why I am using false for append (Although replacing the false with true or no parameter at all does not fix the problem).

The basic problem is that even though the file gets created, there are no lines of text in the file.

解决方案

Yes it is true that in VB.net this (en exclusive flush) was not needed with the default settings but with CSharp you need a Writer.Flush call to force the write. Of course - Writer.Close() would force the flush as well. Alternatively we can set the AutoFlush Property of the StreamWriter instance:

sw.AutoFlush = true;
// Gets or sets a value indicating whether the StreamWriter 
// will flush its buffer to the underlying stream after every  
// call to StreamWriter.Write.

From: http://msdn.microsoft.com/en-us/library/system.io.streamwriter.autoflush(v=vs.110).aspx

这篇关于StreamWriter.WriteLine()不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆