项目中有一个xxx.xsl模板文件,需要在项目中读取,本地运行可以读取到,打成jar包后就读取不到了
public class FileUtil {
/**
* 读取配置文件
* @return
*/
public static byte[] readConfigFile(String cfgFile) {
try {
InputStream in=FileUtil.class.getClassLoader().getResource(cfgFile).openStream();
BufferedReader br=new BufferedReader(new InputStreamReader(in));
StringBuilder sb=new StringBuilder();
String line="";
while((line=br.readLine())!=null) {
sb.append(line);
}
return sb.toString().getBytes();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
//省略其他的方法
//主方法
public static void main(String[] args) throws IOException {
byte[] buf = FileUtil.readConfigFile("templates/device_tem.xsl");
}
}
路径这样"/templates/device_tem.xsl"的写法是错误的,正确写法"templates/device_tem.xsl"