Java 为什么我的程序会创建空的 .lck 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2723280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is my program creating empty .lck files?
提问by Roman
I am trying to use Java Logger. I get my logger file (name.log) with the content, it works and I also get an empty name.log.lck
file.
我正在尝试使用 Java Logger。我得到了包含内容的记录器文件 (name.log),它可以工作,而且我也得到了一个空name.log.lck
文件。
Why does this file appear, what program is creating them and how I can remove this behavior?
为什么会出现这个文件,是什么程序在创建它们以及如何消除这种行为?
回答by DVK
".lck" sounds suspiciously like a lock file. You didn't say which logger you use but at elast one of them uses .lck files for locks - see this reply:
“.lck”听起来像一个锁文件。您没有说您使用的是哪个记录器,但最后其中一个使用 .lck 文件进行锁定 - 请参阅此回复:
When the log file is created, a separet lock file called (in your case) "dbslogfile.txt.lck" is also created. The Logger uses this as a mutual exclusion mechanism for access to the actual log file. It doesn't seem to have been able to create it (it would have to create the lock file before the log file, of course).
创建日志文件时,还会创建一个名为(在您的情况下)“dbslogfile.txt.lck”的分离锁文件。Logger 将此用作访问实际日志文件的互斥机制。它似乎无法创建它(当然,它必须在日志文件之前创建锁定文件)。
回答by Larry K
lock files are commonly used in Unix/Linux to ensure exclusive or serial access to an important resource.
锁定文件通常用于 Unix/Linux 以确保对重要资源的独占或串行访问。
In this case, the resource is the log file itself--you wouldn't want two or more logger instances trying to write to the same log file at the same time. That would not work out well at all. More about file locking
在这种情况下,资源是日志文件本身——您不希望两个或多个记录器实例同时尝试写入同一个日志文件。那根本行不通。有关文件锁定的更多信息
As Peter Barrett saysabout the Java Logger:
正如 Peter Barrett关于 Java Logger所说:
When the log file is created, a separate lock file called (in your case) "dbslogfile.txt.lck" is also created. The Logger uses this as a mutual exclusion mechanism for access to the actual log file.
创建日志文件时,还会创建一个名为(在您的情况下)“dbslogfile.txt.lck”的单独锁定文件。Logger 将此用作访问实际日志文件的互斥机制。
回答by Rana
.lck
is used by the handler(file handler) to lock the file in order to delete this file. You need to close the Handler that is associated with that logger object before you close your program.
.lck
处理程序(文件处理程序)使用它来锁定文件以删除此文件。在关闭程序之前,您需要关闭与该记录器对象关联的处理程序。
Here is sample lines how you can close associated handler:
以下是如何关闭关联处理程序的示例行:
for(Handler h:log.getHandlers())
{
h.close(); //must call h.close or a .LCK file will remain.
}
回答by Eric Leschinski
Why are there .LCK files hanging around?
为什么会有 .LCK 文件?
In Linux I had these .LCK
files:
在 Linux 中,我有这些.LCK
文件:
> ls -l
-rw-r--r-- 1 el el 4810 Feb 9 2013 mybackground.gif
-rw-r--r-- 1 el el 33 Feb 9 2013 mybackground.gif.LCK
-rwxr--r-- 1 el el 193 Feb 9 2013 my_file.html
-rw-r--r-- 1 el el 33 Feb 9 2013 my_file.html.LCK
Some program is creating these files. You have to find out which one it is. The program doing this will be a program that tries to do some kind of operation on these files across a network or perhaps even on this disk. A poorly written program that synchronizes, copies or deletes files on a disk might elect to use lock files in its operation.
某些程序正在创建这些文件。你必须找出它是哪一个。执行此操作的程序将尝试通过网络或什至在该磁盘上对这些文件进行某种操作。同步、复制或删除磁盘上文件的编写不当的程序可能会选择在其操作中使用锁定文件。
My lock file contained the following information:
我的锁定文件包含以下信息:
eric||[email protected]
It is safe to remove .LCK
files once the the irresponsible process that left them open has exited. The purpose of these files is to make sure two processes doing the same thing don't step on each other causing errors.
.LCK
一旦离开文件的不负责任的过程退出,删除文件是安全的。这些文件的目的是确保做同一件事的两个进程不会相互踩踏而导致错误。
The usage of .LCK
files is a poor programming practice violating the rule of "don't second guess yourself". Creating code that does redundant things just to make triple sure we did it right is a sign you are a bad programmer.
.LCK
文件的使用是一种糟糕的编程实践,违反了“不要怀疑自己”的规则。创建代码做多余的事情只是为了确保我们做对了,这表明你是一个糟糕的程序员。
Any program caught red handed littering these .LCK files hanging around should be judged negatively with extreme prejudice.
任何被发现乱扔这些 .LCK 文件的程序都应该受到极端偏见的负面评价。
In my case, it was a "DreamWeaver CS4 auto-synchronize" operation that was creating and leaving open these files. You'll have to delete these files by hand, and then figure out what action is causing these files to be left open, then submit bug reports to fix that software.
就我而言,它是一个“DreamWeaver CS4 自动同步”操作,它创建并保持打开这些文件。您必须手动删除这些文件,然后找出导致这些文件保持打开状态的操作,然后提交错误报告以修复该软件。