天天看點

讀取web中不同位置的檔案

package cn.itcast.web.servlet;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;

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

//用servletContext讀取web工程不同位置的資源檔案
public class ServletDemo11 extends HttpServlet {

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

    //讀取src下面的配置檔案
    private void test5() throws IOException {
        InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
        Properties prop = new Properties();  //map
        prop.load(in);


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

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

    //讀取src下面的配置檔案(傳統方式不可取)
    private void test4() throws IOException {
        FileInputStream in = new FileInputStream("classes/db.properties");
        System.out.println(in);
    }


    //讀取配置檔案的第三種方式
    private void test3() throws IOException {
        ServletContext context = this.getServletContext();
        URL url = context.getResource("/resource/db.properties");
        InputStream in = url.openStream();

    }


    //讀取配置檔案的第二種方式
    private void test2() throws IOException {
        ServletContext context = this.getServletContext();
        String realpath = context.getRealPath("/db.properties");  //c:\\sdsfd\sdf\db.properties

        //擷取到操作檔案名   realpath=abc.properties
        String filename = realpath.substring(realpath.lastIndexOf("\\")+);
        System.out.println("目前讀到的檔案是:" + filename);


        FileInputStream in = new FileInputStream(realpath);
        Properties prop = new Properties();
        prop.load(in);

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

        System.out.println("檔案中有如下資料:");
        System.out.println(url);
        System.out.println(username);
        System.out.println(password);

    }


    //讀取配置檔案的第一種方式
    private void test1() throws IOException {

        ServletContext context = this.getServletContext();
        InputStream in = context.getResourceAsStream("/db.properties");

        Properties prop = new Properties();  //map
        prop.load(in);


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

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

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

}
           

繼續閱讀