OOP(Object Oriented Progamming )面向对象的程序设计
2014-02-10 22:34
489 查看
太无聊了,写个读properties文件的类方便以后直接copy
private String fileName; private Properties properties; private FileInputStream inputFile; private String filePath; // Constructor public ProUtil() { this.properties = new Properties(); } // get path of property file public String getConfigPath() { String strClassName = getClass().getName(); String strPackageName = ""; if (getClass().getPackage() != null) { strPackageName = getClass().getPackage().getName(); } String strClassFileName = ""; if (!"".equals(strPackageName)) { strClassFileName = strClassName.substring( strPackageName.length() + 1, strClassName.length()); } else { strClassFileName = strClassName; } URL url = null; url = getClass().getResource(strClassFileName + ".class"); String strURL = url.toString(); try { strURL = java.net.URLDecoder.decode(strURL, "UTF-8"); System.out.println("strURL : " + strURL); } catch (Exception ex) { ex.printStackTrace(); } strURL = strURL.substring(strURL.indexOf('/') + 1, strURL.lastIndexOf('/') + 1); return strURL; } // file configuration private void initFileConfig() { try { filePath = this.getConfigPath(); filePath += fileName; System.out.println("filePath is : " + filePath); inputFile = new FileInputStream(filePath); properties.load(inputFile); inputFile.close(); } catch (FileNotFoundException e) { System.out.println("配置文件路径错误"); } catch (IOException e) { System.out.println("配置文件度取失败"); } catch (Exception ex) { ex.printStackTrace(); } } // read Value by key from input parameter "fileName" public String readValueByKey(String fileName, String key) { try { String value = ""; this.fileName = fileName; initFileConfig(); if (properties.containsKey(key)) { value = properties.getProperty(key); return value; } else return value; } catch (Exception ex) { ex.printStackTrace(); return ""; } } // read all info from property file public String readAllValues(String fileName) { try { @SuppressWarnings("rawtypes") Enumeration en = properties.propertyNames(); while (en.hasMoreElements()) { String key = (String) en.nextElement(); String value = properties.getProperty(key); System.out.println("Key is : " + key + " Value is : " + value); } } catch (Exception ex) { ex.printStackTrace(); return ""; } return null; } // read Value by key // public String getValue(String key) { // if (propertie.containsKey(key)) { // String value = propertie.getProperty(key); // return value; // } else { // return ""; // } // } //test public static void main(String[] args) { ProUtil property = new ProUtil(); System.out.println(property.readValueByKey("xxx.properties", "a")); System.out.println(property.readAllValues("xxx.properties")); }
相关文章推荐
- OOP(Object Oriented Programming)面向对象程序设计
- OOP(Object Oriented Programming 面向对象程序设计)
- 面向对象程序设计(OOP-Object Oriented Programming)
- OOP:Object Oriented Programming
- Java(面向对象Object-Oriented)
- 面向对象的程序设计(Object Oriented Programming)之三-----多态
- Object Oriented Programming in JavaScript 在Javascript中使用面向对象的编程
- JS Pro-深入面向对象的程序设计之继承的详解
- 面向对象(Object-Oriented)的系统(II)
- 【Java】Java学习笔记之一 ----面向对象(Object-Oriented)
- Object Oriented Programming(OOP)
- 面向对象(oop)的程序设计编写高质量代码
- 面向对象编程(OOP)Object Oriented Programming
- javascript中的面向对象(object-oriented)编程
- Javascript高级程序设计第二版第六章--面向对象程序设计(Object-Oriented Programming)简称OOP编程--笔记
- javascript面向对象的程序设计之Object.getOwnPropertyDescriptor()
- JavaScript 面向对象(object-oriented)方法
- Javascript高级程序设计第二版第六章--面向对象程序设计(Object-Oriented Programming)简称OOP编程--笔记
- Object-Oriented Game Design (面向对象的游戏设计)
- The S.O.L.I.D. Object Oriented Programming(OOP) Principles