码迷,mamicode.com
首页 > 其他好文 > 详细

servlet和普通类获取资源文件的方法

时间:2014-07-10 21:28:31      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:style   http   java   文件   os   io   

package cn.servlet.demo1;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Properties;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


//servlet获取资源文件
public class servlet4 extends HttpServlet {


public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

//获取到资源文件
InputStream in = this.getServletContext().getResourceAsStream("WEB-INF"+File.separator+"classes"+File.separator+"db.properties");

//在servlet下直接访问目录下的文件
// InputStream in = this.getServletContext().getResourceAsStream("file"+File.separator+"db.properties");

//只要是Properties文件都要用Properties读
Properties pro = new Properties();
pro.load(in);
//获取到资源文件中的内容
String url = pro.getProperty("url");
String username = pro.getProperty("username");
String password = pro.getProperty("password");

PrintWriter out = response.getWriter();
out.println(url+username+password);
}


public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);


}

//另一种访问资源文件的方法
public void test() throws IOException{
String path = this.getServletContext().getRealPath("/WEB-INF/classes/db.properties");
FileInputStream file = new FileInputStream(path);

Properties pro = new Properties();
pro.load(file);

String url = pro.getProperty("url");
String username = pro.getProperty("username");
String password = pro.getProperty("password");

System.out.println(url+username+password);

}


}




普通类获取资源文件
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;


import org.junit.Test;


public class demo {
@Test
public void show() throws IOException{
//不会更新
// InputStream in = demo.class.getClassLoader().getResourceAsStream(".."+File.separator+".."+File.separator+"file/file.properties");
// Properties pro = new Properties();
// pro.load(in);
// String url = pro.getProperty("url");
// System.out.println(url);

//会更新的
// String path = demo.class.getClassLoader().getResource(".."+File.separator+".."+File.separator+"file/file.properties").getPath();
// Properties pro = new Properties();
// FileInputStream in = new FileInputStream(path);
// pro.load(in);
// String url = pro.getProperty("url");
// System.out.println(url);

//db.properties 必须在 WEB-INF\classes 目录下才可以直接访问。
// String path = demo.class.getClassLoader().getResource("db.properties").getPath();
// Properties pro = new Properties();
// FileInputStream in = new FileInputStream(path);
// pro.load(in);
// String url = pro.getProperty("url");
// System.out.println(url);


InputStream in = demo.class.getClassLoader().getResourceAsStream("db.properties");
Properties pro = new Properties();
pro.load(in);
String name = pro.getProperty("url");
System.out.println(name);
}
}













servlet和普通类获取资源文件的方法,布布扣,bubuko.com

servlet和普通类获取资源文件的方法

标签:style   http   java   文件   os   io   

原文地址:http://blog.csdn.net/u013032887/article/details/37598213

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!