properties文件简单读取

来源:互联网 发布:数学建模优化方法模型 编辑:程序博客网 时间:2024/05/23 15:39

文件的处理步骤:

1、以流的方式读取文件、2.根据需要采用相应的JDK或框架处理流、3.处理完成后切记关闭输入流

实例:测试类读取源文件下的db.properties文件,并读取文件中的内容

一、工程结构


二、db.properties


三、LoaderFile.java

public class LoaderFile {public static void main(String[] args) throws Exception {//1.以流的方式读取文件InputStream is = null;is = Thread.currentThread().getContextClassLoader().getResourceAsStream("db.properties");// 1-1.判断文件是否存在if(is==null){throw new Exception("文件不存在!");}//2.Properties加载流Properties p = new Properties();p.load(is);// 2.2判断文件中是否存在数据if(p.isEmpty()){throw new Exception("文件中无任何数据!");}//3.读取文件中的内容System.out.println(p.get("dbname")+"\n"+p.get("dbuser")+"\n"+p.get("dbpwd"));//4.关闭文件流is.close();}}

四、控制台结果


0 0