由System.getProperty("user.dir")引起的聯想

  • 是什麼:
    • System.getPorperty("user.dir")的功能是獲得當前項目路徑。

 

1    @Test
2     public void test02() throws IOException {
3         String path1 = System.getProperty("user.dir") ;
4         System.out.println("當前工程路徑----"+path1);
5         String path2 = Test01.class.getPackage().getName().replaceAll("//.","/");
6         System.out.println("當前包路徑---"+path2);
7         String canonicalPath = new java.io.File( "." ).getCanonicalPath();
8         System.out.println("canonicalPath---"+canonicalPath);
9     }

  控制檯輸出以下:(ps:這是我本地的項目所在路徑)
  當前工程路徑----D:\remote_medical\02\test
  當前包路徑---com.qtong.test
  canonicalPath---D:\remote_medical\02\test   
  • 爲何:
    • 全部在java.io包中的類,都是將相對路徑名解釋爲起始於用戶當前的工做目錄,因此咱們能夠經過 System.getProperty("user.dir") 來得到當前目錄。可是經過 System.getProperties("user.dir")取得的值是根據咱們的運行環境改變而改變的。
    • 建議不要使用System.getProperties("user.dir");,由於經過鍵"user.dir"取得的值是根據咱們的運行環境改變而改變的。通常狀況下,在服務端都是使用絕對路徑,將絕對路徑定義在"filePath.properties"這樣的配置文件中。對於web開發來講,在服務器端使用相對路徑,是沒有意義的。
  • 用處

  System類屬於java.lang包,system類的構造函數的修飾符是private,因此這個類不能夠被實例化,加載的時候調用static代碼塊。java

  System類提供了標準輸入輸出流,錯誤輸出流,獲取外部屬性和系統環境的方法,加載類庫和文件的方法,快速copy數組的方法;其中out和err的類型是PrintStream.web

  1. system類中的其餘經常使用方法
    1. 標準輸入流         System.in() ;(標準輸入常常與Scanner類與結合 )
    2. 標準輸出流         System.out();
    3. 錯誤輸出流         System.error(); 其中 outerr 的類型是PrintStream
    4. 對外部定義的屬性和環境變量的訪問           System.getProperty("user.dir");
    5. 加載文件和庫的方法          
    6. 快速複製數組的一部分    System.arraycopy(Object src,  int  srcPos, Object dest, int destPos,int length)
    7. System.getProperties()能夠肯定當前的系統屬性,返回值是一個Properties;
    8. System.load(String filename)等同於:System.getProperties().load(String filename)它們的做用是能夠從做爲動態庫德本地文件系統中指定的文件名加載代碼文件。
    9. System.setProperties(Properties propes):將系統屬性設置爲Properties參數;
    10. System.setProperties(String key,String value)等同於System.getProperties().setProperties(String key,String value):設置指定鍵指示的系統屬性
  2. 咱們能夠經過System.getProperties().toString() 看一下目前全部的屬性,都是keyvalue對。
1     @Test
2     public void test01() throws IOException {
3         Properties properties = System.getProperties();
4         String[] split = properties.toString().split(",");
5         int i = 0;
6         for (String string : split) {
7                 System.out.println(string);
8             }
9     }

控制檯輸出以下:
 1     java.runtime.name = Java(TM) SE Runtime Environment,
 2     sun.boot.library.path = D:\aboutDev\java\jdk1.7\jre\bin,
 3     java.vm.version = 24.72- b04,
 4     java.vm.vendor = Oracle Corporation,
 5     java.vendor.url = http://java.oracle.com/,
 6     path.separator = ;,
 7     java.vm.name = Java HotSpot(TM) 64- Bit Server VM,
 8     file.encoding.pkg = sun.io,
 9     user.country = CN,
10     user.script = ,
11     sun.java.launcher = SUN_STANDARD,
12     sun.os.patch.level = Service Pack 1,
13     java.vm.specification.name = Java Virtual Machine Specification,
14     user.dir = D:\remote_medical\02\test, 15     java.runtime.version = 1.7.0_72- b14,
16     java.awt.graphicsenv = sun.awt.Win32GraphicsEnvironment,
17     java.endorsed.dirs = D:\aboutDev\java\jdk1.7\jre\lib\endorsed,
18     os.arch = amd64,
19     java.io.tmpdir = C:\Users\Duchong\AppData\Local\Temp\,
20     line.separator = ,
21     java.vm.specification.vendor = Oracle Corporation,
22     user.variant = ,
23     os.name = Windows 7,
24     sun.jnu.encoding = GBK,
25     java.library.path =「太長了,因此在這裏就不寫了」} 

 

   

看到了一片很好的文章,是對System.getProperty()方法的介紹,是英文的。戳這裏Java.lang.System.getProperty() Method數組

網站在這裏 服務器

相關文章
相關標籤/搜索