JAVA读取外部资源的方法
2015-08-24 15:09
465 查看
在java代码中经常有读取外部资源的要求:如配置文件等等,通常会把配置文件放在classpath下或者在web项目中放在web-inf下.
1.从当前的工作目录中读取:
[java] view
plaincopy
try {
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream("wkdir.txt")));
String str;
while ((str = in.readLine()) != null) {
System.out.println(str);
}
in.close();
} catch (IOException e) {
}
2,从classpath中读取(读取找到的第一个符合名称的文件):
[java] view
plaincopy
try {
InputStream stream = ClassLoader.getSystemResourceAsStream("fileinjar.txt");
BufferedReader in = new BufferedReader(new InputStreamReader(stream));
String str;
while ((str = in.readLine()) != null) {
System.out.println(str);
}
in.close();
} catch (IOException e) {
}
3,从classpath中读取(读取找到的所有符合名称的文件,如Spring中带有classpath*:前缀的情况就会从classpath中遍历):
[java] view
plaincopy
try {
Enumeration resourceUrls = Thread.currentThread().getContextClassLoader().getResources("fileinjar.txt");
while (resourceUrls.hasMoreElements()) {
URL url = (URL) resourceUrls.nextElement();
System.out.println(url);
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String str;
while ((str = in.readLine()) != null) {
System.out.println(str);
}
in.close();
}
} catch (IOException e) {
}
4,从URL中读取:
[java] view
plaincopy
try {
URL url = new URL("http://blog.csdn.net/kkdelta");
System.out.println(url);
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String str;
while ((str = in.readLine()) != null) {
System.out.println(str);
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
5,web项目从web-inf文件夹读取(通过得到ServletContext读取,可以在servlet或者能够得到request的类中使用):
[java] view
plaincopy
try {
URL url = (URL) getServletContext().getResource("/WEB-INF/webinffile.txt");
// URL url = (URL)req.getSession().getServletContext().getResource("/WEB-INF/webinffile.txt");
System.out.println(url);
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String str;
while ((str = in.readLine()) != null) {
System.out.println(str);
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
以上代码在eclipse环境中运行测试过.不过最近在用JUnit的时候,通过ant运行JUnit时通过ClassLoader.getSystemResourceAsStream("file.txt");的方式去找不到文件.改成 Xclass.class.getClassLoader().getResourceAsStream("file.txt");能从ant指定的classpath中找到文件.原因是ClassLoader和Xclass.class.getClassLoader()是不同的,查找的路径不一样.
1.从当前的工作目录中读取:
[java] view
plaincopy
try {
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream("wkdir.txt")));
String str;
while ((str = in.readLine()) != null) {
System.out.println(str);
}
in.close();
} catch (IOException e) {
}
2,从classpath中读取(读取找到的第一个符合名称的文件):
[java] view
plaincopy
try {
InputStream stream = ClassLoader.getSystemResourceAsStream("fileinjar.txt");
BufferedReader in = new BufferedReader(new InputStreamReader(stream));
String str;
while ((str = in.readLine()) != null) {
System.out.println(str);
}
in.close();
} catch (IOException e) {
}
3,从classpath中读取(读取找到的所有符合名称的文件,如Spring中带有classpath*:前缀的情况就会从classpath中遍历):
[java] view
plaincopy
try {
Enumeration resourceUrls = Thread.currentThread().getContextClassLoader().getResources("fileinjar.txt");
while (resourceUrls.hasMoreElements()) {
URL url = (URL) resourceUrls.nextElement();
System.out.println(url);
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String str;
while ((str = in.readLine()) != null) {
System.out.println(str);
}
in.close();
}
} catch (IOException e) {
}
4,从URL中读取:
[java] view
plaincopy
try {
URL url = new URL("http://blog.csdn.net/kkdelta");
System.out.println(url);
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String str;
while ((str = in.readLine()) != null) {
System.out.println(str);
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
5,web项目从web-inf文件夹读取(通过得到ServletContext读取,可以在servlet或者能够得到request的类中使用):
[java] view
plaincopy
try {
URL url = (URL) getServletContext().getResource("/WEB-INF/webinffile.txt");
// URL url = (URL)req.getSession().getServletContext().getResource("/WEB-INF/webinffile.txt");
System.out.println(url);
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String str;
while ((str = in.readLine()) != null) {
System.out.println(str);
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
以上代码在eclipse环境中运行测试过.不过最近在用JUnit的时候,通过ant运行JUnit时通过ClassLoader.getSystemResourceAsStream("file.txt");的方式去找不到文件.改成 Xclass.class.getClassLoader().getResourceAsStream("file.txt");能从ant指定的classpath中找到文件.原因是ClassLoader和Xclass.class.getClassLoader()是不同的,查找的路径不一样.
相关文章推荐
- 【java】 java.util.concurrent
- SpringEL详解及应用
- JEECG社区公司,招聘全职JAVA工程师(全职)
- java面试总结-(hibernate ibatis struts2 spring)
- 安装TestNG在eclipse中的插件
- ios信息推送 异常 javax.net.ssl.SSLHandshakeException: Received fatal alert: handshak
- 深入理解java异常处理机制
- [Java] cmd命令行如何切换目录
- Spring实现AOP的4种方式
- java中Timer定时器实现和通过定时器调用url请求
- join()之让线程按顺序执行
- 利用模板导出文件(二)之jacob利用word模板导出word文件(Java2word)
- JAVA获取系统当前时间
- Spring Security password hashing example
- Spring MVC 教程,快速入门,深入分析
- 黑马学习笔记_java基础知识总结
- 遍历Map的两种方法:keySet()和entrySet()
- 基于Maven的Spring + Spring MVC + Mybatis的环境搭建
- 使用javaHelp制作java swing帮助文档
- 第22章 Spring基础