Let's say I have the following code:
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class EditFile { public static void main(String[] args) { try{ String verify, putData; File file = new File("file.txt"); file.createNewFile(); FileWriter fw = new FileWriter(file); BufferedWriter bw = new BufferedWriter(fw); bw.write("Some text here for a reason"); bw.flush(); bw.close(); FileReader fr = new FileReader(file); BufferedReader br = new BufferedReader(fr); while( br.readLine() != null ){ verify = br.readLine(); if(verify != null){ putData = verify.replaceAll("here", "there"); bw.write(putData); } } br.close(); }catch(IOException e){ e.printStackTrace(); } } }
All I wanted to do was write something in a text file, in my case "Some text is here for some reason." Then, to read the data from my file and finally change the text from my file from "Some text is here for a reason" to "Some text is there for some reason". I ran the code, but all that happens is to write in my file "Some text here for some reason."
I tried to figure out what might be wrong in my code, but unfortunately it was in vain. Any advice or rewriting is much appreciated from me.
source share