
how do i search for an occurance of a word in a file and replace with the user input in java?

Java Replace a word in a file
import java.io.*;
import java.util.*;
class FileOperations{
public static void main(String[] args)throws Exception{
int count=0;
File f=new File("C:/hello.txt");
BufferedReader br=new BufferedReader(new FileReader(f));
String str="",st;
while((st=br.readLine())!=null){
str+=st+" ";
}
Scanner input=new Scanner(System.in);
System.out.print("Search Word: ");
String word=input.next();
String arr[]=str.split(" ");
for(int i=0;i<arr.length;i++){
if(arr[i].equals(word)){
count++;
}
}
System.out.print("Enter Word: ");
String newword=input.next();
System.out.println("The word "+word+" occurs "+count+" times in a file");
String newstring=str.replace(word,newword);
BufferedWriter bw=new BufferedWriter(new FileWriter(f));
bw.write(newstring);
bw.close();
}
}

Thank u very much... this work fine....

Thank u very much... this work fine....
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.