1

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");
        }
    }
}
14
  • 3
    Well what do it do? I wouldn't use Scanner for this, to be honest - I'd use BufferedReader and just read a line at a time, probably writing as I went.
    – Jon Skeet
    Aug 25, 2012 at 9:57
  • Who knows what actual problem is working with binary or text stream. Java file may contain unicode chars.
    – Roman C
    Aug 25, 2012 at 11:03
  • For me Code works fine in .java file also.
    – Vinesh
    Aug 25, 2012 at 11:11
  • @Jon Skeet: I am using scanner here with a blank string as delimiter since I need to preserve the all default delimiters of scanner and bufferedReader in the edited file. Only "oldString"s will be changed. Aug 25, 2012 at 12:05
  • 1
    What is the error you get exactly ?
    – Y__
    Aug 25, 2012 at 12:09

1 Answer 1

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.