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();
}
}
}