Search code examples
javaxmljdomjdom-2

read a variable from txt in java


Here is my code:

try{
    if(fichadaHecha==false)
        {
            Element fichada = new Element("fichada");
                  //Nº TERMINAL
                  fichada.addContent(new Element("N_Terminal").setText("XX"));
                  //TARJETA
                  fichada.addContent(new Element("Tarjeta").setText(codOperario));
                  //FECHA
                  Date fechaFormatoFecha = new Date( );
                  fichada.addContent(new Element("Fecha").setText(formatoFecha.format(fechaFormatoFecha)));
                  //HORA
                  Date fechaFormatoHora = new Date( );
                  fichada.addContent(new Element("Hora").setText(formatoHora.format(fechaFormatoHora)));
                  //CAUSA
                  fichada.addContent(new Element("Causa").setText("0"));
                  doc.getRootElement().addContent(fichada);
                  XMLOutputter xmlOutput = new XMLOutputter();
                  xmlOutput.setFormat(Format.getPrettyFormat());
                  xmlOutput.output(doc, new FileWriter("C:\\fichadas.xml"));
                  contador=contador+1;
                  //fichadaHecha=true;

              }
                  } catch(IOException io){
                  }

          }
      }, 0L, 5000);

and here is my conf.txt

N_TERMINAL=18

I want to use the value of N_TERMINAL (18) on conf.txt to use it here

fichada.addContent(new Element("N_Terminal").setText("HERE"));

Does someone know how to do this?


Solution

  • Properties props = new Properties();
    props.load(new FileReader("conf.txt"));
    fichada.addContent(new Element("N_Terminal").setText(props.getProperty("N_TERMINAL")));