Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

ReadProperties.java 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import java.io.*;
  2. import java.util.*;
  3. public class ReadProperties {
  4. public static void main(String[] args) throws IOException {
  5. if(args.length <= 0) { System.out.println("No file provided."); return; }
  6. File f = new File(args[0]);
  7. if(!f.exists()) { System.out.println("File not found: " + args[0]); return; }
  8. Properties prop = new Properties();
  9. prop.load(new FileInputStream(f));
  10. boolean isFirst = true; // I fucking hate java, why don't they have a native string join function?
  11. System.out.print("{");
  12. for (Map.Entry<Object, Object> item : prop.entrySet()) {
  13. String key = (String) item.getKey();
  14. String value = (String) item.getValue();
  15. if(isFirst) { isFirst = false; }
  16. else { System.out.print(","); }
  17. System.out.print("\"" + escape(key) + "\":\"" + escape(value) + "\"");
  18. }
  19. System.out.print("}");
  20. }
  21. static String escape(String s) { // Taken from http://code.google.com/p/json-simple/
  22. StringBuffer sb = new StringBuffer();
  23. for(int i = 0; i < s.length(); i++) {
  24. char ch = s.charAt(i);
  25. switch(ch) {
  26. case '"': sb.append("\\\""); break;
  27. case '\\': sb.append("\\\\"); break;
  28. case '\b': sb.append("\\b"); break;
  29. case '\f': sb.append("\\f"); break;
  30. case '\n': sb.append("\\n"); break;
  31. case '\r': sb.append("\\r"); break;
  32. case '\t': sb.append("\\t"); break;
  33. case '/': sb.append("\\/"); break;
  34. default:
  35. //Reference: http://www.unicode.org/versions/Unicode5.1.0/
  36. if (('\u0000' <= ch && ch <= '\u001F')
  37. || ('\u007F' <= ch && ch <= '\u009F')
  38. || ('\u2000' <= ch && ch <= '\u20FF')) {
  39. String ss = Integer.toHexString(ch);
  40. sb.append("\\u");
  41. for(int k = ss.length(); k < 4; k++) {
  42. sb.append('0');
  43. }
  44. sb.append(ss.toUpperCase());
  45. } else {
  46. sb.append(ch);
  47. }
  48. }
  49. }
  50. return sb.toString();
  51. }
  52. }