I have this content in a text file :
|0| 1 | 2 |
I want to replace the values between "|" by new one.I tried with this method but it add the new modification after the old one like this:
|0| 1 | 2 |new1|new2|.
instead of :
|0| new1 | new2 |
My code is:
public static void generateReplace(String newfield) throws FileNotFoundException, IOException {
FileArrayProvider rs = new FileArrayProvider();
BufferedWriter bw = new BufferedWriter(new FileWriter(new File("C:/aa.txt"), true));
Scanner sc = new Scanner(new File("C:/test.txt"));
// bw.write("|");
while(sc.hasNext()){
String line = sc.nextLine();
String nueva = rs.replace(line,newfield);
bw.write(nueva);
// bw.newLine();
}
bw.write("|");
bw.close();
}
FileArrayProvider is my class and it has Replace method :
public String replace(String a,String newfield){
String str = a;
String toBeReplaced = str.substring(0, a.length());
String resultado = str.replace(toBeReplaced, newfield);
return resultado;
}