首页 > 代码库 > servlet和普通类获取资源文件的方法
servlet和普通类获取资源文件的方法
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);
}
}