【问题标题】:Java: Properties return always nullJava:属性总是返回 null
【发布时间】:2017-12-29 06:26:30
【问题描述】:

我有问题。 prop.getProperty(key) 始终返回 null。 如果我更改属性文件中的某些内容并调用该方法(启动 java 程序),它会重置这些值。 我的代码:

    public static boolean checkProperties(){

    Properties prop = new Properties();
    try {

        Writer writer = new FileWriter(propertyFile);
        Reader reader = new FileReader(propertyFile);
        prop.load(reader);


        System.out.println(prop.getProperty("server-port")); // OUTPUT: null; But it is in the file!


        if(prop.getProperty("server-port") == null || prop.getProperty("server-port").equals("")){
            prop.setProperty("server-port", "2424");
            System.out.println("No port was specified in the properties file! Set port to 2424.");
        }
        if(prop.getProperty("info-text") == null || prop.getProperty("info-text").equals("")){
            prop.setProperty("info-text", "Welcome to my Game Server!");
            System.out.println("No info text was specified in the properties file! Set to 'Welcome to my Game Server!'");
        }
        prop.store(writer, "Game Server Config");
        writer.close();
        reader.close();

    } catch (IOException e) {
        System.err.println("Something went wrong while checking properties file!");
        System.err.println(e.getMessage());
        e.printStackTrace();
        return false;
    }

    return true;
}

谢谢, Max2002_。

【问题讨论】:

  • 什么是propertyFile?它指向什么?如果没有这些详细信息,我们无法重现该问题。
  • File propertyFile = new File("server.properties");
  • "new FileWriter" 总是创建一个新文件,所以你总是用一个新的空文件覆盖任何现有文件
  • @gusto2 谢谢你,但我怎样才能改变它,让它工作?
  • 您能告诉我们,您是在哪里创建的属性文件吗?路径是否正确?

标签: java properties null return


【解决方案1】:

试试这个

public static boolean checkProperties() {
        Properties prop = new Properties();
        InputStream input = null;
        boolean flag = false;
        try {
            String filename = "server.properties";
            input = Test.class.getClassLoader().getResourceAsStream(filename);
            prop.load(input);
            System.out.println(prop.getProperty("server-port"));
            if(prop.getProperty("server-port") == null){
                flag=false;
            }
            else{
                flag=true;
            }

        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return flag;
    }

如果您遇到任何问题,请告诉我。

【讨论】:

    【解决方案2】:

    现在可以了。代码:

        public static boolean checkProperties(){
    
        Properties prop = new Properties();
        try {
    
            //Writer writer = new FileWriter(propertyFile);
            FileOutputStream out = null;
            Reader reader = new FileReader(propertyFile);
            prop.load(reader);
            printToConsole(prop.getProperty("server-port"), LEVEL_NORMAL, true);
            if(prop.getProperty("server-port") == null || prop.getProperty("server-port").equals("")){
                prop.setProperty("server-port", "2424");
                printToConsole("No port was specified in the properties file! Set port to 2424.", LEVEL_NORMAL, true);
            }
            if(prop.getProperty("info-text") == null || prop.getProperty("info-text").equals("")){
                prop.setProperty("info-text", "Welcome to my Game Server!");
                printToConsole("No info text was specified in the properties file! Set to 'Welcome to my Game Server!'", LEVEL_NORMAL, true);
            }
            out = new FileOutputStream(propertyFile);
            prop.store(out, "Game Server Config");
            out.close();
            reader.close();
    
        } catch (IOException e) {
            printToConsole("Something went wrong while checking properties file!", LEVEL_ERROR, true);
            printToConsole(e.getMessage(), LEVEL_ERROR, true);
            return false;
        }
    
        try{
            port = Integer.parseInt(prop.getProperty("server-port"));
        }catch(Exception e){
            printToConsole("Error: Can not use the port in the properties file ('" + prop.getProperty("server-port") + "')!", LEVEL_ERROR, true);
            printToConsole(e.getMessage(), LEVEL_ERROR, true);
            return false;
        }
    
    
        return true;
    }
    

    【讨论】:

      猜你喜欢
      • 2013-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-09
      • 1970-01-01
      相关资源
      最近更新 更多