JAVA資格認證(關于Java文件路徑問題)

字號:

1.如何獲得當前文件路徑
    常用:
    字符串類型:System.getProperty("user.dir");
    綜合:
    package com.zcjl.test.base;
    import java.io.File;
    public class Test {
    public static void main(String[] args) throws Exception {
    System.out.println(
    Thread.currentThread().getContextClassLoader().getResource(""));
    System.out.println(Test.class.getClassLoader().getResource(""));
    System.out.println(ClassLoader.getSystemResource(""));
    System.out.println(Test.class.getResource(""));
    System.out.println(Test.class.getResource("/"));
    System.out.println(new File("").getAbsolutePath());
    System.out.println(System.getProperty("user.dir"));
    }
    }
    file:/E:/workSpace/javaTest/target/classes/
    file:/E:/workSpace/javaTest/target/classes/
    file:/E:/workSpace/javaTest/target/classes/
    file:/E:/workSpace/javaTest/target/classes/javaAPI/
    file:/E:/workSpace/javaTest/target/classes/
    E:\workSpace\javaTest
    E:\workSpace\javaTest
    2.Web服務中
    (1).Weblogic
    WebApplication的系統(tǒng)文件根目錄是你的weblogic安裝所在根目錄.
    例如:如果你的weblogic安裝在c:\bea\weblogic700……
    那么,你的文件根路徑就是c:\.所以,有兩種方式能夠讓你訪問你的服務器端的文件:a.使用絕對路徑:比如將你的參數(shù)文件放在c:\yourconfig\yourconf.properties,直接使用 new FileInputStream("/yourconfig/yourconf.properties");b.使用相對路徑:相對路徑的根目錄就是你的webapplication的根路徑,即WEB-INF的上一級目錄,將你的參數(shù)文件放在yourwebapp\yourconfig\yourconf.properties,這樣使用:new FileInputStream("yourconfig/yourconf.properties");這兩種方式均可,自己選擇.
    (2).Tomcat
    在類中輸出System.getProperty("user.dir");顯示的是%Tomcat_Home%/bin
    (3).Resin
    不是你的JSP放的相對路徑,是JSP引擎執(zhí)行這個JSP編譯成SERVLET的路徑為根.比如用新建文件法測試File f = new File("a.htm");這個a.htm在resin的安裝目錄下
    (4).如何讀相對路徑哪?
    在Java文件中getResource或getResourceAsStream均可
    例:getClass().getResourceAsStream(filePath);//filePath可以是"/filename",這里的/代表web發(fā)布根路徑下WEB-INF/classes
    也可以getClass().getClassLoader().getResourceAsStream(filePath)//filePath不是帶“/”的
    (5).獲得文件真實路徑
    string file_real_path=request.getRealPath("mypath/filename");
    通常使用request.getRealPath("/");
    3.遺留問題
    目前new FileInputStream()只會使用絕對路徑,相對
    InputStream in1 = new FileInputStream("abc1.properties"); // 相對路徑InputStream in2 = new FileInputStream("/abc2.properties"); // 絕對路徑,E盤下InputStream in3 = new FileInputStream("e://abc3.properties"); //相對路徑
    4.按Java文件類型分類讀取配置文件
    配置文件是應用系統(tǒng)中不可缺少的,可以增加程序的靈活性.java.util.Properties是從jdk1.2就有的類,一直到現(xiàn)在都支持load ()方法,jdk1.4以后save(output,string) ->store(output,string).如果只是單純的讀,根本不存在煩惱的問題.web層可以通過 Thread.currentThread().getContextClassLoader().
    getResourceAsStream("xx.properties") 獲??;
    Application可以通過new FileInputStream("xx.properties");直接在classes一級獲取.關鍵是有時我們需要通過web修改配置文件,我們不 能將路徑寫死了.經(jīng)過測試覺得有以下心得:
    1.servlet中讀寫.如果運用Struts 或者Servlet可以直接在初始化參數(shù)中配置,調(diào)用時根據(jù)servlet的getRealPath("/")獲取真實路徑,再根據(jù)String file = this.servlet.getInitParameter("abc");獲取相對的WEB-INF的相對路徑.
    例:InputStream input = Thread.currentThread().getContextClassLoader().
    getResourceAsStream("abc.properties");Properties prop = new Properties();prop.load(input);input.close();OutputStream out = new FileOutputStream(path);prop.setProperty("abc", “test");prop.store(out, ”–test–");out.close();
    2.直接在jsp中操作,通過jsp內(nèi)置對象獲取可操作的絕對地址.
    例:// jsp頁面String path = pageContext.getServletContext().getRealPath("/");String realPath = path+"/WEB-INF/classes/abc.properties";
    //java 程序InputStream in = getClass().getClassLoader().getResourceAsStream("abc.properties"); // abc.properties放在webroot/WEB-INF/classes/目錄下prop.load(in);in.close();
    OutputStream out = new FileOutputStream(path); // path為通過頁面?zhèn)魅氲穆窂絧rop.setProperty("abc", “abcccccc");prop.store(out, ”–test–");out.close();
    3.只通過Java程序操作資源文件InputStream in = new FileInputStream("abc.properties"); // 相對路徑,項目下的路徑
    OutputStream out = new FileOutputStream("abc.properties");