明月松間照 清泉石上流


                                                  ——— 兵臨城下   貓科動(dòng)物
          posts - 70, comments - 137, trackbacks - 0, articles - 23
            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理
          轉(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.使用絕對(duì)路徑:
          比如將你的參數(shù)文件放在c:\yourconfig\yourconf.properties,
          直接使用 new FileInputStream("/yourconfig/yourconf.properties");
          b.使用相對(duì)路徑:
          相對(duì)路徑的根目錄就是你的webapplication的根路徑,即WEB-INF的上一級(jí)目錄,將你的參數(shù)文件放在yourwebapp\yourconfig\yourconf.properties,
          這樣使用:
          new FileInputStream("yourconfig/yourconf.properties");
          這兩種方式均可,自己選擇。

          (2).Tomcat

          在類中輸出System.getProperty("user.dir");顯示的是%Tomcat_Home%/bin

          (3).Resin

          不是你的JSP放的相對(duì)路徑,是JSP引擎執(zhí)行這個(gè)JSP編譯成SERVLET
          的路徑為根.比如用新建文件法測(cè)試File f = new File("a.htm");
          這個(gè)a.htm在resin的安裝目錄下

          (4).如何讀相對(duì)路徑哪?

          在Java文件中g(shù)etResource或getResourceAsStream均可

          例:getClass().getResourceAsStream(filePath);//filePath可以是"/filename",這里的/代表web發(fā)布根路徑下WEB-INF/classes

          也可以getClass().getClassLoader().getResourceAsStream(filePath)//filePath不是帶“/”的

          (5).獲得文件真實(shí)路徑

          string? file_real_path=request.getRealPath("mypath/filename");?

          通常使用request.getRealPath("/");?

          ?4.遺留問題

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


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

          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一級(jí)獲取。關(guān)鍵是有時(shí)我們需要通過web修改配置文件,我們不 能將路徑寫死了。經(jīng)過測(cè)試覺得有以下心得:

          1.servlet中讀寫。如果運(yùn)用Struts 或者Servlet可以直接在初始化參數(shù)中配置,調(diào)用時(shí)根據(jù)servlet的getRealPath("/")獲取真實(shí)路徑,再根據(jù)String file = this.servlet.getInitParameter("abc");獲取相對(duì)的WEB-INF的相對(duì)路徑。
          例:
          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)置對(duì)象獲取可操作的絕對(duì)地址。
          例:
          // jsp頁(yè)面
          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為通過頁(yè)面?zhèn)魅氲穆窂?br />prop.setProperty("abc", “abcccccc");
          prop.store(out, “–test–");
          out.close();

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

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


          評(píng)論

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

          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.
          主站蜘蛛池模板: 马龙县| 徐汇区| 措美县| 浏阳市| 自贡市| 江源县| 宣汉县| 肃北| 南宁市| 新竹市| 浦东新区| 内乡县| 会理县| 得荣县| 沙坪坝区| 商河县| 方正县| 东辽县| 台北市| 和政县| 巴塘县| 惠安县| 吕梁市| 大竹县| 巩义市| 万荣县| 景泰县| 华蓥市| 长岭县| 冕宁县| 静安区| 开远市| 乌拉特中旗| 龙江县| 东源县| 岳普湖县| 阿拉善左旗| 南丰县| 博白县| 北海市| 稻城县|