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

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);
}
}













  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值