簡體   English   中英

從Java中的文本文件中獲取設置

[英]Get settings from a text file in Java

我正在使用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;
    }

怎么了?

在你的equals測試中,你將每個鍵與你的實例變量進行比較(它們似乎有默認值:對象為null ,數字為0等)。 使用實際的字符串來測試密鑰:

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

通常建議在文字/常量上調用equals以消除NPE的風險:

if ("sisnetHost".equals(key))

這里准備靜態類

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

暫無
暫無

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

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