2

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;

}
Blip
  • 3,061
  • 5
  • 22
  • 50
  • Your sample looks likeyou add 2 values at the end? – Jens Apr 27 '15 at 09:32
  • yes but i want to replace the old one by the new one like this : |new1|new2| – yasser101991 Apr 27 '15 at 09:34
  • Can you add a working example. – Jens Apr 27 '15 at 09:37
  • 1
    What is a `FileArrayProvider`? Do you have code for its replace method? – nitegazer2003 Apr 27 '15 at 09:41
  • But wahat is `newfield` – Jens Apr 27 '15 at 09:41
  • I have this content in test.txt : |0| 1 | 2 | when i call the method generateReplace(3) the file change like this : |0| 1 | 2 |3| not to |3| – yasser101991 Apr 27 '15 at 09:43
  • FileArrayProvider is my class and it have a replace method: public String replace(String a,String newfield){ String str = a; int startIndex = a.indexOf("|"); String toBeReplaced = str.substring(0, a.length()); String resultado = str.replace(toBeReplaced, newfield); return resultado; } – yasser101991 Apr 27 '15 at 09:45
  • please clarify what you want and what is the logic you are following editing the post and not in comments. – Blip Apr 27 '15 at 09:55
  • As a tip : You should always name variables, functions and methods in the same language. Usually, it is english - the code is internationalizable. If you really don't want any english for some reason, have none. – Docteur Apr 27 '15 at 10:04
  • If you want to replace the entire line with the new field, why do you even care about what written there in the first place? – Bhoot Apr 27 '15 at 10:05
  • What is the output you want? – Blip Apr 27 '15 at 10:11
  • do you mean to say as stated in your question `|0| 1 | 2 |` to be replaced to `|0| new1 | new2 |` ? – Blip Apr 27 '15 at 10:24
  • now how to you tell you program to replace `1` with `new1` and replace `2` with `new2`. Also kindly add @ when you reply to the comment to a specific user to get noticed earlier. – Blip Apr 27 '15 at 10:29
  • @Blip you can consider 0 as a new value .Well to more clarify my question i have an Gui interface and it has 3 Textbox and a submit Button.At first it loads the values existing in the text file (0,1,2)in each TextBox .When i modify them like fo example (new1,new2,new3) and i click on the submit Button the text file should change to |new1|new2|new3| But it changes to |0| 1 | 2 |new1|new2|new3| . – yasser101991 Apr 27 '15 at 10:44
  • are you overwriting the file or saving it to a new file? – Blip Apr 27 '15 at 10:47
  • @Blip I'm testing on a new file called "aa.txt" but at the end i'm trying to overwrite my file called "test.txt" – yasser101991 Apr 27 '15 at 10:51

2 Answers2

0

You are appending the file thus you do not get your required output, just change the true to false in this line

BufferedWriter bw = new BufferedWriter(new FileWriter(new File("/Test/src/com/aa.txt"), false));

FileWriter public FileWriter(File file, boolean append)

Parameters: file - a File object to write to

append - if true, then bytes will be written to the end of the file rather than the beginning

FileWriter in java

Md. Nasir Uddin Bhuiyan
  • 1,598
  • 1
  • 14
  • 24
0

My answer is I do not understand why are you trying to replace the String in the file from which you are loading the data.

There is a simpler approach to this. Since you are loading all the data to the GUI Text Boxes, You simply take the data from the from the text boxes and format them into a string and write the string to a file. If you are thinking of replacing the data on the same file, then you have to call the delete(Path path) method of the File class and then write the file.

Blip
  • 3,061
  • 5
  • 22
  • 50