Created
March 28, 2012 22:53
-
-
Save Miuler/2231304 to your computer and use it in GitHub Desktop.
Clase de java para poder manejar los properties con UTF-8
This file contains hidden or 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
| package pe.gob.mef.banco_proyectos.commons.util; | |
| import java.io.IOException; | |
| import java.io.InputStream; | |
| import java.io.InputStreamReader; | |
| import java.net.URL; | |
| import java.net.URLConnection; | |
| import java.util.Locale; | |
| import java.util.PropertyResourceBundle; | |
| import java.util.ResourceBundle; | |
| import java.util.ResourceBundle.Control; | |
| public class UTF8Control extends Control { | |
| public ResourceBundle newBundle(String baseName, Locale locale, | |
| String format, ClassLoader loader, boolean reload) | |
| throws IllegalAccessException, InstantiationException, IOException { | |
| // The below is a copy of the default implementation. | |
| String bundleName = toBundleName(baseName, locale); | |
| String resourceName = toResourceName(bundleName, "properties"); | |
| ResourceBundle bundle = null; | |
| InputStream stream = null; | |
| if (reload) { | |
| URL url = loader.getResource(resourceName); | |
| if (url != null) { | |
| URLConnection connection = url.openConnection(); | |
| if (connection != null) { | |
| connection.setUseCaches(false); | |
| stream = connection.getInputStream(); | |
| } | |
| } | |
| } else { | |
| stream = loader.getResourceAsStream(resourceName); | |
| } | |
| if (stream != null) { | |
| try { | |
| // Only this line is changed to make it to read properties files | |
| // as UTF-8. | |
| bundle = new PropertyResourceBundle(new InputStreamReader( | |
| stream, "UTF-8")); | |
| } finally { | |
| stream.close(); | |
| } | |
| } | |
| return bundle; | |
| } | |
| } | |
| /* | |
| // Así se debe de usar esta clase, anterior para usarlo en el properties. | |
| public static ResourceBundle resource; | |
| static { | |
| resource = ResourceBundle.getBundle(Constantes.ARCHIVO_PROPIEDADES, new UTF8Control()); | |
| } | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment