FlieWriter with cycle for

0

I have this code and I want certain words to be modified and I take care that the cycle is modified so that it changes every time but it only modifies the file once, they could tell me where I am wrong or how to do what I am thinking. ?

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

public class PruebaDeBuscarDentroDeUnArchivo {

public static void main(String[] args) {

    try {
        final BufferedReader reader = new BufferedReader(
            new FileReader("C:\text.txt")
        );

        String line = "", content = "";
        while((line = reader.readLine())!= null) {
            content += line + "\r\n";
        }

        reader.close();

        String[] replacement={"cambialo ","usalo"};
        String[] needle ={ "public","static"};
        FileWriter writer = new FileWriter("test.txt");
    try{
    for(int i=0;i<=replacement.length;i++){

        String newContent = content.replaceAll(needle[i], replacement[i]);





        writer.write(newContent);
    }}catch(ArrayIndexOutOfBoundsException e){

    }
    writer.flush();
    writer.close();

    } catch (FileNotFoundException e) {e.printStackTrace();
    } catch (IOException           e) {e.printStackTrace();
    } 
}
}
    
asked by Luis Fernando 04.06.2018 в 07:12
source

2 answers

1

The writer.write(newContent); must go outside the loop. but also have to make substitutions on the result you have already obtained.

Out of the loop String newContent = content;  and within the loop will always work with newContent

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

    public class PruebaDeBuscarDentroDeUnArchivo {

    public static void main(String[] args) {

        try {
            final BufferedReader reader = new BufferedReader(
                new FileReader("C:\text.txt")
            );

            String line = "", content = "";
            while((line = reader.readLine())!= null) {
                content += line + "\r\n";
            }

            reader.close();

            String[] replacement={"cambialo ","usalo"};
            String[] needle ={ "public","static"};
            FileWriter writer = new FileWriter("test.txt");

            String newContent = content;
        try{

        for(int i=0;i<=replacement.length;i++){

            newContent  = newContent.replaceAll(needle[i], replacement[i]);


        }}catch(ArrayIndexOutOfBoundsException e){

        }
        writer.write(newContent);
        writer.flush();
        writer.close();

        } catch (FileNotFoundException e) {e.printStackTrace();
        } catch (IOException           e) {e.printStackTrace();
        } 
    }
    }
    
answered by 04.06.2018 / 07:53
source
0

You had a couple of things to correct in your code, which would look like this:

public static void main(String[] args) {

    try {
        final BufferedReader reader = new BufferedReader(
                new FileReader("C:\Users\ivcl\Desktop\text.txt")
        );
        String[] replacement = {"cambialo ", "usalo"};
        String[] needle = {"public", "static"};
        FileWriter writer = new FileWriter("C:\Users\ivcl\Desktop\test.txt");

        String line = "", content = "";
        while ((line = reader.readLine()) != null) {
            content += line + "\r\n";
        }

        reader.close();

        String newContent = content;
        for (int i = 0; i < replacement.length; i++) {
            newContent = newContent.replaceAll(needle[i], replacement[i]);
            writer.write(newContent);
        }

        writer.flush();
        writer.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Basically, the variable that you use to write (newContent) was declaring it in each loop loop, which is why you were not "accumulating" the substitutions.

The second problem was the condition you used in that loop (i

answered by 04.06.2018 в 08:14