replacing text in a .java file

Hey all,

im trying to make a program that, as one of its features, will find and replace a word in a java file. it is a selenium test that has been converted to java to be more precise. i am trying to replace all occurances ofverifyTrue(...) with assertTrue(...). i dont really want to create a new file, so it would be great if you could help me out. online tutorials would be appreciated as well as help. thanks in advance.

[438 byte] By [Thorbjorna] at [2007-11-27 11:39:35]
# 1

Thorbjorn .. Icelandic?

There are numerous editors that allow you to find and replace text like that.

Heck, Notepad can even handle that.

If you are writing your own editor and don't know how to read or write a file, have a look at this:

http://java.sun.com/docs/books/tutorial/essential/io/

dwga at 2007-7-29 17:27:03 > top of Java-index,Java Essentials,Java Programming...
# 2

Hi,

If I understand well the program you try to make will be used only once.

Therefore it is better to open the Java project into an IDE like Eclipse which can easily refactor the project as you wish.

java_knighta at 2007-7-29 17:27:03 > top of Java-index,Java Essentials,Java Programming...
# 3

How many classes do you have?

java_knighta at 2007-7-29 17:27:03 > top of Java-index,Java Essentials,Java Programming...
# 4

perhaps i should have been more specifik. im making a program that will launch some selenium test. these test at recorded in selenium IDE and we run them through the cmd. however im tired of writing the cmd commands every time i have to run a simple test. so i making this program for it. now my boss saw it the other day and he liked it so he said i should expand on it. now im making some buttons that will replace a given string with another given string (ie: verifyTrue with assertTrue) so what i want to do is to once the button is pressed the program searches a list of files through and replaces all instances of the old string with the new string. im thinking that i should search the files through, line by line, and if the string is there, read it into the memory and then replace the string. after the replacement is done write it back into the file. what do you think?

Thorbjorna at 2007-7-29 17:27:03 > top of Java-index,Java Essentials,Java Programming...
# 5

That's a dangerous game to play with source code, what if I define a class that has a method called verifyTrue but has nothing to do with your selenium tests?

Automatic refactoring is not just find/replace, you would need to statically analyze the code to make sure the changes are being made in the correct context.

dwga at 2007-7-29 17:27:03 > top of Java-index,Java Essentials,Java Programming...
# 6

its not exactly source code.. the code im changing is the code for the selenium tests and if the code is wrong here the tests just fail so that is not so important

Thorbjorna at 2007-7-29 17:27:03 > top of Java-index,Java Essentials,Java Programming...
# 7

OK, then the only thing I could see a potential problem with is if the files are large.

It could be a problem to read such a file entirely into memory. In such a case you could read part of the file into memory, make the changes, write it to a temporary file. When done processing the file just delete the original and rename the temporary file to the same name as the original.

dwga at 2007-7-29 17:27:03 > top of Java-index,Java Essentials,Java Programming...
# 8

okay thanks. the files are only 2 or 3 KB so i dont think that will be a problem :)

Thorbjorna at 2007-7-29 17:27:03 > top of Java-index,Java Essentials,Java Programming...