问题描述
我正在编写一个Java应用程序.当前,这会在.jar文件所在的目录中生成一个名为"trace.txt"的文件.有时这可能会导致文件权限出现问题,例如写入C:\ Program Files \ Foo \ trace.txt.
在对SO进行了一些研究之后,看来一般的意见是为给定的用户登录到Application Data \ Foo \.这与其他操作系统的位置不同.
在这种情况下,最佳做法是什么?我正在使用java.util.logging.
您始终可以使用USER_HOME,在Linux或〜p中由〜标识
System.getProperty("user.home");在Java中.
您会看到许多应用程序都有自己的.myappname文件夹,并放置配置文件,并在其中登录.
这将映射到linux中的/home/user/.myapp或Windows中的C:/Documents and settings/user/.myapp,并且应始终可写.
I'm writing a java application. Currently this produces a file called "trace.txt" in the same directory the .jar file lives. This can sometimes cause issue with file permissions, eg writing to C:\Program Files\Foo\trace.txt.
After a bit of research on SO it seems the general opinion is to log to Application Data\Foo\ for the given user. This won't be the same location for other OS's.
What's the best practice in this situation? I'm using java.util.logging.
You could always use the USER_HOME, identified by ~ in Linux or
System.getProperty("user.home"); in java.
You'll see lots of apps have their own .myappname folder, and put config files, logs in there.
This will map to /home/user/.myapp in linux or C:/Documents and settings/user/.myapp in windows, and should always be writable.
这篇关于如何以独立于平台的方式为Java应用程序选择日志文件的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!