1

我正在使用 Eclipse,并且在 mi src 文件夹之外创建了一个 res 文件夹。在其中,我创建了一个名为“config.cfg”的文本文件。看起来像这样:

# System configuration
# Comments will automatically be excluded by the program

radiomodemPort=20001

sisnetPort=5562

sisnetHost=213.229.135.3

sisnetUser=jogg

sisnetPass=jogg

为读取它而编写的代码不起作用:它不会加载任何存储的变量。我的代码是:

private String sisnetHost;
private int sisnetPort;
private int radiomodemPort;
private String sisnetUser;
private String sisnetPass;

private boolean sisnetHostLoaded;
private boolean sisnetPortLoaded;
private boolean radiomodemPortLoaded;
private boolean sisnetUserLoaded;
private boolean sisnetPassLoaded;

public boolean getSettingsFromFile(){
        Properties config = new Properties();
        try {
            config.load(new FileInputStream("res/config.cfg"));
            Enumeration<Object> en = config.keys();
            while (en.hasMoreElements()) {
                String key = (String) en.nextElement();
                if(key.equals(sisnetHost)){
                    sisnetHost = (String)config.get(key);
                    sisnetHostLoaded = true;
                }
                if(key.equals(sisnetPort)){
                    sisnetPort = (Integer)config.get(key);
                    sisnetPortLoaded = true;
                }
                if(key.equals(sisnetUser)){
                    sisnetUser = (String)config.get(key);
                    sisnetUserLoaded = true;
                }
                if(key.equals(sisnetPass)){
                    sisnetPass = (String)config.get(key);
                    sisnetPassLoaded = true;
                }
                if(key.equals(radiomodemPort)){
                    radiomodemPort = (Integer)config.get(key);
                    radiomodemPortLoaded = true;
                }
            }

        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
            return false;
        } catch (IOException ex) {
            ex.printStackTrace();
            return false;
        }

        if(!(sisnetHostLoaded && sisnetPortLoaded && sisnetUserLoaded && sisnetPassLoaded && radiomodemPortLoaded))
            fillUnloadedSettings();
        return true;
    }

怎么了?

4

2 回答 2

4

在您的equals测试中,您将每个键与您的实例变量(似乎具有默认值:null对象、0数字等)进行比较。使用实际的字符串来测试键:

if(key.equals("sisnetHost")) // NOT if(key.equals(sisnetHost))

通常建议调用equals文字/常量以消除 NPE 的风险:

if ("sisnetHost".equals(key))
于 2012-07-31T11:03:07.587 回答
0

这里准备好了静态类

import java.io.*;
import java.util.Properties;
public class Settings {
    public static String Get(String name,String defVal){
        File configFile = new File(Variables.SETTINGS_FILE);
        try {
            FileReader reader = new FileReader(configFile);
            Properties props = new Properties();
            props.load(reader);
            reader.close();
            return props.getProperty(name);
        } catch (FileNotFoundException ex) {
            // file does not exist
            logger.error(ex);
            return defVal;
        } catch (IOException ex) {
            // I/O error
            logger.error(ex);
            return defVal;
        } catch (Exception ex){
            logger.error(ex);
            return defVal;
        }
    }
    public static Integer Get(String name,Integer defVal){
        File configFile = new File(Variables.SETTINGS_FILE);
        try {
            FileReader reader = new FileReader(configFile);
            Properties props = new Properties();
            props.load(reader);
            reader.close();
            return Integer.valueOf(props.getProperty(name));
        } catch (FileNotFoundException ex) {
            // file does not exist
            logger.error(ex);
            return defVal;
        } catch (IOException ex) {
            // I/O error
            logger.error(ex);
            return defVal;
        } catch (Exception ex){
            logger.error(ex);
            return defVal;
        }
    }
    public static Boolean Get(String name,Boolean defVal){
        File configFile = new File(Variables.SETTINGS_FILE);
        try {
            FileReader reader = new FileReader(configFile);
            Properties props = new Properties();
            props.load(reader);
            reader.close();
            return Boolean.valueOf(props.getProperty(name));
        } catch (FileNotFoundException ex) {
            // file does not exist
            logger.error(ex);
            return defVal;
        } catch (IOException ex) {
            // I/O error
            logger.error(ex);
            return defVal;
        } catch (Exception ex){
            logger.error(ex);
            return defVal;
        }
    }
    public static void Set(String name, String value){
        File configFile = new File(Variables.SETTINGS_FILE);
        try {
            Properties props = new Properties();
            FileReader reader = new FileReader(configFile);
            props.load(reader);
            props.setProperty(name, value.toString());
            FileWriter writer = new FileWriter(configFile);
            props.store(writer, Variables.SETTINGS_COMMENT);
            writer.close();
        } catch (FileNotFoundException ex) {
            // file does not exist
            logger.error(ex);
        } catch (IOException ex) {
            // I/O error
            logger.error(ex);
        } catch (Exception ex){
            logger.error(ex);
        }
    }
    public static void Set(String name, Integer value){
        File configFile = new File(Variables.SETTINGS_FILE);
        try {
            Properties props = new Properties();
            FileReader reader = new FileReader(configFile);
            props.load(reader);
            props.setProperty(name, value.toString());
            FileWriter writer = new FileWriter(configFile);
            props.store(writer,Variables.SETTINGS_COMMENT);
            writer.close();
        } catch (FileNotFoundException ex) {
            // file does not exist
            logger.error(ex);
        } catch (IOException ex) {
            // I/O error
            logger.error(ex);
        } catch (Exception ex){
            logger.error(ex);
        }
    }
    public static void Set(String name, Boolean value){
        File configFile = new File(Variables.SETTINGS_FILE);
        try {
            Properties props = new Properties();
            FileReader reader = new FileReader(configFile);
            props.load(reader);
            props.setProperty(name, value.toString());
            FileWriter writer = new FileWriter(configFile);
            props.store(writer,Variables.SETTINGS_COMMENT);
            writer.close();
        } catch (FileNotFoundException ex) {
            // file does not exist
            logger.error(ex);
        } catch (IOException ex) {
            // I/O error
            logger.error(ex);
        } catch (Exception ex){
            logger.error(ex);
        }
    }
}

这里示例:

    Settings.Set("valueName1","value");
    String val1=Settings.Get("valueName1","value");
    Settings.Set("valueName2",true);
    Boolean val2=Settings.Get("valueName2",true);
    Settings.Set("valueName3",100);
    Integer val3=Settings.Get("valueName3",100);
于 2019-03-03T10:56:06.483 回答