View Single Post
  #4 (permalink)  
Old 10-15-2008, 07:05 PM
Norm's Avatar
Norm Norm is offline
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,225
Norm is on a distinguished road
Can you explain what the objective of your code is?
Here's simple code to replace the %20 with a blank.
Your code already does it. I don't understand what the regex if for.

Code:
// Find path to class file (w/o %20) import java.net.URLDecoder; public class FindPathToFile { public static void main(String[] args) { try { new FindPathToFile(); }catch(Exception x){x.printStackTrace();} } private final static String Percent = "%"; public FindPathToFile() throws Exception { // Okay, this should be our name. String string = this.getClass().getName(); string.replaceAll("\\.", "/"); System.out.println("string=" + string + ", user.dir=" + System.getProperty("user.dir")); StringBuffer sb = new StringBuffer(); // Normal sanity check. if(string != null) { // sb.append(/*FileDog.*/this.getClass().getResource("/" + string + ".class")); java.net.URL url= new java.net.URL(sb.toString()); System.out.println("url=" + url); String URLStr = url.toString(); if(URLStr.indexOf(Percent) >= 0) { URLStr = URLDecoder.decode(URLStr, "UTF8"); System.out.println("decode= " + URLStr); } // java.util.regex.Pattern pitterPatter = java.util.regex.Pattern.compile("file:(/|\\w|\\d|-|_|\\.|!|~|\\*|\'|\\(|\\))+WEB-INF/");// // String dfltCS = new String(java.nio.charset.Charset.defaultCharset().toString()); System.out.println("defaultCS=" + dfltCS); //1252 String decodedURL = new String(java.net.URLDecoder.decode(url.toExternalForm(), dfltCS)); System.out.println("decode2=" + decodedURL); /****** here we go... ******/ java.util.regex.Matcher mtchr = pitterPatter.matcher(decodedURL); // If we captured state of where we are at .... while(mtchr.find()) { // sb.append(mtchr.group()); // isValid=Boolean.valueOf(true);// } System.out.println( sb.toString());// } // prints: file:/C:/Documents%20and%20Settings/Owner/.Arachnophilia/CustomClasses/FileDog.class } // end main } // end class /* Output from above Running: D:\Java\jre6_10\bin\java.exe -classpath D:\JavaDevelopment;. -Xmx128M FindPathToFile string=FindPathToFile, user.dir=D:\JavaDevelopment\Testing\JavaForum\Folder with blanks url=file:/D:/JavaDevelopment/Testing/JavaForum/Folder%20with%20blanks/FindPathToFile.class decode= file:/D:/JavaDevelopment/Testing/JavaForum/Folder with blanks/FindPathToFile.class defaultCS=windows-1252 decode2=file:/D:/JavaDevelopment/Testing/JavaForum/Folder with blanks/FindPathToFile.class file:/D:/JavaDevelopment/Testing/JavaForum/Folder%20with%20blanks/FindPathToFile.class 0 error(s) */
Reply With Quote