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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章