Java Reference
In-Depth Information
public class Me {
public static void main(String[] args) {
System.out.println(
Me.class.getName().replaceAll( "\\." , "/") + ".class");
}
}
To solve this kind of problem, release 5.0 provides the new static method
java.util.regex.Pattern.quote . It takes a string as a parameter and adds any necessary escapes,
returning a regular expression string that matches the input string exactly. Here is how the program
looks when modified to make use of this method:
package com.javapuzzlers;
import java.util.regex.Pattern;
public class Me {
public static void main(String[] args) {
System.out.println(Me.class.getName().
replaceAll( Pattern.quote(".") , "/") + ".class");
}
}
Another problem with this program is that the correct behavior is platform dependent. Not all file
systems use the slash character to separate hierarchical filename components. To get a valid
filename for the platform on which you are running, you should use the correct platform-dependent
separator character in place of the slash. That is exactly what the next puzzle does.
< Day Day Up >
 
 
Search WWH ::




Custom Search