This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import java.io.InputStream; | |
import java.net.URL; | |
import java.util.Properties; | |
import com.google.common.io.ByteSource; | |
import com.google.common.io.Resources; | |
/** | |
* Reads a properties file and print all the key value pairs to the console. | |
* Note: Writing to console is just for convenience here. | |
*/ | |
public final class ReadPropertiesWithGuava { | |
public final static void main(final String[] args) { | |
final URL url = Resources.getResource("spring-config.properties"); | |
final ByteSource byteSource = Resources.asByteSource(url); | |
final Properties properties = new Properties(); | |
InputStream inputStream = null; | |
try { | |
inputStream = byteSource.openBufferedStream(); | |
properties.load(inputStream); | |
properties.list(System.out); | |
} catch (final IOException ioException) { | |
ioException.printStackTrace(); | |
} finally { | |
if (inputStream != null) { | |
try { | |
inputStream.close(); | |
} catch (final IOException ioException) { | |
ioException.printStackTrace(); | |
} | |
} | |
} | |
} | |
} |
For more info, please check Resources class of Guava.
Update on 05th April, 2015: After a fair bit of time here, I have moved on to GitHub hosted Octopress blogs. Please find me on http://P7h.org henceforth for all new updates.
No comments:
Post a Comment