getProperty()总是返回null - Java

时间:2017-12-09 06:25:53

标签: java properties getproperty getproperties

在下面的代码中,getProperty()方法总是返回null,即使我为

添加了一个默认值
prop.get

所以它不为null,下一个使用getProperty()的方法将返回null。 存储属性完美无瑕。

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 ...

也会返回null。

NumberOfAccounts=1
OtherStoredProperties=etc
etc...

我确定答案很简单,我错过了,但我已经筋疲力尽了。

Accounts.txt看起来像这样

private static final WRITER = assignWriter();

更新

我发现了这个错误,但不知道它为什么会发生,也不知道如何修复它。

事实证明,如果我改变

WRITER = null 

getProperty()

NullPointerException方法完美无瑕。我在尝试存储属性文件时显然得到WRITER = null,因为WRITER,这是一个简单的修复。我不明白的是,为什么分配getProperty()导致{{1}}方法只返回n​​ull?

1 个答案:

答案 0 :(得分:0)

您可以轻松地设置和获取用于限制访问的键值对。而且,当您将代码推送到github之类的远程git上时,它是安全的。

请参阅下面的完整代码:

  1. Config.java类

    导入java.io. *; 导入java.util.Properties;

    公共类Config {

    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;
    }
    

    }

  2. Main.java类

    import 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"));
        }
    
    }