String filePath = this.getClass().getClassLoader().getResource("").toString();得到的结果是:file:/D:/workspace/Order/src/web/WEB-INF/classes/
而在linux下是file:/opt/tomcat/webapps/Order/WEB-INF/classes/
这个结果前面有让人讨厌的file:/,我要利用这里的将结果构建一个绝对的路径,但是由于讨厌的file:/,
我原计划根据系统的类型,
如果是windows:
  filePath.replaceAll("file:/","");
如果是linux
  filePath.replaceAll("file:", "");只是觉得这样写有点别扭。不知道各位有木有更好的办法。

解决方案 »

  1.   

    用 String 的 startWith()方法测试 试试看!
    if(filePath.startWith("file:/")){
       filePath=filePath.subString(filePath.indexOf("/")+1);
    }//(没测试过)
      

  2.   

    ServletContext sc= getServletContext();
    String ClassPath = sc.getRealPath("/WEB-INF/classes/");
      

  3.   

    貌似,windows下2个":",linux下只有一个
    lz可以通过这个判断,然后作处理
      

  4.   

    String str="file:/d:/workspace/Order/src/web/WEB-INF/classes/ ";
            if(str.startsWith("file:")){
               str= str.substring(5);
               if(str.indexOf(':')!=-1){
                   str=str.substring(1);
               }
            }
    前提条件是文件夹的名称里面不能含有“:”,不过windows下貌似不能用“:”做文件名,linux没试过