i hope you can help me in this, I am reading a file with multiple lines. I use BufferedReader to separate the datas. If data 1 does not match with new data 1 input, it will use BufferedWriter to replace the new data 1 input into old data 1. With the below code, it did changed the data 1 but it append with other line data 2 and 3. I want to change only data 1 but the lines are remain the same.
Example(Input)
abc, 123, 456, 3345
test, t245, 442, d444
dddd, 4444, ffff, 333
public void test(String DATA1, String DATA2) throws IOException{
br = new BufferedReader(new FileReader(file));
while ((CurrentLine = br.readLine()) != null) {
String[] modify = CurrentLine.split(split);
String data1 = modify[0];
String data2 = modify[1];
String data3 = modify[2];
String data4 = modify[3];
bw = new BufferedWriter(new FileWriter(file, true));
if (!data1.contains(DATA1)) {
StringBuilder sb = new StringBuilder();
while(CurrentLine != null){
sb.append(CurrentLine.replace(data1, DATA1));
sb.append("\n");
CurrentLine = br.readLine();
}
bw.write(sb.toString());
RESIDENTS_OLDDETAILS = new File(oldfile);
RESIDENTS_OLDDETAILS.delete();
RESIDENTS_NEWDETAILS = new File(newfile);
RESIDENTS_NEWDETAILS.renameTo(old);
br.close();
bw.close();
Example(Output of the above code)
newabc, t245, 442, d444
newabc, t245, 442, d444
newabc, t245, 442, d444
Expected output
aaa, 123, 456, 3345
test, t245, 442, d444
dddd, 4444, ffff, 333
Edited Expected output
aaa, 123, 456, 3345
test, t245, 442, d444
dddd, 4444, ffff, 333aaa, 123, 456, 3345
test, t245, 442, d444
dddd, 4444, ffff, 333