I have written the following code in java to open another .java file and create tokens (using scanner class)
import java.io.FileReader;
import java.util.Scanner;
public class scanner1 {
public static void main(String[] arg) throws Exception
{
FileReader fin = new FileReader("mysourcefile.java");
Scanner scan=new Scanner(fin);
scan.useDelimiter(" "); // the delimiter pattern required
while(scan.hasNext()) {
System.out.println(scan.next());
}
}
}
My task is to create the tokens of complete Java file and the delimiters should be treated as the tokens also.
So what should be the delimiter pattern to use here in scan.useDelimiter("")?
UPDATE:
The above task was completed using the stringtokenizer. But I don’t know the exact pattern of the delimiter to create tokens for a .java file. Can I have an answer about what the delimiter pattern to use in the given case ?
import java.util.*;
public class sstring2
{
public static void main(String[] args)
{
String s = "a=(b+c); String st='hello! my dear';";
StringTokenizer st = new StringTokenizer(s, "[ =+';().*{}[],!@#$%^&/]", true);
while(st.hasMoreTokens()){
System.out.println(st.nextToken());
}
}
}
This code gives the correct results.
Take a look at the java.lang.instrument packagae. It provides some nice APIs to transform an already loaded class. The retransformation allows to change method bodies as in your case. Here’s the link