code_junkie

How to write java.util.Properties to XML with sorted keys?

Nov 10th, 2011
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. String propFile = "/path/to/file";
  2. Properties props = new Properties();
  3. /*set some properties here*/
  4. try {
  5. FileOutputStream xmlStream = new FileOutputStream(propFile);
  6. /*this comes out unsorted*/
  7. props.storeToXML(xmlStream,"");
  8. } catch (IOException e) {
  9. e.printStackTrace();
  10. }
  11.  
  12. String propFile = "/path/to/file";
  13. Properties props = new Properties();
  14. /*set some properties here*/
  15. Properties tmp = new Properties() {
  16.  
  17. @Override
  18. public Set<Object> keySet()
  19. {
  20. return Collections.unmodifiableSet(new TreeSet<Object>(super.keySet()));
  21. }
  22.  
  23. };
  24. tmp.putAll(props);
  25. try {
  26. FileOutputStream xmlStream = new FileOutputStream(propFile);
  27. /*this comes out SORTED! */
  28. tmp.storeToXML(xmlStream,"");
  29. } catch (IOException e) {
  30. e.printStackTrace();
  31. }
  32.  
  33. public static void main(String[] args){
  34. String propFile = "/tmp/test2.xml";
  35. Properties props = new Properties();
  36. props.setProperty("key", "value");
  37. props.setProperty("key1", "value1");
  38. props.setProperty("key2", "value2");
  39. props.setProperty("key3", "value3");
  40. props.setProperty("key4", "value4");
  41.  
  42. try {
  43. BufferedWriter out = new BufferedWriter(new FileWriter(propFile));
  44. List<String> list = new ArrayList<String>();
  45. for(Object o : props.keySet()){
  46. list.add((String)o);
  47. }
  48. Collections.sort(list);
  49. out.write("<?xml version="1.0" encoding="UTF-8"?>n");
  50. out.write("<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">n");
  51. out.write("<properties>n");
  52. out.write("<comment/>n");
  53. for(String s : list){
  54. out.write("<entry key="" + s + "">" + props.getProperty(s) + "</entry>n");
  55. }
  56. out.write("</properties>n");
  57. out.flush();
  58. out.close();
  59. } catch (IOException e) {
  60. e.printStackTrace();
  61. }
  62. }
  63.  
  64. Properties props = new Properties() {
  65. @Override
  66. public Set<Object> keySet(){
  67. return Collections.unmodifiableSet(new TreeSet<Object>(super.keySet()));
  68. }
  69.  
  70. @Override
  71. public synchronized Enumeration<Object> keys() {
  72. return Collections.enumeration(new TreeSet<Object>(super.keySet()));
  73. }
  74. };
  75. props.put("B", "Should come second");
  76. props.put("A", "Should come first");
  77. props.storeToXML(new FileOutputStream(new File("sortedProps.xml")), null);
  78. props.store(new FileOutputStream(new File("sortedProps.properties")), null);
  79.  
  80. new Properties() {
  81. @Override Set<Object> keySet() {
  82. return new TreeSet<Object>(super.keySet());
  83. }
  84. }
  85.  
  86. Set keys = props.keySet();
  87. Iterator i = keys.iterator();
  88.  
  89. Set keys = props.keySet();
  90. List<String> newKeys = new ArrayList<String>();
  91. for(Object key : keys)
  92. {
  93. newKeys.add(key.toString());
  94. }
  95. Collections.sort(newKeys);
  96. Iterator i = newKeys.iterator();
Add Comment
Please, Sign In to add comment