1擷取webcontent路徑下的設定檔
<span style="font-size:18px;">String path = ParseReader.class.getResource("/").getPath();String websiteUrl = path.replace("/build/classes", "").replace("%20", " ")+ "WebContent/WEB-INF/";System.out.println(websiteUrl);</span>
列印的結果為/D:/workspace/com.phy.smart.pipe.monitor/WebContent/WEB-INF/
/D:/workspace/com.phy.smart.pipe.monitor是我當前的工程本地地址
以上思路是根據ParseReader類的載入擷取到當前工程的地址,這裡/build/classes是web工程在tomcat部署之後編譯的.class檔案路徑,要替換為空白,%20為空白格,%20 其實就是空格,序列化之後就變成%20了%20 其實就是空格,序列化之後就變成%20了
然後就可以任意擷取你webcontent下的檔案路徑了。 2.擷取src路徑下的設定檔
public class PropertiesReader {public static String USERNAME = "nancht";public static String PASSWD = "VprJG8#h==";private static Properties props = new Properties();static {try {InputStream in = new ClassPathResource("/ws.properties").getInputStream();props.load(in);} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}}
3。以下總結三種方法讀取:
//用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(); //mapprop.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.propertiesString filename = realpath.substring(realpath.lastIndexOf("\\")+1);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(); //mapprop.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);}}