Java 在 Linux 上存储应用程序数据(非用户特定)的位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1510104/
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
Where to store application data (non-user specific) on Linux
提问by William
In my OSGi-based Java app I am developing a bundle to provide the rest of the system with access to the file system. In addition to providing access to the user home directory, I also wish to provide access to a non-user specific area. Exactly what this area will be used for is as yet undetermined, but it will not be for preferences (handled by a different bundle), however it may be used to store data that could change at runtime.
在我的基于 OSGi 的 Java 应用程序中,我正在开发一个包来为系统的其余部分提供对文件系统的访问。除了提供对用户主目录的访问之外,我还希望提供对非用户特定区域的访问。该区域的确切用途尚未确定,但它不会用于首选项(由不同的包处理),但它可用于存储可能在运行时更改的数据。
I intend on using the following directories for this purpose:
我打算为此使用以下目录:
- Windows Vista & Windows 7: “\ProgramData”.
- Windows XP: “\Documents and Settings\All Users“.
- Mac OS X: “/Library/Application Support”.
- Windows Vista 和 Windows 7:“\ProgramData”。
- Windows XP:“\Documents and Settings\All Users”。
- Mac OS X:“/库/应用程序支持”。
Where is a sensible equivalent in Linux and how do I get a handle on it from my Java code?
在 Linux 中哪里有合理的等价物,我如何从我的 Java 代码中处理它?
采纳答案by Rob Hruska
It depends on what kind of data you're planning on storing. This answer is under the premise that you're storing and modifying data at runtime.
这取决于您计划存储的数据类型。这个答案是在您在运行时存储和修改数据的前提下。
Contrary to what others have suggested, I would recommend against using /usr/share
for storage. From the Filesystem Hierarchy Standard:
与其他人的建议相反,我建议不要使用/usr/share
存储。从文件系统层次结构标准:
The /usr/share hierarchy is for all read-only architecture independent data files.
/usr/share 层次结构适用于所有与只读体系结构无关的数据文件。
As you're modifying data, this goes against the read-only nature of the /usr
subsystem.
当您修改数据时,这与/usr
子系统的只读性质背道而驰。
A seemingly better place to store your application state data would be /var
, or more specifically, /var/lib
. This also comes from the Hierarchy Standard. You could create a /var/lib/myapp
, or if you're also using things like lock files or logs, you could leverage /var/lock
or /var/log
.
一个看似更好的存储应用程序状态数据的地方是/var
,或者更具体地说,/var/lib
. 这也来自层次标准。您可以创建一个/var/lib/myapp
,或者如果你也使用像锁文件或日志,你可以利用/var/lock
或/var/log
。
Have a deeper look at the standard as a whole (linked to above) - you might find a place that fits what you want to do even better.
更深入地了解整个标准(与上文相关联) - 您可能会找到一个更适合您想做的事情的地方。
Like Steve K, I would also recommend using the Preferences API for application preference data.
像Steve K一样,我也建议使用 Preferences API 来获取应用程序首选项数据。
回答by Locksfree
If it is non user specific, you can probably store it under /usr/share/appname
如果它不是特定于用户的,您可以将其存储在 /usr/share/appname 下
回答by catwalk
In /usr/share or /usr/local/share folders
在 /usr/share 或 /usr/local/share 文件夹中
回答by James R. Perkins
Do you want to hard-code it like that. You could use the System.getProperty("user.home") to get the users home so it's more platform independent.
你想像那样硬编码它。您可以使用 System.getProperty("user.home") 将用户带回家,使其更加独立于平台。
回答by Steve K
Since you are using Java, have you looked at the Preferences API?
由于您使用的是 Java,您是否看过Preferences API?
From the introduction:
从介绍:
Applications require preference and configuration data to adapt to the needs of different users and environments. The java.util.prefs package provides a way for applications to store and retrieve user and system preference and configuration data. The data is stored persistently in an implementation-dependent backing store. There are two separate trees of preference nodes, one for user preferences and one for system preferences
应用程序需要偏好和配置数据以适应不同用户和环境的需求。java.util.prefs 包为应用程序提供了一种存储和检索用户和系统首选项和配置数据的方法。数据永久存储在依赖于实现的后备存储中。有两种独立的偏好节点树,一种用于用户偏好,一种用于系统偏好
I'd let the built in API do the work.
我会让内置的 API 来完成这项工作。
回答by starblue
It depends.
这取决于。
Global configuration -> /etc/appname
Read-only, independent of machine architecture -> /usr/share/appname
Read-only, machine specific -> /usr/lib/appname
Read-write -> /var/lib/appname
全局配置 -> /etc/appname
只读,独立于机器架构 -> /usr/share/appname
只读,特定于机器 -> /usr/lib/appname
读写 -> /var/lib/appname
No guarantee for completeness, please check the Filesystem Hierarchy Standard.
不保证完整性,请检查文件系统层次结构标准。
回答by Pascal Thivent
The freedesktop.org(previously known as the X Desktop Group) project has defined some standards for this in the XDG Base Directory Specification.
该freedesktop.org(以前称为X桌面组)项目已在定义了一些标准,这XDG基本目录规范。
In your case, I'd have a look at $XDG_DATA_DIRS
:
在你的情况下,我会看看$XDG_DATA_DIRS
:
$XDG_DATA_DIRS
defines the preference-ordered set of base directories to search for data files in addition to the$XDG_DATA_HOME
base directory. The directories in$XDG_DATA_DIRS
should be seperated with a colon ':'.If
$XDG_DATA_DIRS
is either not set or empty, a value equal to/usr/local/share/:/usr/share/
should be used.
$XDG_DATA_DIRS
定义除基本目录外还搜索数据文件的优先顺序的一组$XDG_DATA_HOME
基本目录。中的目录$XDG_DATA_DIRS
应该用冒号“:”分隔。如果
$XDG_DATA_DIRS
未设置或为空,/usr/local/share/:/usr/share/
则应使用等于的值。
I warmly suggest to read the XDG Base Directory Specification.
我强烈建议阅读XDG 基本目录规范。
回答by david1024
I know this is an old question, but according to https://en.wikipedia.org/wiki/Filesystem_Hierarchy_Standard(which seems to be updated and correct as of July 2015)...
我知道这是一个老问题,但根据https://en.wikipedia.org/wiki/Filesystem_Hierarchy_Standard(截至 2015 年 7 月似乎已更新和正确)...
Assuming that the datafiles are understood to not meet the requirements of
/tmp
or/var/tmp
then/usr/local/share/theApp
or/usr/local/theApp
.
假设数据文件被理解为不满足
/tmp
or/var/tmp
then/usr/local/share/theApp
or的要求/usr/local/theApp
。