0

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
javasimple
  • 23
  • 4

1 Answers1

0

Have a look at these modifications, you would want to replace data1 right after you detect it's different, then just read the rest of the lines and append them to your StringBuilder

public static void main(String[] args) throws Exception {
    test("aaa", "bbb");
}

public static void test(String DATA1, String DATA2) throws IOException {
    String file = "data.txt";
    String newFile = "dataNew.txt";
    
    String CurrentLine = "";
    BufferedReader br = new BufferedReader(new FileReader(file));
    while ((CurrentLine = br.readLine()) != null) {

        String[] modify = CurrentLine.split(", ");
        String data1 = modify[0];
        String data2 = modify[1];
        String data3 = modify[2];
        String data4 = modify[3];

        BufferedWriter bw = new BufferedWriter(new FileWriter(newFile, true));

        if (!data1.contains(DATA1)) {
            StringBuilder sb = new StringBuilder();
            sb.append(CurrentLine.replace(data1, DATA1));
            sb.append("\n");

            while ((CurrentLine = br.readLine()) != null) {
                sb.append(CurrentLine);
                sb.append("\n");
            }

            bw.write(sb.toString());

            File RESIDENTS_OLDDETAILS = new File(file);
            RESIDENTS_OLDDETAILS.delete();

            File RESIDENTS_NEWDETAILS = new File(newFile);
            RESIDENTS_NEWDETAILS.renameTo(RESIDENTS_OLDDETAILS);

            br.close();
            bw.close();
            break;
        }
    }
}

Input:

abc, 123, 456, 3345

test, t245, 442, d444

dddd, 4444, ffff, 333

Output:

aaa, 123, 456, 3345

test, t245, 442, d444

dddd, 4444, ffff, 333

Community
  • 1
  • 1
Shar1er80
  • 9,001
  • 2
  • 20
  • 29
  • Hi, unfortunately it does not change the data1, instead it re-write 2nd line and 3rd line into the file. – javasimple May 10 '15 at 18:40
  • @javasimple Updated answer. I just called my files data.txt and dataNew.txt. Obviously use your data file names. – Shar1er80 May 10 '15 at 18:51
  • Hi thank you so much, it did however change data1, somehow it rewrite again. Also, it is possible to made the modification in the same text file name? – javasimple May 10 '15 at 19:10
  • @javasimple The provided answer I gave does just that. Look at the difference between your edited code and my posted answer. They are not the same. – Shar1er80 May 10 '15 at 19:13
  • Thanks, it was my bad. Is it possible to edit into the same file name? – javasimple May 10 '15 at 19:18
  • @javasimple Yes, but that's a different question. In this question, we're writing to a different file and then renaming it to the original file. If you're wanting to read/write from/to the same file, have a look at http://stackoverflow.com/questions/20753600/creating-writing-and-editing-same-text-file-in-java – Shar1er80 May 10 '15 at 19:21