Last active
October 3, 2019 18:57
-
-
Save inazense/27fd6c96a7afb186d3aaa722653a7a5c to your computer and use it in GitHub Desktop.
Clase que maneja la lectura de ficheros properties usando un patrón Singleton
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.FileNotFoundException; | |
import java.io.FileReader; | |
import java.io.IOException; | |
import java.util.Enumeration; | |
import java.util.HashMap; | |
import java.util.Map; | |
import java.util.Properties; | |
/** | |
* Clase usada para leer archivos properteis usando un | |
* patrón Singleton | |
* @author Inazio | |
* | |
*/ | |
public class ManejadorProperties { | |
// Propiedades | |
private String rutaFichero = "fichero.properties"; | |
private static Properties propiedades = null; | |
// Constructor | |
private ManejadorProperties() throws FileNotFoundException, IOException { | |
propiedades = new Properties(); | |
propiedades.load(new FileReader(rutaFichero)); | |
} | |
// Métodos | |
/** | |
* Lee una propiedad | |
* @param miClave | |
* @return Valor de la propiedad | |
* @throws FileNotFoundException | |
* @throws IOException | |
*/ | |
public static String leerPropiedad(String miClave) throws FileNotFoundException, IOException { | |
if (propiedades == null) { | |
new ManejadorProperties(); | |
} | |
String resultado = ""; | |
resultado = propiedades.getProperty(miClave); | |
return resultado; | |
} | |
/** | |
* Devuelve todas las propiedades del archivo de configuración | |
* @return Mapa de clave valor con las propiedades correspondientes | |
* @throws FileNotFoundException | |
* @throws IOException | |
*/ | |
public Map<String, String> leerTodasLasPropiedades() throws FileNotFoundException, IOException { | |
if (propiedades == null) { | |
new ManejadorProperties(); | |
} | |
Map<String, String> listadoPropiedades = new HashMap<String, String>(); | |
Enumeration<Object> claves = propiedades.keys(); | |
while (claves.hasMoreElements()) { | |
Object clave = claves.nextElement(); | |
listadoPropiedades.put(clave.toString(), propiedades.get(clave).toString()); | |
} | |
return listadoPropiedades; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment