读取配置文件的方法
第一种
1 try{ 2 //读取配置文件 3 Properties prop = new Properties(); 4 InputStream is = DBUtil.class.getClassLoader().getResourceAsStream("config.properties"); 5 prop.load(is); 6 is.close(); 7 8 //获取驱动 9 String driver = prop.getProperty("jdbc.className"); 10 11 System.out.println(driver); 12 //获取地址 13 url = prop.getProperty("jdbc.url"); 14 15 //获取用户名 16 user = prop.getProperty("jdbc.username"); 17 18 //获取密码 19 pwd = prop.getProperty("jdbc.password"); 20 21 //注册驱动 22 Class.forName(driver); 23 24 }catch(Exception e){ 25 e.printStackTrace(); 26 }
第二种
Properties prop = new Properties(); try { String path = "WEB-INF/classes"; path = request.getSession().getServletContext().getRealPath(path); InputStream is = new FileInputStream(path+"/arcgis.properties"); prop.load(is); is.close(); String mapserver = prop.getProperty("MAPSERVER"); model.addAttribute("mapserver", mapserver); return "view/gp/bian_all"; } catch (Exception e) { e.printStackTrace(); return null; }