replaceAll txt JAVA

0

I try to replace substrings of a txt file by changing the same word but capitalized. The program opens and displays the txt, the question that subword wants to search in the file, stores it in the variable patron . With the help of the classes pattern and matcher I search the subword within the txt and it shows how many matches there are. Now what I'm looking for is to print the text but with the matches that I found to change them to uppercase. I hope you can help me out.

try {

            BufferedReader bufferedReader = new BufferedReader(new FileReader("src\cadena.txt")); 

            String texto = "";
            System.out.println("Que palabra desea buscar?");
            String patron = leer.next();

            while((texto = bufferedReader.readLine())!=null){
                Pattern regex = Pattern.compile(patron);
                Matcher match = regex.matcher(texto);
                int count=0;
               while(match.find()){
                  count ++;
                  System.out.println("Encontrado "+match.group() );
               }
               System.out.println("se encontraron "+count+" incidencias");
               System.out.println(texto);
/*  Aca es donde necesito ayuda, stexto quiero asignarle el texto del archivo, y textoNuevo es donde imprimo el texto con las coincidencias buscadas en mayusculas */ 

               String textoNuevo=null;
               while((texto = bufferedReader.readLine())!=null) {
                   String stexto = texto;
                    textoNuevo = stexto.replace(patron, patron.toUpperCase());

               }System.out.println(textoNuevo);
            }
         } catch (FileNotFoundException e) {e.printStackTrace();
    }
}
} 
    
asked by JUAN RAMIREZ 22.08.2017 в 02:25
source

1 answer

0

The method I propose is the following:

private String replaceAll(String original, CharSequence target, CharSequence replacement) {
    String aux = original;
    while (aux.contains(target)) {
        aux = aux.replace(target, replacement);
    }
    return aux;
}
    
answered by 23.08.2017 в 20:02