背景:读取mcbuild.jar中的资源文件TestMapper.xml。FileCodeBulder是jar包中的一个类文件。同目录下放着TestMapper.xml文件。
一般来说,读取文件有两种方式:
1.getResource 不可以正常使用
URL url = FileCodeBulder.class.getResource("TestMapper.xml");//这里不能写成"/TestMapper.xml"
上面是获取到类文件同目录下TestMapper.xml,如果文件不存在url会为null。
if (url == null) {
System.out.println("TestMapper.xml is not exist!");
return;
}
有趣的是此时url不会null。
String path = url.getPath().replaceAll("%20", " ");
File file = new File(path);//path为 E:/workspace/MIS/IntegManage/WebRoot/WEB-INF/lib/mcbuild.jar!/com/ht/build/TestMapper.xml
System.out.println(file.exists());
//输出为false。这里我想不明白,唯一合理的解估计是 “相对工程,jar包中的文件是不存在的”。
2.getResourceAsStream 可以正常使用
InputStream file = FileCodeBulder.class.getResourceAsStream("TestMapper.xml"); // 这里不能写成"/TestMapper.xml"
byte[] temp = new byte[1024];
String content = "";
int len = 0;
try {
while ((len = file.read(temp)) != -1) {
content += new String(temp, 0, len, "utf-8");
}
file.close();
}
catch (Exception e) {
System.out.println(fileName + " is not exist!");
e.printStackTrace();
}
return content;
在结尾在闲扯一下FileCodeBulder.class.getResource("TestMapper.xml")和FileCodeBulder.class.getClass().getResource("/TestMapper.xml").
前面的是获得跟FileCodeBulder同目录下的TestMapper.xml文件
而后面的是获得工程class目录下的TestMapper.xml。 如:E:/workspace/MIS/IntegManage/WebRoot/WEB-INF/classes/TestMapper.xml。
两者除了得到的路径不一样,输入的参数也有差异。后面的多了“/”.
一般来说,读取文件有两种方式:
1.getResource 不可以正常使用
URL url = FileCodeBulder.class.getResource("TestMapper.xml");//这里不能写成"/TestMapper.xml"
上面是获取到类文件同目录下TestMapper.xml,如果文件不存在url会为null。
if (url == null) {
System.out.println("TestMapper.xml is not exist!");
return;
}
有趣的是此时url不会null。
String path = url.getPath().replaceAll("%20", " ");
File file = new File(path);//path为 E:/workspace/MIS/IntegManage/WebRoot/WEB-INF/lib/mcbuild.jar!/com/ht/build/TestMapper.xml
System.out.println(file.exists());
//输出为false。这里我想不明白,唯一合理的解估计是 “相对工程,jar包中的文件是不存在的”。
2.getResourceAsStream 可以正常使用
InputStream file = FileCodeBulder.class.getResourceAsStream("TestMapper.xml"); // 这里不能写成"/TestMapper.xml"
byte[] temp = new byte[1024];
String content = "";
int len = 0;
try {
while ((len = file.read(temp)) != -1) {
content += new String(temp, 0, len, "utf-8");
}
file.close();
}
catch (Exception e) {
System.out.println(fileName + " is not exist!");
e.printStackTrace();
}
return content;
在结尾在闲扯一下FileCodeBulder.class.getResource("TestMapper.xml")和FileCodeBulder.class.getClass().getResource("/TestMapper.xml").
前面的是获得跟FileCodeBulder同目录下的TestMapper.xml文件
而后面的是获得工程class目录下的TestMapper.xml。 如:E:/workspace/MIS/IntegManage/WebRoot/WEB-INF/classes/TestMapper.xml。
两者除了得到的路径不一样,输入的参数也有差异。后面的多了“/”.
相关推荐
Java加载资源文件的两种方法getResource与getResourceAsStream
java getResourceAsStream种类,分我所知道的3种情况
主要介绍了Java中getResourceAsStream用法,较为详细的分析了getResourceAsStream的功能及用法,需要的朋友可以参考下
Java中getResourceAsStream的用法.md
主要介绍了Java 使用getClass().getResourceAsStream()方法获取资源的相关资料,这里主要讲解哪种方式可以获取到文件资源,需要的朋友可以参考下
ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault()); 3。使用java.util.PropertyResourceBundle类的构造函数 示例: InputStream in = new BufferedInputStream(new FileInputStream...
Document document = sb.build(this.getClass().getClassLoader().getResourceAsStream(fileName)); Element root = document.getRootElement(); List list = XPath.selectNodes(root, "/beans/bean"); for...
InputStream inputStream = CommonTest.class.getClassLoader().getResourceAsStream("mybatis-configuration.xml"); SQLSessionFactory SQLSessionFactory = new SQLSessionFactoryBuilder().build(inputStream)...
getResourceAsStream( " path/to/resource.txt " ); 此问题影响非Android单元测试和Java资源,仅在具有“单元测试”配置的Android Studio上执行。 它不影响使用Gradle(在Android Studio或命令行中)执行测试用例...
is = new Document().getClass().getResourceAsStream("/resources/aspose.word.license.xml"); if(is==null) throw new RuntimeException("Cannot find licenses file. Please contact wdmsyf@yahoo....
InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);XML 配置文件(configuration XML)中包含了对 ...
javaEE-web的文件路径, 用getClass().getResourceAsStream("/")来确定文件路径 并用jdom读取xml和下载1
InputStream inStream = this.getClass().getClassLoader().getResourceAsStream("mobilesoap.xml"); try { addressView.setText(MobileInfoService.getMobileAddress(inStream, mobile)); } catch...
activiti部署中文乱码解决,亲测有效, 1.activiti-webapp-explorer2\src\main\webapp\editor-app\i18n下en.json 2.activiti-webapp-explorer2\src\main\resources下 activiti-custom-context.xml ...
java项目读取配置文件的实例代码,使用class.getClassLoader().getResourceAsStream方法。
刚开始简单破解了一下。 生成PDF都没问题,后来发现生成... InputStream is = XXX.class.getResourceAsStream("/pdflicense.xml"); license.setLicense(is); is.close(); 其中pdflicense.xml已经包含在jar文件里面
.getResourceAsStream(DBINFO_FIIE_NAME); try { infos.load(is); } catch (Exception e) { e.printStackTrace(); throw new ExceptionInInitializerError("属性文件加载错误"); } finally { ...
InputStream is = com.aspose.words.Document.class.getResourceAsStream("/com.aspose.words.lic_2999.xml"); License aposeLic = new License(); aposeLic.setLicense(is); result = true; is.close(); } ...
InputStream in=getClass().getResourceAsStream(propFileName); prop.load(in); //通过输入流对象加载Properties文件 dbClassName = prop.getProperty("DB_CLASS_NAME"); //获取数据库驱动 dbUrl =...
InputStream is = Demo1.class.getResourceAsStream("/students.xml");// src目录下 // 1. 获得文档解析器工厂对象 DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); // 2. ...