明月松間照 清泉石上流


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

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

          Posted on 2006-12-29 20:39 兵臨城下 閱讀(2931) 評論(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.
          主站蜘蛛池模板: 定安县| 女性| 平泉县| 疏附县| 霞浦县| 福鼎市| 镇原县| 沙田区| 额敏县| 泽库县| 安义县| 尚义县| 石柱| 公安县| 永年县| 灵川县| 英吉沙县| 修文县| 平湖市| 金塔县| 长春市| 华宁县| 浦城县| 宜都市| 永登县| 台北市| 宿松县| 武城县| 永仁县| 嘉善县| 吴江市| 隆回县| 会东县| 尼木县| 遂溪县| 铅山县| 石楼县| 西贡区| 保靖县| 柘荣县| 九龙城区|