简体   繁体   English

config.properties 文件如果不存在,如何在控制台中传递消息并继续下一个设置而不抛出异常

[英]config.properties file if not exist, how to pass a message in console and continue to next setup without throwing an exception

public static String readProperty(String property) {
    Properties prop;
    String value = null;
    try {
        prop = new Properties();
        prop.load(new FileInputStream(new File("config.properties")));

        value = prop.getProperty(property);

        if (value == null || value.isEmpty()) {
            throw new Exception("Value not set or empty");
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return value;
}

I need to check if config.properties exist before loading - if not then just send soft warning message in console system.out.println("config.properties not found")我需要在加载之前检查 config.properties 是否存在 - 如果不存在,那么只需在控制台中发送软警告消息 system.out.println("config.properties not found")

FileInputStream throws FileNotFoundException FileInputStream抛出FileNotFoundException

if the file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading.如果文件不存在,是目录而不是常规文件,或者由于某些其他原因无法打开读取。

you can catch FileNotFoundException and print your soft warning.您可以捕获FileNotFoundException并打印您的软警告。 for example:例如:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Properties;

public class PropTest
{
        public static String readProperty(String property)
        {
                try (FileInputStream f = new FileInputStream(new File("config.properties"))) {
                        Properties prop = new Properties();
                        prop.load(f);
                        return prop.getProperty(property);

                } catch (FileNotFoundException e) {
                        System.out.println("config.properties not found");
                } catch (Exception e) {
                        e.printStackTrace();
                }

                return null;
        }

        public static void main(String[] args)
        {
                String value = readProperty("foo.bar");
                System.out.println(value);

                value = readProperty("foo.baz");
                System.out.println(value);
        }
}

the method readProperty() now returns null if the property is not set or an error occurs while reading the properties file and it also prints a message if the file doesn't exists:如果未设置属性或在读取属性文件时发生错误,则方法readProperty()现在返回null ,如果文件不存在,它还会打印一条消息:

$ javac PropTest.java
$ java PropTest
config.properties not found
config.properties not found
$ echo "foo.bar=Hello world" > config.properties
$ java PropTest
Hello world
null
$

the line:该行:

try (FileInputStream f = new FileInputStream(new File("config.properties"))) {

uses what is called a try-with-resources which automatically closes the file when the code has done with it.使用所谓的try-with-resources在代码完成后自动关闭文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM