java项目文件读取

 

 Properties文件读取

properties文件的信息读取:  读取配置文件的信息

 

之前

Propertry prop = New Propertry();

Prop.load(new FileReader());

 

// 属性    其值在静态代码块获取,这个类在项目启动被加载的时候,读取一次配置文件;

private static Properties prop = null;

 

// 静态代码块

static {

prop = new Properties();

 

// 加载

try {

prop.load(new FileReader("bean.properties"));

} catch (IOException e) {

throw new RuntimeException("配置文件加载失败!");

}

}

 

还可以

ResourceBundle  rb =  ResourceBundle.getBundle(“配置文件”)   //不带后缀名

String className = rb.getString(key);

 

Src类路径下的配置文件读取

当文件在src目录下,而不是与src平齐的项目目录下;  获取src下面文件的路径

BufferedInputStream bis = new BufferedInputStream(new FileInputStream(this.getClass().getResource("/abc.xml").getPath()));

 

//读取mybatis-config.xml

InputStream in = Resources.getResourceAsStream("mybatis-config.xml");   //读取src目录下的文件,获取流

 

 

Dom操作-saxreader

形成文档树dom

SAXReader saxReader = new SAXReader();

Document doc = saxReader.read("efg.xml");   //参数可以是文件,可以直接是文件,可以是流;这个文件是项目路径下面的,与src平级??

Document document = saxReader.read(BeanFactory.class.getClassLoader().getResourceAsStream("beans.xml"));   

 

posted @ 2021-03-10 17:00  加瓦加瓦  阅读(481)  评论(0编辑  收藏  举报