[英]getProperty() always returns null - Java
在下面的代碼中,getProperty() 方法總是返回 null,即使我為
loadNumberOfAccounts();
所以它不是空的,下一個使用 getProperty() 的方法將返回空。 存儲屬性完美無缺。
prop.get
也返回空值。
public class AccountList {
private static Properties prop = new Properties();
private static final File DIRECTORY = Constants.DIRECTORY;
private static final File BANK_ACCOUNTS_FILE = new
File(DIRECTORY.getName() + File.separator + "Accounts.txt");
private static final FileOutputStream WRITER = assignWriter();
private static final FileInputStream READER = assignReader();
private static final List<Bank> ACCOUNT_LIST = new ArrayList<>();
private static final String NUMBER_OF_ACCOUNTS = "NumberOfAccounts";
private static final String BANK_ACCOUNT_NAME = "BankAccount";
private static final String SEPERATOR = "-";
private static int accounts = 0;
static {
BANK_ACCOUNTS_FILE.setReadOnly();
}
private static FileInputStream assignReader() {
FileInputStream tmp = null;
try {
tmp = new FileInputStream(BANK_ACCOUNTS_FILE);
} catch (FileNotFoundException e) {
EvaluateErrors.eveluateException(e);
}
if (tmp == null) {
EvaluateErrors.eveluateException(new NullPointerException());
}
return tmp;
}
private static FileOutputStream assignWriter() {
BANK_ACCOUNTS_FILE.setWritable(true);
FileOutputStream tmp = null;
try {
tmp = new FileOutputStream(BANK_ACCOUNTS_FILE);
} catch (IOException e) {
EvaluateErrors.eveluateException(e);
}
if (tmp == null) {
EvaluateErrors.eveluateException(new NullPointerException());
}
BANK_ACCOUNTS_FILE.setReadOnly();
return tmp;
}
public static void loadAccounts() {
try {
prop.load(READER);
} catch (IOException e) {
EvaluateErrors.eveluateException(e);
}
loadNumberOfAccounts();
loadAccountsList();
for (Bank object : ACCOUNT_LIST) { //to try and debug
System.out.println(object.getName());
System.out.println(object.getId());
System.out.println(object.getBankAccountFunds());
System.out.println(object.getOverdrawFee());
}
}
public static void saveAccounts() {
BANK_ACCOUNTS_FILE.setWritable(true);
saveAccountsList();
saveNumberOfAccounts();
try {
prop.store(WRITER, null);
} catch (IOException e) {
EvaluateErrors.eveluateException(e);
} finally {
CloseLogs();
BANK_ACCOUNTS_FILE.setReadOnly();
}
private static void loadNumberOfAccounts() {
String string = prop.getProperty(NUMBER_OF_ACCOUNTS);
System.out.println(string); // always null
try {
accounts = Integer.parseInt(string);
} catch (NumberFormatException e) { // this exception is thrown
// because string is always null.
EvaluateErrors.eveluateException(e);
}
// more methods ...
我確信答案很簡單,我剛剛錯過了它,但我已經用盡了所有選項。
Accounts.txt 看起來像這樣
NumberOfAccounts=1
OtherStoredProperties=etc
etc...
更新
我發現了這個錯誤,但不知道它為什么會發生,也不知道如何修復它。
原來如果我改變
private static final WRITER = assignWriter();
到
WRITER = null
getProperty()
方法完美運行。 在嘗試存儲屬性文件時,我顯然會收到NullPointerException
WRITER = null
,因為WRITER = null
,但是,這是一個簡單的解決方法。 我不明白的是,為什么分配WRITER
會導致getProperty()
方法只返回 null?
您可以輕松設置和獲取用於限制訪問的鍵值對。 當您將代碼推送到像 github 這樣的遠程 git 上時,它是安全的。
請參閱下面的完整代碼:
Config.java 類
導入 java.io.*; 導入 java.util.Properties;
公共類配置{
Properties p; final String filename = "config.properties"; public Config() throws FileNotFoundException { } //Storing environmental values public void setConfigValues() throws IOException { OutputStream os = new FileOutputStream(filename); p = new Properties(); p.setProperty("name","Anand"); p.setProperty("age","27"); p.store(os, null); } //retrieving environmental values public String getConfigValues(String key) throws IOException { InputStream is = new FileInputStream(filename); p = new Properties(); String name; p.load(is); name = p.getProperty(key); return name; }
}
Main.java 類
導入 java.io.IOException;
public class Main { public static String propertyValue; public static void main(String[] args) throws IOException { Config cfg = new Config(); cfg.setConfigValues(); propertyValue = cfg.getConfigValues("name"); System.out.println(propertyValue); System.out.println(cfg.getConfigValues("age")); } }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.