- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
每当用户输入“~”作为参数时,我的程序都会将其替换为 System.getProperty("user.home")。
调试后,我发现这将“~”替换为“C:UsersSoulBeaver”,而不是“C:/Users/SoulBeaver”。
正在经历previous questions about incorrect user.home folders ,我发现Java尝试从
获取路径HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell 文件夹\
但是,我使用的是 Windows 8,似乎没有任何问题:
此时我假设 Java“吃掉”了反斜杠...那么我该如何防止这种情况发生呢?
更新
既然请求了代码,就放在这里。这取自 Allen Holub 的 Solving Java's Configuration Problem
/**
* For every enum element in the array, treat keys[i].name() as a key
* and load the associated value from the following places (in order):
*
* <ol>
* <li>a -D command-line switch (in System properties)</li>
* <li>if no -D value found, an environment variable with the same name as the key</li>
* <li>if no environment found, the default stored in the Enum element itself</li>
* </ol>
*
* That value must identify an existing directory in the file system, and a
* File representing that location can be retrieved from {@link #directory(Enum)}.
*
* @param keys The values() array associated with the enum that's using this class.
* @throws IllegalStateException if a given key doesn't have a value associated with it
* or if that value doesn't identify an existing directory.
*/
public LocationsSupport(T[] keys) throws IllegalStateException {
StringBuilder logMessage = new StringBuilder("Loaded environment/-D properties:\n");
try {
for (T element : keys) {
String how = "???";
String key = element.name();
String value;
if ((value = System.getProperty(key)) != null)
how = "from system property (-D)";
else if ((value = System.getenv(key)) != null)
how = "from environment";
else if ((value = element.defaultValue()) != null)
how = "from default. Mapped from: " + value;
if (value != null)
value = value.replaceAll("~", System.getProperty("user.home"));
if (value == null || value.isEmpty())
throw new IllegalStateException("Value for " +key +" cannot be null or empty.");
File location = new File(value);
createLocationIfNecessary(location, element.createIfNecessary());
if (!location.isDirectory())
throw new IllegalStateException("Location specified in "
+key
+" (" +asString(location) +") "
+"does not exist or is not a directory.");
dictionary.put(key, location);
logMessage.append("\t");
logMessage.append(key);
logMessage.append("=");
logMessage.append(asString(location) );
logMessage.append(" (");
logMessage.append(how);
logMessage.append(")\n");
}
} finally {
if (log.getAllAppenders() instanceof NullEnumeration)
System.err.println(logMessage);
else
log.info(logMessage);
}
}
尝试查找 CONFIG 的默认位置失败:
public enum Places implements Locations {
CONFIG ("~/config"),
HOME ("~"),
TMP ("~/tmp", true),
TERM_STORE ("~/tmp/indices/term_store/", true),
RESOURCE_STORE ("~/tmp/indices/resource_store/", true),
PERSON_STORE ("~/tmp/indices/person_store/", true);
我使用的是 Java 1.7.0_13 和 IntelliJ IDEA 12.1.3
最佳答案
您正在使用基于正则表达式的替换。在 java 正则表达式的替换模式中,'\'
字符是特殊的。在将用户主目录用作替换模式之前,您需要通过 Matcher.quoteReplacement()
传递用户主目录(如 javadoc for the relevant method 中所述)。
关于Java System.getProperty ("user.home") 目录缺少分隔符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16545885/
当我使用 Html.ActionLink 时,我发现 @Html.ActionLink("Home", "Index", "home")生成此字符串 Home ,那么为什么地址是\home\index
我有一个现有的 webapi 2 应用程序需要添加基本的前端。现有的 webapi Controller 已在名为 xController yController 的 Controllers 目录根目
我家里有一个应用程序小部件,它会定期更新。如果 HOME 不可见,我想停止更新。我使用一项服务来注册屏幕开/关接收器,以便在屏幕开/关时开始/停止更新应用程序小部件。但我不知道如何检测屏幕是否打开但用
我在使用 create-react-app 时遇到了这个问题,我得到了一个错误: Home does not contain an export named Home. 下面是我如何设置我的 App.
最近我不得不在 .bashrc 中更新我的 JAVA 环境变量 echo $JAVA_HOME # prints out /usr/java/... echo $(JAVA_HOME) # raise
$ cat Makefile all: echo VAR is ${HOME} echo VAR is $${HOME} 给予 $ make echo VAR is /home/abc
我尝试在 google home 模拟器中测试我的项目。直到昨天它还在工作,但今天它显示错误“完成您的请求时出错”网址:“https://console.actions.google.com/proj
我已经实现了具有 OnOff 特性的风扇打开/关闭功能和具有 Fanspeed 特性的风扇模式(“高”、“中”、“低”),现在我想实现百分比 Controller 。例如:将风扇速度设置为 50%。我
我制作了一个小型 Google Home 应用程序,我的服务通过 SimpleMessage + Card 返回响应。 在 console.actions.google.com 模拟器中运行应用程序时
我还在用log4j-1.2.14.jar 在我尝试使用的属性中 log4j.appender.R=org.apache.log4j.DailyRollingFileAppender log4j.app
我正在使用 google smarthome actions for IOT...我更新了我的操作 URL 和帐户链接详细信息。当我尝试在模拟器中启用测试以将我的 TestAPP 部署到云时,它失败并
我在使用 Spring Boot 时遇到了一些问题。 当我使用 @RequestMapping(value = "/home") 时,一切正常。但是当我使用 @RequestMapping(name
起初,文件图标在我的 Ubuntu 20.04 中丢失。后来我运行 sudo root ,但是点击桌面上的图标打开文件还是有些麻烦。现在我运行 sudo nautilus ,我看到 Home 是/ro
我想在 ionic 中制作一个应用程序,所以我通过命令在 ionic 中添加了一些自定义页面: ionic g 页面用户 但是我启动 ionic 服务器它显示错误 Runtime Error Cann
非常感谢您的帮助。 标题说明一切:使用有什么区别: echo `basename $HOME` 和 echo $(basename $HOME) 请注意,我知道 basename 命令的作用,两种语法
在 Rust 中,默认情况下,文件放在 $HOME/.cargo 和 $HOME/.rustup 中。有什么方法可以覆盖这些默认值吗? 我正在尝试调试一个模糊的问题,我想尝试更改文件位置。 最佳答案
我正在尝试以 root 身份备份机器的一些关键文件和目录,包括一些/home 数据,手动挑选一些文件以减小 tarball 大小。大多数情况下一切正常,因为大多数文件无论如何都归 root 所有,但说
如果菜单项是链接,屏幕阅读器应该将其读作“主页、链接、菜单项”还是“主页、菜单项”? 目前,屏幕阅读器将它读作“主页、链接、菜单项”,但我得到的是关于它的混合信息。一些信息说它应该是“主页、链接、菜单
我在一个有多个组织的项目上使用 php codeigniter。每个组织都会有自己的网站,我需要根据组织名称将每个用户重定向到其组织网站。 我得到了部分运行。以本地主机为例: localhost/ap
我正在 Mac OS X 上安装 Ruby on Rails。我遵循的教程说要添加: [[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/script
我是一名优秀的程序员,十分优秀!