Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-6 of 6 total  RSS 

Printing out a Map in sorted order

// print out a Map in sorted order of its keys

   1  
   2      public static String dumpToString(Map<String, Object> map) {
   3          if (map == null) {
   4              return Constants.EMPTY_STRING;
   5          }
   6  
   7          StringBuffer strbuf = new StringBuffer();
   8  
   9          SortedMap<String, Object> sMap = new TreeMap<String, Object>(map);
  10          Set<Map.Entry<String, Object>> s = sMap.entrySet();
  11  
  12          for (Map.Entry<String, Object> elem : s) {
  13              String key = elem.getKey();
  14              Object value = elem.getValue();
  15              strbuf.append(key);
  16              strbuf.append("=");
  17              strbuf.append(value == null ? Constants.EMPTY_STRING : value);
  18              strbuf.append("\r\n");
  19          }
  20  
  21          return strbuf.toString();
  22      }

Recursively dump imbricated Map of Maps

// description of your code here

   1  
   2      public void dumpMapOfMap(Map map) {
   3          Set s = map.entrySet();
   4          Iterator sit = s.iterator();
   5          boolean isFirst = true;
   6  
   7          while (sit.hasNext()) {
   8              Map.Entry elem = (Map.Entry)sit.next();
   9              String key = (String)elem.getKey();
  10              Object value = elem.getValue();
  11  
  12              if (value instanceof String) {
  13                  // recursivity stop condition
  14                  System.out.print(key);
  15                  System.out.print(" : ");
  16                  System.out.println(value);
  17              } else {
  18                  if (!isFirst) {
  19                      System.out.println("");
  20                  } else {
  21                      isFirst = false;
  22                  }
  23                  System.out.println(key);
  24                  Map valueMap = (Map)elem.getValue();
  25                  dumpMapOfMap(valueMap);
  26              }
  27          }
  28      }

Obtaining the value of a public static field by reflexion

// Obtaining the value of a static public constant of a class, here Boolean for example
   1  
   2          Object returnedObj = null;
   3          try {
   4              Class clazz = Class.forName("java.lang.Boolean");
   5              returnedObj = clazz.getField("TRUE").get(clazz);
   6              assert java.lang.Boolean.TRUE == returnedObj : "wrong returned object";
   7          } catch (ClassNotFoundException e) {
   8              e.printStackTrace();
   9          } catch (SecurityException e) {
  10              e.printStackTrace();
  11          } catch (NoSuchFieldException e) {
  12              e.printStackTrace();
  13          } catch (IllegalArgumentException e) {
  14              e.printStackTrace();
  15          } catch (IllegalAccessException e) {
  16              e.printStackTrace();
  17          }
  18  
  19          return returnedObj;

Printing out the full path of a resource on the classpath

// displaying the full path of a resource found by the class loader on the classpath, here the log4j.xml file
   1  
   2  String resourceName = "/log4j.xml"; // pay attention to the leading '/' !
   3  URL location = AnyClass.class.getResource(resourceName);
   4  
   5  if (location != null) {
   6      System.out.println(location.getPath());
   7  } else {
   8      System.out.println(resourceName + " not found on the classpath");
   9  }

Loading a text file into a String from the classpath

Locate a text file in the classpath and read it into a String

   1  
   2      /** 
   3       * @param filePath      name of file to open. The file can reside
   4       *                      anywhere in the classpath
   5       */
   6      private String readFileAsString(String filePath) throws java.io.IOException {
   7          StringBuffer fileData = new StringBuffer(1000);
   8          BufferedReader reader = new BufferedReader(new InputStreamReader(this
   9              .getClass().getClassLoader().getResourceAsStream(filePath)));
  10          char[] buf = new char[1024];
  11          int numRead = 0;
  12          while ((numRead = reader.read(buf)) != -1) {
  13              String readData = String.valueOf(buf, 0, numRead);
  14              fileData.append(readData);
  15              buf = new char[1024];
  16          }
  17          reader.close();
  18          return fileData.toString();
  19      }

Loading a property file from the classpath

Loads a property file located anywhere in the classpath

   1  
   2      private Properties getPropertiesFromClasspath(String propFileName) throws IOException {
   3          // loading xmlProfileGen.properties from the classpath
   4          Properties props = new Properties();
   5          InputStream inputStream = this.getClass().getClassLoader()
   6              .getResourceAsStream(propFileName);
   7  
   8          if (inputStream == null) {
   9              throw new FileNotFoundException("property file '" + propFileName
  10                  + "' not found in the classpath");
  11          }
  12  
  13          props.load(inputStream);
  14  
  15          return props;
  16      }
« Newer Snippets
Older Snippets »
Showing 1-6 of 6 total  RSS