I need to make a find and replace utility in java

1

I need help with a util java.

In the program that I am doing I need a utility of find and replace .

I need it to work with text and excel documents.

I can not use the program tools like notepad or the same excel to make find and replace . Since they ask me to be automatic, it is for a web application of loads to data base and some of the data files come to us with data that need to be replaced.

At the moment I have this

static void modifyFile(String filePath, String oldString, String newString){

    File fileToBeModified = new File(filePath);
    String oldContent = "";
    BufferedReader reader = null;
    FileWriter writer = null;

    try
    {
        reader = new BufferedReader(new FileReader(fileToBeModified)); 
        //Lectura de todas las líneas del archivo
        String line = reader.readLine();
        while (line != null) 
        {
            oldContent = oldContent + line + System.lineSeparator();
            line = reader.readLine();
        }
        //Reemplazo el contenido viejo
        String newContent = oldContent.replaceAll(oldString, newString);
        //Reescrivo el fichero
        writer = new FileWriter(fileToBeModified);
        writer.write(newContent);
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    finally
    {
        try
        {
            reader.close();
            writer.close();
        } 
        catch (IOException e) 
        {
            e.printStackTrace();
        }
    }
}

public static void main(String[] args){
    modifyFile("file.txt", ".", "0");
    System.out.println("done");
}

I replaced the file, but not the points, if not everything by 0.

    
asked by Iván Montero 11.05.2018 в 10:31
source

1 answer

1

The problem is that you use replaceAll .

The first parameter is a string that represents a

answered by 11.05.2018 / 11:39
source