1. properties

  1. # 键值对写法,要求键不相同,否者后者会覆盖前者
  2. account=root
  3. username=root
  4. password=123
  5. # 层级键值对写法
  6. mysql.account=root
  7. mysql.username=root
  8. mysql.password=123
  1. import java.io.*;
  2. import java.util.Enumeration;
  3. import java.util.Properties;
  4. // 内置函数无需调用
  5. // Properties说明
  6. public Properties(); // 创建空属性列表
  7. void load(InputStream inStream); // 从字符输入流中读取属性列表
  8. String getProperty(String key); // 获取指定键值
  9. void store(OutputStream out, String comments);// 写入properties
  10. void clear(); // 清除所以键值对
  11. // Properties单信息读取
  12. public static String readValue(String filePath, String key){
  13. Properties props = new Properties();
  14. try{
  15. InputStream in = new BufferedInputStream (new FileInputStream(filePath));
  16. props.load(in);
  17. return props.getProperty(key);
  18. }catch(Exception e){
  19. e.printStackTrace();
  20. return null;
  21. }
  22. }
  23. // properties多信息查看
  24. public static void printAllPropperties(String filePath){
  25. Properties props = new Properties();
  26. try{
  27. InputStream in = new BufferedInputStream (new FileInputStream(filePath));
  28. props.load(in);
  29. Enumeration en = props.propertyNames();
  30. while (en.hasMoreElements()) {
  31. String key = (String)en.nextElement();
  32. System.out.println(key + " : " + props.getProperty(key));
  33. }
  34. }catch(Exception e){e.printStackTrace();}
  35. }
  36. // properties写入
  37. public static void writeProperties(String filePath, String key, String value){
  38. Properties prop = new Properties();
  39. try{
  40. InputStream fis = new FileInputStream(filePath);
  41. prop.load(fis);
  42. OutputStream fos = new FileOutputStream(filePath);
  43. prop.setProperty(key, value);
  44. prop.store(fos, "Update '" + key + "' value");
  45. }catch(IOException e){
  46. System.err.println("Visit "+filePath+" for updating "+key+" value error");
  47. }
  48. }

2. ini

  1. [mysql]
  2. account=root
  3. username=root
  4. password=123
  5. [redis]
  6. account=user
  7. username=user
  8. password=456
  1. IniFileHelper inifile = new IniFileHelper( GlobalSetting.AppPath + "\\file.ini");
  2. MessageBox.Show(GlobalSetting.AppPath);
  3. String mysqlAccount = inifile.IniReadValue("mysql", "account");
  4. String mysqlUsername = inifile.IniReadValue("mysql", "username");
  5. String mysqlPpassword = inifile.IniReadValue("mysql", "password");

3. yaml / yml

  1. mq:
  2. data:
  3. xx:
  4. - v1
  5. - v2
  6. mysql:
  7. data:
  8. account: root
  9. username: root
  10. password: 123

4. xml

lib.keyxml所需jar,将后缀改成7z

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <mysql>
  3. <account id="xx">root</account>
  4. <username>root</username>
  5. <password>123</password>
  6. </mysql>
  1. //1·加载文件
  2. InputStream stream = test.class.getClassLoader()
  3. .getResourceAsStream("info.xml");
  4. //2.创建dom4j的解析器
  5. SAXReader saxReader = new SAXReader();
  6. //3.处理xml文件流 xml -> java.document
  7. Document document = saxReader.read(stream);
  8. //4.xpath语法,寻找所需标签
  9. // 元素标签 / -> 从匹配根标签
  10. Element username = (Element)document.selectSingleNode("/mysql/username");
  11. System.out.println(username.getText());
  12. // 元素标签集, // -> 匹配所有标签
  13. List<Element> password = document.selectNodes("//password");
  14. System.out.println(password.get(0).getText());
  15. // 属性找标签
  16. Attribute id = element.attribute("id"); // 获取属性
  17. id.getName;id.getValue(); // 获取属性名;获取属性值