簡體   English   中英

Java System.getProperty(“ user.home”)目錄缺少分隔符

[英]Java System.getProperty(“user.home”) directory missing separator

每當用戶輸入“〜”作為參數時,我的程序都會將其替換為System.getProperty(“ user.home”)。

調試后,我看到它將“〜”替換為“ C:UsersSoulBeaver”,而不是“ C:/ Users / SoulBeaver”。

通過前面有關不正確的user.home文件夾的問題 ,我發現Java試圖從以下位置獲取路徑:

HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders\\

但是,我使用的是Windows 8,似乎沒有錯:

在這一點上,我假設Java“吃了”反斜杠...那么如何防止這種情況發生呢?

更新

由於請求了代碼,因此就在這里。 這摘自艾倫·霍魯布(Allen Holub) 解決Java的配置問題

/**
 * 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_13IntelliJ IDEA 12.1.3

您正在使用基於正則表達式的替換。 在Java正則表達式的替換模式中, '\\'字符是特殊字符。 您需要Matcher.quoteReplacement()用戶主目錄傳遞給Matcher.quoteReplacement()然后再將其用作替換模式(如相關方法javadoc所述 )。

可能下面可以給出一些信息。

竊聽器

討論

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM