0

In the following code the getProperty() method always returns null, even if I add a default for

loadNumberOfAccounts();

so its not null, the next method which uses getProperty() will return null. storing the properties works flawlessly.

prop.get

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

i'm sure the answer is quite simple and I have just missed it, but I have exhausted all options.

Accounts.txt looks like this

NumberOfAccounts=1
OtherStoredProperties=etc
etc...

UPDATE

I have found the bug but do not know why it's happening, nor how to fix it.

Turns out that if I change

private static final WRITER = assignWriter();

to

WRITER = null 

the getProperty() method works flawlessly. I obviously get a NullPointerException when attempting to store the property file because WRITER = null, however, that's an easy fix. What I don't understand is, why does assigning the WRITER cause the getProperty() method to only return null?

4

1 回答 1

0

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

请参阅下面的完整代码:

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

    }

  2. 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"));
        }
    
    }
    
于 2020-02-07T07:21:27.983 回答