日期:2014-05-20  浏览次数:21027 次

C#写入文件到底要怎么写?
FileStream   NewFile   =   new   FileStream( "C:\\aa.txt ",FileMode.Create,FileAccess.Write);
StreamWriter   NewStream   =   new   StreamWriter(NewFile);
NewStream.WriteLine( "hhhhjhjhjj ");
我这样为什么文件是创建了就是没写入,不知道是什么原因,请各位大哥,帮忙看看

------解决方案--------------------
NewStream.Flush();
NewStream.Close();
------解决方案--------------------
同意楼上..

可以没有
NewStream.Flush();

但是不能没有
NewStream.Close();

------解决方案--------------------

------解决方案--------------------
这么快就解决咯。。。。
------解决方案--------------------
StreamWriter NewStream = new StreamWriter( "C:\\aa.txt ");
NewStream.WriteLine( "hhhhjhjhjj ");
NewStream.Close();
------解决方案--------------------
/*
WriteFileTest()
{
Console.WriteLine(DateTime.Now.ToString());

FileStream fStream = new FileStream( "c:\\bb.txt ",FileMode.Append);
const int length = 1024 * 6;
StringBuilder strvalue = new StringBuilder();
//Console.WriteLine( "read start " + DateTime.Now.Millisecond.ToString());
for(int i = 0; i < 70000;++i)
{
for(int k = 0;k < 100;++k)
{
strvalue.Append(i);
strvalue.Append( ";11 ");
strvalue.Append( ";22 ");
strvalue.Append( ";ggg ");
strvalue.Append( ";fffff ");
strvalue.Append( ";aaaa ");
strvalue.Append( ";22 ");
strvalue.Append( ";ggg ");
strvalue.Append( ";fffff ");
strvalue.Append( ";发大幅度 ");
strvalue.Append( ";aaaa ");
strvalue.Append( ";fffff ");
strvalue.Append( ";发大幅度 ");
strvalue.Append( "\r\n ");
}

//Console.WriteLine( "read end " + DateTime.Now.Millisecond.ToString());
byte[]ImageContent = System.Text.Encoding.Default.GetBytes(strvalue.ToString());
//Console.WriteLine( "write start " + DateTime.Now.Millisecond.ToString());
fStream.Write(ImageContent,0,ImageContent.Length);
//Console.WriteLine( "write end " + DateTime.Now.Millisecond.ToString());
strvalue.Remove(0,strvalue.Length);
}
fStream.Close();
Console.WriteLine(DateTime.Now.ToString());
}
经过测试,这段代码的运行速度是最快的,写一个 477M 的文件耗时25s,效率的
瓶颈是内部的100次循环,组装字符串,IO 写操作非常快

各位看还有什么优化的措施

*/