java – Map.Keyset() method does not return all keys

Question:

Friends, I have a problem and I can't identify the cause.

I have a properties file here . I'm reading it normally but when I get all the keys from it by the Map.keySet() method not all the keys are being returned.

input = new FileInputStream("config.properties");

// load a properties file
prop.load(input);
for (Object string : prop.keySet()) {
   System.out.println(string.toString());
}

Output is not matching all keys in the file.

I performed another test by reading every line in the file with FileReader and using a regular expression to separate [ key , value ].

I noticed that the reading was done correctly, and soon after, I assigned each key and value pair to a Map<String, String> and noticed that the following lines are not being assigned in the map keys:

ArquivoSefipVisao.cabecalho.dataRecolhimentoPrevidenciaSocial
ArquivoSefipVisao.$COLECAO.listaCabecalhoEmpresa.informacaoAdicional.tipoInscricaoEmpresa
ArquivoSefipVisao.$COLECAO.listaCabecalhoEmpresa.informacaoAdicional.inscricaoEmpresa
ArquivoSefipVisao.$COLECAO.listaCabecalhoEmpresa.informacaoAdicional.receitaEventoDesportivoPatrocinio
ArquivoSefipVisao.$COLECAO.listaCabecalhoEmpresa.informacaoAdicional.indicativoOrigemReceita
ArquivoSefipVisao.$COLECAO.listaCabecalhoEmpresa.informacaoAdicional.recolhimentoDeCompetenciasAnterioresInss
ArquivoSefipVisao.$COLECAO.listaCabecalhoEmpresa.informacaoAdicional.recolhimentoDeCompetenciasAnterioresOutrasEntidades

Answer:

hello, try it this way:

    Properties prop = new Properties();
    InputStream input = null;

try {

    String filename = "config.properties";
    input = getClass().getClassLoader().getResourceAsStream(filename);
    if (input == null) {
        System.out.println("Sorry, unable to find " + filename);
        return;
    }

    prop.load(input);

    Enumeration<?> e = prop.propertyNames();
    while (e.hasMoreElements()) {
        String key = (String) e.nextElement();
        String value = prop.getProperty(key);
        System.out.println("Key : " + key + ", Value : " + value);
    }

} catch (IOException ex) {
    ex.printStackTrace();
} finally {
    if (input != null) {
        try {
            input.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Scroll to Top