java读取.properties文件

来源:互联网 发布:js children用法 编辑:程序博客网 时间:2024/06/06 13:15
public class PropertiesTest {    public static void main(String[] args) {        Properties properties = new Properties();        try {            //查看当前类的Path            File classPath = new File("");            String classAbsolutePath = classPath.getAbsolutePath();            String classCnoPathString = classPath.getCanonicalPath();            System.out.println("classAbsolutePath="+classAbsolutePath);            System.out.println("classCnoPathString="+classCnoPathString);            //读取src目录下的properties文件            InputStream inputStreamPro =                   PropertiesTest.class.getClassLoader().getResourceAsStream("test.properties");              //读取test.properties配置文件            InputStream inputStream = new BufferedInputStream(inputStreamPro);               properties.load(inputStream);            Iterator<String> iterator = properties.stringPropertyNames().iterator();            while(iterator.hasNext())            {                String key = iterator.next();                System.out.println(key+" : "+properties.getProperty(key));            }                inputStream.close();        } catch (FileNotFoundException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }}
原创粉丝点击