文件写入期间崩溃,如何恢复? [英] crash during file writing, how to recover ?

查看:92
本文介绍了文件写入期间崩溃,如何恢复?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在写一个必须可靠的商业程序。它必须对硬盘上的文件进行一些基本的读写操作,

以及软盘。

我有预见到一个潜在的问题。写入文件时程序可能会意外崩溃

。如果是这样,我的程序应该在启动期间检测到这一点,然后(在启动期间)可能删除添加到文件中的

数据并重做写入操作。


文件写入操作是原子的吗?即当你写一个文件时,

它会成功地做,或者说一半失败(例如写几个字母

而不是完成),或者不提交任何如果在这一点发生崩溃,那么对于文件的更改是什么?


我的下一个问题是如何处理商业编程?我想b $ b计划在另一个文件上写一个标志(比方说,一个简单的字符)(这个
表示文件写入即将开始),然后

在文件写入操作完成后删除此字符。

然后在启动时我只检查标志。如果没有删除标志,那么发生了b $ b崩溃,所以必须打开文件并摆脱任何垃圾。


有没有人做过类似的事情b4 ?如果是这样你是如何处理这个

崩溃的情况。我的应用程序可能会完全填满,如果我没有
处理这个权利。


顺便说一句,我使用的是java语言和api 。这可能会影响

如何写入文件,所以我想我应该提一下。

很多人感谢

Joseph

Hi
I''m writing a commercial program which must be reliable. It has to do
some basic reading and writing to and from files on the hard disk,
and also to a floppy.
I have foreseen a potential problem. The program may crash
unexpectedly while writing to the file. If so, my program should
detect this during startup, and then (during startup) probably delete the
data added to the file and redo the writing operation.

Are file writing operations atomic ? ie when you write to a file,
will it either do it succesfully, OR say half fail (eg write a few letters
and not finish), OR not commit any changes to the file if a crash at
this point occurs?

My next question is how is this handled in commercial programming? I
plan on writing a flag (say, a simple char) to another file (this
would signal that a file write is about to begin), and then
removing this char after the file writing operation is completed.
Then on startup i just check the flags. if flag hasn''t been removed a
crash occurred, so have to open file and get rid of any garbage.

Has anyone done anything similar b4? if so how did you handle this
crash scenario. My application could totally stuff up if i don''t
handle this right.

by the way, i''m using the java language and api. this might effect
how files are written to, so i thought i should mention this.
MANY THANKS
Joseph

推荐答案

2004年4月30日星期五01:36:56 GMT,Joseph< ka **** @ bigpond.com>写道或

引用:
On Fri, 30 Apr 2004 01:36:56 GMT, Joseph <ka****@bigpond.com> wrote or
quoted :
文件写入操作是原子的吗?即当你写一个文件时,它会成功地执行,或者说一半失败(例如写几个字母而不完成),或者如果在崩溃时没有对文件进行任何更改
这一点发生了吗?
Are file writing operations atomic ? ie when you write to a file,
will it either do it succesfully, OR say half fail (eg write a few letters
and not finish), OR not commit any changes to the file if a crash at
this point occurs?




想象一下写一张软盘。电源在中途完成了

写下你的一首曲目。然后你的软盘上有胡言乱语。

此外,胖和目录可能不同步。当你使用

CHKDSK / F时它试图解决这个问题。该文件包含乱码。你可能应该批量擦除并重新格式化这样的软盘。


检测问题的一种非常简单的方法是在
$之前写一个小文件b $ b你开始吧。你做你的事,退出时你擦除文件。你可以通过使用

bat语言进行创建测试和删除来对任何应用程序执行此操作。


当你开始时,检查文件是否存在。如果你看到它b / b
你假设最坏的,并要求从备份或你需要做的任何事情恢复以便安全地重新开始。

-

Canadian Mind Products,Roedy Green。

辅导,解决问题,经济合同编程。

参见 http://mindprod.com/jgloss/jgloss.html




" Roedy Green" < se*@mindprod.com.invalid>在消息中写道

news:45 ******************************** @ 4ax.com ...

"Roedy Green" <se*@mindprod.com.invalid> wrote in message
news:45********************************@4ax.com...
2004年4月30日星期五01:36:56 GMT,Joseph< ka **** @ bigpond.com>写道或
引用:
On Fri, 30 Apr 2004 01:36:56 GMT, Joseph <ka****@bigpond.com> wrote or
quoted :
文件写入操作是原子的吗?即,当你写一个文件时,它会成功地执行,或者说一半失败(例如写几个
字母并没有完成),或者如果在<崩溃时没有对文件进行任何更改这个点发生了吗?
Are file writing operations atomic ? ie when you write to a file,
will it either do it succesfully, OR say half fail (eg write a few lettersand not finish), OR not commit any changes to the file if a crash at
this point occurs?



