<address id="zhpbl"></address>
<noframes id="zhpbl">
<address id="zhpbl"><form id="zhpbl"><th id="zhpbl"></th></form></address>

    <em id="zhpbl"></em>

      <address id="zhpbl"><th id="zhpbl"><progress id="zhpbl"></progress></th></address>
      更多精彩內容,歡迎關注:

      視頻號
      視頻號

      抖音
      抖音

      快手
      快手

      微博
      微博

      java獲取路徑

      文檔

      java獲取路徑

      java獲取路徑的方法一共有3種:1、使用System獲取路徑;2、使用當前類的ProtectionDomain或者ClassLoader獲取路徑;3、使用Thread獲取路徑。
      推薦度:
      導讀java獲取路徑的方法一共有3種:1、使用System獲取路徑;2、使用當前類的ProtectionDomain或者ClassLoader獲取路徑;3、使用Thread獲取路徑。

      很多朋友都想知道java如何獲取路徑?下面就一起來了解一下吧~

      方法一:使用System獲取路徑

      方法二:使用當前類的ProtectionDomain或者ClassLoader獲取路徑

      方法三:使用Thread獲取路徑

      方法一:

      示例:

      ?public static? final String URLConfig = System.getProperty("user.dir").replace("bin", "webapps")+"/URLConfig.properties";//這種是將配置文件放到Tomcat的webapps的目錄下

      其中的 System.getProperty("user.dir")? 為獲取用戶當前工作目錄

      java的System.getProperty()方法可以獲取的值

      獲取的代碼示例:

      public???class??SystemProperty?{??
      ????public???static???void??main(String?args[])?{?????
      ????System.out.println("java_vendor:"??+?System.getProperty(?"java.vendor"?));?????
      ????System.out.println("java_vendor_url:"??????
      ?????????????+?System.getProperty("java.vendor.url"?));?????
      ????System.out.println("java_home:"??+?System.getProperty(?"java.home"?));?????
      ????System.out.println("java_class_version:"??????
      ?????????????+?System.getProperty("java.class.version"?));?????
      ????System.out.println("java_class_path:"??????
      ????????????+?System.getProperty("java.class.path"?));?????
      ????System.out.println("os_name:"??+?System.getProperty(?"os.name"?));?????
      ????System.out.println("os_arch:"??+?System.getProperty(?"os.arch"?));?????
      ????System.out.println("os_version:"??+?System.getProperty(?"os.version"?));?????
      ????System.out.println("user_name:"??+?System.getProperty(?"user.name"?));?????
      ????System.out.println("user_home:"??+?System.getProperty(?"user.home"?));?????
      ????System.out.println("user_dir:"??+?System.getProperty(?"user.dir"?));?????
      ????System.out.println("java_vm_specification_version:"??????
      ????????????+?System.getProperty("java.vm.specification.version"?));?????
      ????System.out.println("java_vm_specification_vendor:"??????
      ????????????+?System.getProperty("java.vm.specification.vendor"?));?????
      ????System.out.println("java_vm_specification_name:"??????
      ????????????+?System.getProperty("java.vm.specification.name"?));?????
      ????System.out.println("java_vm_version:"??????
      ????????????+?System.getProperty("java.vm.version"?));?????
      ????System.out.println("java_vm_vendor:"??????
      ????????????+?System.getProperty("java.vm.vendor"?));?????
      ????System.out?????
      ????????????.println("java_vm_name:"??+?System.getProperty(?"java.vm.name"?));?????
      ????System.out.println("java_ext_dirs:"??????
      ????????????+?System.getProperty("java.ext.dirs"?));?????
      ????System.out.println("file_separator:"??????
      ????????????+?System.getProperty("file.separator"?));?????
      ????System.out.println("path_separator:"??????
      ????????????+?System.getProperty("path.separator"?));?????
      ????System.out.println("line_separator:"??????
      ????????????+?System.getProperty("line.separator"?));?????
      }

      方法二:

      public?static?String?getMyDIR(){//獲取當前類文件的絕對路徑
      String?jarWholePath?=?ConfigerPraram.class.getProtectionDomain().getCodeSource().getLocation().getFile();?
      try?{?
      //保險起見,將路徑進行decode轉碼
      jarWholePath?=?java.net.URLDecoder.decode(jarWholePath,?"UTF-8");?
      }?catch?(UnsupportedEncodingException?e)?{?System.out.println(e.toString());?}?
      //獲取jar包的上級目錄
      String?jarPath?=?new?File(jarWholePath).getParentFile().getAbsolutePath();?
      return?jarPath;
      }
      /**
      ?????*?獲取項目所在路徑
      ?????*?
      ?????*?@return
      ?????*/
      ????public?static?String?getRealPath()?{
           //通過類加載器獲取jar包的絕對路徑
      ????????String?realPath?=?MyPath.class.getClassLoader().getResource("")
      ????????????????.getFile();
      ????????java.io.File?file?=?new?java.io.File(realPath);
      ????????realPath?=?file.getParentFile().getAbsolutePath();?//獲取jar包的上級目錄
      try?{
             //路徑decode轉碼
      ????????????realPath?=?java.net.URLDecoder.decode(realPath,?"utf-8");
      ????????}?catch?(Exception?e)?{
      ????????????e.printStackTrace();
      ????????}  
          return?realPath?;?
        }

      方法三:

      Thread.currentThread().getContextClassLoader().getResource("")來得到當前的classpath的絕對路徑的URI表示法

      Application可以通過new FileInputStream("xx.properties");直接在classes一級獲取。關鍵是有時我們需要通過web修改配置文件,我們不 能將路徑寫死了。經過測試覺得有以下心得:

      1.servlet中讀寫。如果運用Struts 或者Servlet可以直接在初始化參數中配置,調用時根據servlet的getRealPath("/")獲取真實路徑,再根據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();
      prop.setProperty("abc",?“test");
      prop.store(new?FileOutputStream(path),?“–test–");?
      out.close();

      2.直接在jsp中操作,通過jsp內置對象獲取可操作的絕對地址。

      例:

      //?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為通過頁面傳入的路徑
      prop.setProperty("abc",?“abcccccc");
      prop.store(out,?“–test–");
      out.close();

      3.只通過Java程序操作資源文件

      InputStream?in?=?new?FileInputStream("abc.properties");?//?相對路徑,項目下的路徑
      OutputStream?out?=?new?FileOutputStream("abc.properties");

      以上就是小編今天的分享,希望能夠幫到大家。

      文檔

      java獲取路徑

      java獲取路徑的方法一共有3種:1、使用System獲取路徑;2、使用當前類的ProtectionDomain或者ClassLoader獲取路徑;3、使用Thread獲取路徑。
      推薦度:
      為你推薦
      資訊專欄
      熱門視頻
      相關推薦
      java cssbox java獲取運行時間 java ctp java獲取隨機字符串 java daemon java獲取隨機數 java dashboard java獲得當前路徑 java表單校驗 java dataframe java規則引擎 java dataset java讀取json java讀取本地excel java decaf java調python java decode java decodeuri java delegate java調用c++接口 java獲取請求域名 java csrf java獲取視頻時長 java crontab java獲取網絡時間 java crash java獲取秒級時間戳 java core java獲取泛型class java corba java cookie java獲取本地ip java controller java獲取本周日期 java context japanese java 微信怎么群發消息 java獲取時間差 java獲取文件名后綴 微博賬號怎么注銷
      Top 少妇高潮太爽了在线视频