在没有延迟时间的情况下将记录写入文件 [英] Writing Record to File without the Delay Time

查看:104
本文介绍了在没有延迟时间的情况下将记录写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将记录写入文件时是否存在消除延迟的方法/过程。我记录到文件但是当它被中断(未知原因,用户结束它等)时,它有时不会将记录写入文件。

Is there a way/process to eliminate the delay when writing a record to a file. I am wirting records to a file but when it is interrupted (unknown cause, user ended it, etc) it will sometimes not write the records to the file.

推荐答案

我无法改变物理定律! - Scotty,The Naked Time-1966



完成所有事情需要时间。

打开文件,

从磁盘加载数据(到一个缓冲区),用于包含写入新记录的位置的扇区,

将新记录数据传输到缓冲区,

将缓冲区写回磁盘,

关闭文件(更新文件修改时间戳,磁盘分配表等)



系统可以在上面的任何一点被中断,并且无法恢复。
"I cannot change the laws of physics!" - Scotty, "The Naked Time" -1966

It takes time to do everything.
Open the file,
load the data (to a buffer) from the disk for the sector(s) containing the location where the new record will be written,
transfer the new record data to the buffer,
write the buffer back to the disk,
close the file (updating the file modification time stamp, disk allocation tables, etc.)

The system can be interrupted at any point above and there's no way to recover.


你可以在每个之后使用streamwriter或streamwriter.flush()中的autoflush = true。 writeline。



使用(StreamWriter outStream = new StreamWriter(fromPath + outFileName + fileSuffix))

{

//将写入文件设置为自动刷新,以便立即写入每一行。

outStream.AutoFlush = true;

//

// ...程序集我在这里

//

outStream.WriteLine(strText);

}
you would use the autoflush = true within streamwriter or streamwriter.flush() after each writeline.

using (StreamWriter outStream = new StreamWriter(fromPath + outFileName + fileSuffix))
{
// set the writing file to be auto-flushed so as each line gets written right away.
outStream.AutoFlush = true;
//
// ... program stream here
//
outStream.WriteLine(strText);
}


这篇关于在没有延迟时间的情况下将记录写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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