想象一下写一张软盘。在写下你的一首曲目的过程中,电源会中途停止。然后你的软盘上有胡言乱语。
此外,脂肪和目录可能不同步。当你使用
CHKDSK / F时,它试图解决这个问题。该文件包含乱码。您可能应该批量擦除并重新格式化这样的软盘。

检测问题的一种非常简单的方法是在开始之前写入小文件。你做你的事,退出时你擦除文件。您可以通过使用蝙蝠语言进行创建测试和删除来对任何应用程序执行此操作。

启动时,检查文件是否存在。如果你看到它
你认为最糟糕的,并要求从备份或你需要做的任何事情恢复安全再次。

-
加拿大Mind Products,Roedy Green。
辅导,解决问题,经济合同编程。
参见 http://mindprod.com/jgloss/jgloss.html 的Java词汇表。




几年后我有一个有趣的情况前。我在硬盘上使用堆栈器

,因为它只有20兆字节。当我的电池耗尽时,我碰巧在b
做挤压式操作。在下一次

通电时,堆叠器在屏幕上发出一条消息,它发现它在挤压过程中坠毁并继续自动清理。

我印象深刻。



I had an interesting situation a few years ago. I was using stacker
on my hard disk since it was only 20 megabytes. And I happened to
be doing a squeeze type operation when my battery died. At the next
powerup, stacker gave a message on the screen that it discovered that
it crashed during a squeeze and proceeded to automatically clean up.
I was impressed.


Joseph写道:
Joseph wrote:

我正在写一个商业节目必须可靠。它必须对硬盘上的文件以及软盘上的文件进行一些基本的读写操作。

我预见到了一个潜在的问题。写入文件时,程序可能会意外崩溃。如果是这样,我的程序应该在启动时检测到这一点,然后(在启动期间)可能会删除添加到文件中的
数据并重做写入操作。

是文件写入操作原子?即当你写一个文件时,它会成功地执行,或者说一半失败(例如写几个字母而不完成),或者如果在崩溃时没有对文件进行任何更改
这一点出现了?

我的下一个问题是如何处理商业编程?我打算在另一个文件上写一个标志(比方说,一个简单的字符)(这个
表示文件写入即将开始),然后在文件后删除这个字符。写作操作完成。
然后在启动时我只需检查标志。如果标志没有被移除,则发生了崩溃,所以必须打开文件并清除任何垃圾。

有没有人做过类似的b4?如果是这样,你是如何处理这个
崩溃的情况。如果我不能正确处理这个问题,我的应用程序可能会完全填满。顺便说一句,我正在使用java语言和api。这可能会影响文件的写入方式,所以我想我应该提一下。
Hi
I''m writing a commercial program which must be reliable. It has to do
some basic reading and writing to and from files on the hard disk,
and also to a floppy.
I have foreseen a potential problem. The program may crash
unexpectedly while writing to the file. If so, my program should
detect this during startup, and then (during startup) probably delete the
data added to the file and redo the writing operation.

Are file writing operations atomic ? ie when you write to a file,
will it either do it succesfully, OR say half fail (eg write a few letters
and not finish), OR not commit any changes to the file if a crash at
this point occurs?

My next question is how is this handled in commercial programming? I
plan on writing a flag (say, a simple char) to another file (this
would signal that a file write is about to begin), and then
removing this char after the file writing operation is completed.
Then on startup i just check the flags. if flag hasn''t been removed a
crash occurred, so have to open file and get rid of any garbage.

Has anyone done anything similar b4? if so how did you handle this
crash scenario. My application could totally stuff up if i don''t
handle this right.

by the way, i''m using the java language and api. this might effect
how files are written to, so i thought i should mention this.




一种方法是写一个临时文件,然后写已成功完成

,文件已关闭,将

临时文件重命名为目标文件名。这样你就不会耗尽

的磁盘空间。如果您需要覆盖旧文件,请在重命名之前将其删除

。如果您的程序在临时文件创建期间崩溃,那么您将会遗留一个从未使用的损坏的临时文件

再次 - 没什么大不了的。


Calum



One approach is to write to a temporary file, then when writing has
completed successfully, and the file has been closed, rename the
temporary file to the target filename. That way you won''t run out of
disk space either. If you need to overwrite an old file, delete it just
before renaming. If your program crashes during temporary file
creation, you''ll be left with a damaged temp file that is never used
again - no big deal.

Calum


这篇关于文件写入期间崩溃,如何恢复?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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