简体   繁体   English

FileHandler 中的问题

[英]Issue in FileHandler

I'm trying to initialize a FileHandler to write to the given filename, with optional append using the foll syntax:我正在尝试初始化 FileHandler 以写入给定的文件名,并使用以下语法进行可选的附加:

FileHandler fh = null;
fh = new FileHandler("C:\\Users\\Desktop\\New_folder\\logs\\slate\\TodayLoad-2013-10-24.log",true); 

But I'm getting the exception as:但我得到的例外是:

java.io.IOException: Couldn't get lock for  C:\Users\Desktop\New_folder\logs\slate\TodayLoad-2013-10-24.log
    at java.util.logging.FileHandler.openFiles(Unknown Source)
    at java.util.logging.FileHandler.<init>(Unknown Source)
    at TodayLoad.Load.<init>(SlateSheetLoad.java:173)
    at TodayLoad.Load.SlateSheetLoad.main(SlateSheetLoad.java:423)

How can I solve this issue.我该如何解决这个问题。

Probably your file is getting accessed by another process.可能您的文件正在被另一个进程访问。 See this post that can help you couldn't get lock for .log file in Java请参阅这篇文章,它可以帮助您在 Java 中无法锁定 .log 文件

You will find that you get the error after 100 log files have been written.您会发现在写入 100 个日志文件后出现错误。 The root cause is a bug in Java.根本原因是 Java 中的错误。 See, for example,参见,例如,

System Property Controls the java.util.logging.FileHandler's MAX_LOCKS Limit系统属性控制 java.util.logging.FileHandler 的 MAX_LOCKS 限制

and

Increase java.util.logging.FileHandler MAX_LOCKS limit 增加 java.util.logging.FileHandler MAX_LOCKS 限制

These references also suggest a solution: set the system property "jdk.internal.FileHandlerLogging.maxLocks" well before the code that does the logging.这些参考文献还提出了一个解决方案:在执行日志记录的代码之前设置系统属性“jdk.internal.FileHandlerLogging.maxLocks”。 For example, by means of例如,通过

 // Set maxLocks to 200. The default is 100.
 System.setProperty("jdk.internal.FileHandlerLogging.maxLocks", "200");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM