Showing posts with label tips. Show all posts
Showing posts with label tips. Show all posts

Wednesday, May 20, 2015

Various Java trips and tricks

This blog will list some nice tips and tricks that is good to know.

Classpath resource to java.net.URL

To get a java.net.URL from a classpath resource do:
 URL url = getClass().getClassLoader().getResource("some.file");  

Classpath resource to java.io.InputStream

 To get a java.io.InputStream from a classpath resource do:
 InputStream resourceAsStream = getClass().getClassLoader().getResourceAsStream();  

Classpath resource to java.io.File

 To get a java.io.File from a classpath resource do:
 File file = new File(this.getClass().getResource("some.file").toURI());  

Iterate through a Map

 for (Map.Entry<String, String> entry : map.entrySet()) {  
   System.out.println(entry.getKey() + "/" + entry.getValue());  
 }  

Find current root directory

 File rootDirectory = new File(System.getProperty("user.dir"));