文件在循环中写入 [英] File writes in a loop
问题描述
我需要在循环中将数据写入文件。我得到一个文件已经在
使用中第二个循环错误。问题是,我不知道文件的名称
,直到我在循环中。如何保持文件打开
循环写入?
while((data = log.ReadLine())!= null)
{
string date = data.Substring(0,10);
FileStream fileOut = new FileStream(dir + date + name,
FileMode.OpenOrCreate,FileAccess.Write);
fileOut.Write(Encoding.UTF8.GetBytes(data),0,data.Length);
fileOut.Flush();
}
I need to write data to a file in a loop. I get a "file already in
use" error on the second loop. The problem is, I don''t know the name
of the file until I''m in the loop. How can I keep a file open for
writing in a loop?
while ((data = log.ReadLine()) != null)
{
string date = data.Substring(0, 10);
FileStream fileOut = new FileStream(dir + date + name,
FileMode.OpenOrCreate, FileAccess.Write);
fileOut.Write(Encoding.UTF8.GetBytes(data), 0, data.Length);
fileOut.Flush();
}
推荐答案
while((data = log.ReadLine() )!= null)
{
string date = data.Substring(0,10);
using(FileStream fileOut = new FileStream( dir + date + name,
FileMode.OpenOrCreate,FileAccess.Write))
{
byte [] bytes = Encoding.UTF8.GetBytes (数据);
fileOut.Write(bytes,0,bytes.Length);
}
< Ad ***** ****@gmail.com在留言中写道
新闻:bc ***************************** ***** @ l64g2000 hse.googlegroups.com ...
while ((data = log.ReadLine()) != null)
{
string date = data.Substring(0, 10);
using (FileStream fileOut = new FileStream(dir + date + name,
FileMode.OpenOrCreate, FileAccess.Write))
{
byte[] bytes = Encoding.UTF8.GetBytes(data);
fileOut.Write(bytes, 0, bytes.Length);
}
<Ad*********@gmail.comwrote in message
news:bc**********************************@l64g2000 hse.googlegroups.com...
>我需要在循环中将数据写入文件。我得到一个文件已经在
使用中第二个循环错误。问题是,我不知道文件的名称
,直到我在循环中。如何保持文件打开
循环写入?
while((data = log.ReadLine())!= null)
{
string date = data.Substring(0,10);
FileStream fileOut = new FileStream(dir + date + name,
FileMode.OpenOrCreate,FileAccess.Write);
fileOut.Write(Encoding.UTF8.GetBytes(data),0,data.Length);
fileOut.Flush();
}
>I need to write data to a file in a loop. I get a "file already in
use" error on the second loop. The problem is, I don''t know the name
of the file until I''m in the loop. How can I keep a file open for
writing in a loop?
while ((data = log.ReadLine()) != null)
{
string date = data.Substring(0, 10);
FileStream fileOut = new FileStream(dir + date + name,
FileMode.OpenOrCreate, FileAccess.Write);
fileOut.Write(Encoding.UTF8.GetBytes(data), 0, data.Length);
fileOut.Flush();
}
5月16日下午3:35 ,AdemusPr ... @ gmail.com写道:
On May 16, 3:35 pm, AdemusPr...@gmail.com wrote:
我需要在循环中将数据写入文件。我得到一个文件已经在
使用中第二个循环错误。问题是,我不知道文件的名称
,直到我在循环中。如何保持文件打开
循环写入?
while((data = log.ReadLine())!= null)
{
string date = data.Substring(0,10);
FileStream fileOut = new FileStream(dir + date + name,
FileMode.OpenOrCreate,FileAccess.Write);
fileOut.Write(Encoding.UTF8.GetBytes(data),0,data.Length);
fileOut.Flush();
}
I need to write data to a file in a loop. I get a "file already in
use" error on the second loop. The problem is, I don''t know the name
of the file until I''m in the loop. How can I keep a file open for
writing in a loop?
while ((data = log.ReadLine()) != null)
{
string date = data.Substring(0, 10);
FileStream fileOut = new FileStream(dir + date + name,
FileMode.OpenOrCreate, FileAccess.Write);
fileOut.Write(Encoding.UTF8.GetBytes(data), 0, data.Length);
fileOut.Flush();
}
你要在循环中打开多个文件吗?如果是,你
不想关闭它们那么你可以
使用一个哈希表并使用dir + date + name作为键和
filestream作为对象。
are you going have multiple files open in the loop?if yes and you
dont want to close them then you could
use a a hashtable and use dir + date + name as the key and the
filestream as the object.
5月16日,3:35 * pm,AdemusPr ... @ gmail.com写道:
On May 16, 3:35*pm, AdemusPr...@gmail.com wrote:
我需要在循环中将数据写入文件。我得到一个文件已经在
使用中第二个循环错误。问题是,我不知道文件的名称
,直到我在循环中。如何保持文件打开
循环写入?
while((data = log.ReadLine())!= null)
{
string date = data.Substring(0,10);
FileStream fileOut = new FileStream(dir + date + name,
FileMode.OpenOrCreate,FileAccess.Write);
fileOut.Write(Encoding.UTF8.GetBytes(data),0,data.Length);
fileOut.Flush();
}
I need to write data to a file in a loop. I get a "file already in
use" error on the second loop. The problem is, I don''t know the name
of the file until I''m in the loop. How can I keep a file open for
writing in a loop?
while ((data = log.ReadLine()) != null)
{
string date = data.Substring(0, 10);
FileStream fileOut = new FileStream(dir + date + name,
FileMode.OpenOrCreate, FileAccess.Write);
fileOut.Write(Encoding.UTF8.GetBytes(data), 0, data.Length);
fileOut.Flush();
}
你的问题很神秘,所以我猜这里但也许你
需要一个:
fileOut.Close();
刷新后的
?br />
甚至可能是:
fileOut.Dispose();
Your question is cryptic, so I am just guessing here but maybe you
need a:
fileOut.Close();
after the flush?
And maybe even a:
fileOut.Dispose();
这篇关于文件在循环中写入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!