<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

        明月松間照 清泉石上流


                                            ——— 兵臨城下   貓科動物
    posts - 70, comments - 137, trackbacks - 0, articles - 23
      BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

    關(guān)于Java文件路徑問題(轉(zhuǎn))

    Posted on 2006-12-29 20:39 兵臨城下 閱讀(2929) 評論(1)  編輯  收藏 所屬分類: J2EE
    轉(zhuǎn)自:http://blog.csdn.net/soleghost/archive/2006/04/27/679374.aspx

    1.如何獲得當(dāng)前文件路徑

    常用:

    字符串類型: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服務(wù)中

    (1).Weblogic

    WebApplication的系統(tǒng)文件根目錄是你的weblogic安裝所在根目錄。
    例如:如果你的weblogic安裝在c:\bea\weblogic700.....
    那么,你的文件根路徑就是c:\.
    所以,有兩種方式能夠讓你訪問你的服務(wù)器端的文件:
    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文件中g(shù)etResource或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("/");?

    ?4.遺留問題

    目前new FileInputStream()只會使用絕對路徑,相對


    ??InputStream in1 = new FileInputStream("abc1.properties"); // 相對路徑
    ??InputStream in2 = new FileInputStream("/abc2.properties"); // 絕對路徑,E盤下
    ??InputStream in3 = new FileInputStream("e://abc3.properties"); //相對路徑

    5.按Java文件類型分類讀取配置文件

    配 置文件是應(yīng)用系統(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一級獲取。關(guān)鍵是有時我們需要通過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)魅氲穆窂?br />prop.setProperty("abc", “abcccccc");
    prop.store(out, “–test–");
    out.close();

    3.只通過Java程序操作資源文件
    InputStream in = new FileInputStream("abc.properties"); // 相對路徑,項目下的路徑

    OutputStream out = new FileOutputStream("abc.properties");


    評論

    # Podbor Klyuchevyh Slov  回復(fù)  更多評論   

    2009-05-18 11:25 by Podbor Klyuchevyh Slov
    How are you. Everything happens to everybody sooner or later if there is time enough.
    I am from Guinea-Bissau and also now'm speaking English, give true I wrote the following sentence: "We utilize the power of search engines to help people find you.They asked them does advanced white hat seo exist? If I remember right, and this was a long time ago and probably buzzed up so forgive me, every guru said."

    Waiting for a reply :-D, Rayna.
    主站蜘蛛池模板: 亚洲视频在线观看| 亚洲三区在线观看无套内射| 亚洲国产精品无码久久SM| 四虎影视永久在线精品免费| 四虎在线播放免费永久视频| 国产AV日韩A∨亚洲AV电影| 性做久久久久免费观看| mm1313亚洲国产精品无码试看| 好爽又高潮了毛片免费下载| 亚洲色大成网站www久久九| 成人a视频片在线观看免费| 亚洲三级在线观看| 国产青草视频免费观看97| 亚洲AV第一成肉网| 无码欧精品亚洲日韩一区夜夜嗨| 无码的免费不卡毛片视频| 中文亚洲AV片不卡在线观看| 日本三级在线观看免费| 亚洲精品福利网站| 成全影视免费观看大全二| 亚洲AV无码XXX麻豆艾秋| 精品国产亚洲一区二区在线观看| 精品国产福利尤物免费| 777亚洲精品乱码久久久久久| 91精品免费久久久久久久久| 亚洲精品无码你懂的| 亚洲精品无码久久久久AV麻豆| 西西人体免费视频| 亚洲a级成人片在线观看| 日本免费观看网站| 美女巨胸喷奶水视频www免费| 久久亚洲精品人成综合网| 成人免费视频69| 人人爽人人爽人人片A免费| 亚洲成AV人片在线观看| 成人毛片免费观看视频在线| 人人爽人人爽人人片av免费| 亚洲国产精品一区二区久久| 在线免费观看色片| 日本免费A级毛一片| 亚洲爆乳大丰满无码专区|