项目路径问题

来源:互联网 发布:网上淘宝兼职可靠吗 编辑:程序博客网 时间:2024/05/22 03:23

分析问题:
我们在遇到java项目和web项目时,经常把db.properties文件放在src下面,我们怎么用统一的方法去读取db.properties呢?

在java项目中,db.properties会自动放到Path\bin目录下面而在web项目中,db.properties会自动放到webapps\PathPro\WEB-INF\classes目录下面

1.java Project

import java.io.File;import java.io.FileInputStream;import java.io.InputStream;import java.net.URL;import java.sql.Connection;import java.sql.DriverManager;import java.util.Properties;import org.junit.Test;public class Tools {    @Test    public void Demo() throws Exception {        URL url2 = Tools.class.getResource("/db.properties");        System.out.println(url2);        InputStream stream = Tools.class.getResourceAsStream("/db.properties");        Properties properties = new Properties();        properties.load(stream);        String url = (String) properties.get("url");        String user = (String) properties.get("user");        String password = (String) properties.get("password");        System.out.println(url + "----------" + user + "-----------" + password);        // 连接数据库        Class.forName("com.mysql.jdbc.Driver");        Connection connection = DriverManager                .getConnection(url, user, password);        System.out.println(connection);    }}
输出的结果:file:/E:/Users/Administrator/Workspaces/MyEclipse%2010/Path/bin/db.propertiesjdbc:mysql://localhost:3306/day15----------root-----------rootcom.mysql.jdbc.JDBC4Connection@58d9660d

2.web Project

import java.io.File;import java.io.IOException;import java.io.InputStream;import java.net.URL;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;import java.util.Properties;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class PathServlet extends HttpServlet {    public void doGet(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        URL resource = PathServlet.class.getResource("/db.properties");        System.out.println("resource:"+resource);        InputStream stream = PathServlet.class.getResourceAsStream("/db.properties");        Properties properties = new Properties();        properties.load(stream);        String url = (String)properties.get("url");        String user = (String)properties.get("user");        String password = (String)properties.get("password");        System.out.println(url+","+user+","+password);        //链接数据库        try {            Class.forName("com.mysql.jdbc.Driver");        } catch (ClassNotFoundException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        Connection connection;        try {            connection = DriverManager.getConnection(url, user, password);            System.out.println(connection);        } catch (SQLException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }    public void doPost(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        doGet(request, response);    }}
输出结果为:resource:file:/D:/apache-tomcat-6.0.39/webapps/PathPro/WEB-INF/classes/db.propertiesjdbc:mysql://localhost:3306/day15,root,rootcom.mysql.jdbc.JDBC4Connection@73204425

总结:
可以看出:类名.class.getResource(“/”);可以直接获取到当前class文件所在的根目录,我们就可以通过这个代码实现代码的复用,在java 和 web项目均可以直接使用这种方法。

0 0
原创粉丝点击