Check if there is a word in a txt before reading it

0

I was trying to do this exercise

  

11.Implemente a method that given an input file (file that will be read) and one of   output (file that will be written), specified with your route, and two words:   word Replace and new Word, return the contents of the file in the output file   input file, but with the first word replaced by the second.   public void replace (String fileOrganic, String fileAdd, String   word Replace, New String Word).   Example:   Let's suppose the content of the source file:   "A kit can also be part of another kit, so a kit is a group of elements   of any of the types of elements (simple or kit). Each type of element has a   code, name and a price. "   The word to replace "kit" and the new word "set", the method will write to the file   Destination (TargetFile) content:   "A set can also be part of another set, so a set is a group of elements   of any of the types of elements (simple or set). Each type of element has a   code, name and a price. "   Solve this exercise using a StringTokenizer object for its implementation.

I managed to implement the part of replacing one word with another, but now what I want to do is first verify if the word to be replaced is in the file that will be read, but I can not do it, this is my code

package Ejercicio11;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class RemplazarPorPalabraV2 {

FileReader frEntrada;
FileWriter fwSalida;
BufferedReader brEntrada;
String linea;

public void remplazar(String archivoOrigen, String archivoDestino, String
        palabraAReemplazar, String nuevaPalabra) throws Exception {

    try {

        frEntrada = new FileReader(archivoOrigen);
        brEntrada = new BufferedReader(frEntrada);
        fwSalida = new FileWriter(archivoDestino);
        verificarSiExistePalabraARemplazar(palabraAReemplazar);

        while ((linea = brEntrada.readLine()) != null) {


            fwSalida.write(lineaTemp.replace(palabraAReemplazar, nuevaPalabra)+"\r\n");
            fwSalida.flush();



        }

        fwSalida.close();


    } catch (FileNotFoundException e) {

        System.out.println("el archivo no se encuentra en la ruta");

    } catch (IOException e) {

        e.printStackTrace();


    }

}

private void verificarSiExistePalabraARemplazar(String palabraAReemplazar) throws Exception {

    String lineaTemp;
    String pedacitos[];
    boolean noTienePalabra = true;
    FileReader frTemp = frEntrada;
    BufferedReader brTemp = new BufferedReader(frTemp);

    while ((lineaTemp = brTemp.readLine()) != null ){

        if(lineaTemp.contains(palabraAReemplazar)){
            noTienePalabra = false;


        }

    } 


    if(noTienePalabra){

        throw new Exception("La palabra a reemplazar no se encuentra en el archivo");


        }


    }

}

The problem is that this method verifies that the method of replacing stops working properly, directly I do not write anything in the output file, any ideas?

    
asked by ivaaan 16.04.2018 в 07:03
source

1 answer

0

Let's see. It does not change anything, because you have not programmed anything to change it. On the other hand, the boolean "notienepalabra" I would call it "tienepalabra" and would initialize it in false. That is, if it does not find the word, it remains false, if it has ever found it, it is set to true.

Try this:

while ((lineaTemp = brTemp.readLine()) != null ){

    if(lineaTemp.contains(palabraAReemplazar)){
        tienePalabra = true;
        lineaTemp.replaceAll(palabraAReemplazar, nuevaPalabra) //ten en cuenta que tienes que traer la variable esta


    }

} 



if(!tienePalabra){

    throw new Exception("La palabra a reemplazar no se encuentra en el archivo");


    }


}

It would be something like this. Remember to also close the file once you open it. But you have to use the replaceAll, which you were not doing.

    
answered by 16.04.2018 в 12:21