class周辺から、リソースを見つけるメソッド
たぶん、jarにも対応
PathnameUtilsに加えよう
public File searchFileAroundClass(String fileName){ ClassLoader loader=this.getClass().getClassLoader(); String path=this.getClass().getName().replaceAll("\\.","/"); path=path+".class"; URL url=loader.getResource(path); String classPath=url.toString(); System.out.println(classPath); if(classPath.startsWith("jar:")){ int last=classPath.lastIndexOf("!"); classPath=classPath.substring("jar:".length(),last); } System.out.println(classPath); if(classPath.startsWith("file:/")){ File checkFile=new File(classPath.substring("file:/".length())).getParentFile(); while(true){ File tmp=new File(checkFile,fileName); System.out.println(tmp.getAbsolutePath()); if(tmp.exists()) return tmp; else{ checkFile=checkFile.getParentFile(); if(checkFile==null) break; } } return null; }else{ return null; } }>|