How to read an external Propertie?

0

I have a JAR program that I run on a server daily, so that it takes certain values, from the url of a database, folder addresses, etc. I have a file of type properties within the project, the problem that I present, is that I have to constantly change and add more variables, and I find it tedious to compile and modify the code from the IDE, and upload it to replace the previous one. What I need is to read it externally, and thus only change the file from the outside without needing to have it compiled inside the project.

    
asked by Kevin M. 11.11.2016 в 18:09
source

4 answers

1

If you currently read your file properties taking the route to it with getResourceAsStream , you would have to use one of the methods that you already put in the other answers, where you read an external file. The problem is to have the full path to that file.

You can even combine the contents of the internal file with that of an external file:

Properties p = new Properties();
try (InputStream ins = getClass().getResourceAsStream("/dentro/del/zip.properties")) {
    p.load(ins);
}
File f = new File("ruta/al/externo.properties);
if (f.exists() && f.canRead()) {
    try(FileInputStream ins = new FileInputStream(f)) {
        p.load(ins);
    }
}

That way you load the internal file with defaults and only if the external file exists, you also load it, loading new values and overwriting what comes from the file inside the JAR.

    
answered by 11.11.2016 / 18:46
source
0

Hello, what you can do is have a Util class in which you read the properties file, there you indicate the route, after making it load, only with getProperty you must obtain the properties you want to use. I'll show you an example and I hope it's what you're looking for.

//Bloque en tu clase java para leer un arcvhivo externo
static {
    FileInputStream fis = null;
    try {
        prop = new Properties();
        //indica la ruta donde se encuentra  el archivo properties
        fis = new FileInputStream("/ruta/ruta/ruta/miArvhivoProp.properties");
        //leer archivo properties
        prop.load(fis);
    } catch (Exception e) {
        LOGGER.error("Fallo procesando archivo miArvhivoProp.properties.", e);
    } finally {
        try {
            fis.close();
        } catch (Exception e) {
            LOGGER.error("Fallo cerrando archivo miArvhivoProp.properties.", e);
        }
    }
}
//metodo para retornar una propiedad que se encuentre en el archivo properties
public static String parametro(String propiedad) {
    return (String)prop.getProperty(propiedad);
}
    
answered by 11.11.2016 в 18:20
0

You can use Properties :

Properties pop = new Properties();
pop.load(new FileInputStream("mis_properties.properties"));
pop.put("NUEVA_PROPIEDAD", "NUEVO_VALOR");
FileOutputStream output = new FileOutputStream("mis_properties.properties");
pop.store(output, "Editando el archivo properties");
    
answered by 11.11.2016 в 18:34
0

So far, this is the best way I have found, it is necessary to create a method for each variable:

public String getDestiny() throws IOException
    {
        String valor = null;
        Properties archivoPropertie = new Properties();
        FileInputStream archivo;
        String ruta = "./serverProps.properties"; //Se debe guardar en la misma ruta del JAR
        archivo = new FileInputStream(ruta);
        archivoPropertie.load(archivo);
        archivo.close();
        valor = archivoPropertie.getProperty("destiny");
        return valor;
    }

To send it to call it would only be done in the following way:

    try
    {
         String dstvalor = getDestiny();
    }
    catch(IOException e)
    {
         System.out.println(e.getMessage());
    }
    
answered by 11.11.2016 в 18:58