Thursday, November 18, 2010

Listing Hardcoded Strings in a Java Class

The below program Lists all the hard coded strings available in a java source. It will be useful for you to move all the hard coded strings of a java class to a constants file by customizing this program. It is an initial draft of the program.. keep on following this blog for the final version.

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

public class Test {

public static void main(String[] args) throws Exception {

String fileName = "D:/workspace/demo/src/util/Utility.java";
File file = new File(fileName);
FileInputStream fis = new FileInputStream(file);
BufferedReader bis = new BufferedReader(new InputStreamReader(fis));
String strLine;
while((strLine = bis.readLine()) != null) {

while(strLine.contains("\"")) {
int beginIndex = strLine.indexOf('"');
String remString = strLine.substring(beginIndex+1);
int endIndex = remString.indexOf('"');
"+(beginIndex+endIndex));
String temp = strLine.substring(beginIndex, (beginIndex+endIndex+2));
System.out.println("token : "+temp);
strLine = strLine.substring(beginIndex+endIndex+2);

}
}

}

}

No comments:

Post a Comment