Q:

Cannot edit a .java file with a java program

I am a beginner in java. Ihave a string replacement code in which the user specifies the file path, the string to replace and the string to replace with. The code just works fine with .txt or .in files. But when I try to edit a .java file for which I intended to write the code, it somehow is unable to edit it. Can anybody suggest where actually the problem is? My code goes as follows:
import java.io.*;
import java.util.*;
public class StringReplace{
    public static void main(String[] args) throws IOException
    {
        System.out.println("Enter path of file:");
        Scanner sc=new Scanner(System.in);
        String path=sc.nextLine();
        File f=new File(path);
        if (f.canRead())
        {
            System.out.print("Now enter the string to replace:_");
            String oldString=sc.nextLine();
            System.out.print("Now enter the string to replace with:_");
            String newString=sc.nextLine();
            StringBuffer sb=new StringBuffer();
            sc=new Scanner(f);
            sc.useDelimiter("");
            while(sc.hasNext())
            {
                sb.append(sc.next());
            }
            sc.close();
            FileWriter fw=new FileWriter(path);
            PrintWriter pw=new PrintWriter(fw,true);
            System.out.println(sb);
            pw.println(sb.toString().replaceAll(oldString, newString));
            fw.close();
            pw.close();
            System.out.print("DONE!");
        }
        else
            System.out.println("File Does Not Exist");
        }
    }
}

A:

As the comments state, there should be no difference between a ".java" file and any other text file.
I suspect that the problem is that you haven't realized that your editor application is actually coded to do a regular expression search / replace, not a simple string search / replace.   (That is what String.replaceAll(...) does ...)  If unwittingly supply a "string to replace" that contains regex metacharacters, you may find that it doesn't match or that it matches in a place that you are not expecting.

