java将常量设置在配置文件中,读取配置文件

将常量设置在配置文件中,读取配置文件

package properties;

import org.testng.annotations.Test;
import java.io.FileInputStream;
import java.util.Properties;
public class Constant {
    public static Properties prop = getProperties();
    public static Properties getProperties() {
        Properties prop = new Properties();
        try {
            FileInputStream file = new FileInputStream("src/main/resources/test.properties");
            prop.load(file);
            file.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return prop;
    }

    @Test
    public void test(){
        Constant  ctest=new Constant();
        Properties prop = ctest.prop;
        System.out.println(prop.getProperty("test.name"));
    }
}

 

 

test.properties文件中内容为:

test.name = javascript

你可能感兴趣的:(java)