linux路徑分隔字元'/'與windows下的分隔字元'\\',以及java項目,web項目讀取項目的路徑

來源:互聯網
上載者:User

標籤:sys   put   pat   檔案   this   div   his   new   config   

1, linux下的檔案分隔字元是‘/‘, windows下的檔案分隔字元為‘\‘。但是‘\‘這個符號是轉義符。如果需要在console輸出‘\‘這個符號的,你需要輸入‘\\‘。另外轉義符‘\‘還可以用雙引號的字元內部再次使用雙引號的這種情況,例如下面第三行。

@Test    public void test$(){        System.out.println("\\");        System.out.println(‘/‘);        System.out.println("\"為了轉義雙引號\"");        #分別輸出 "\" , "/", "為了轉義雙引號"        System.out.println("\")       #編譯報錯    }

linux與windows環境下分隔字元的替換

//linux下的檔案分隔字元替換成window的分隔字元filePath =  filePath.replace("/","\\");

2,在web項目中擷取當前項目的絕對路徑:

#tomcat項目擷取運行項目的絕對路徑String proPath = this.getClass().getResource("/").getPath().substring(1) ;#例如:proPath ="D:\projectname\project_parent\web\target\vsp-web\WEB-INF\classes"

3, 在java項目中擷取jar所在的當前路徑,並讀取jar包所在路徑下的配置資訊

#java項目打包成jar後,擷取jar當前路徑的位置,並向項目中匯入config.properties的設定檔Properties prop=new Properties();InputStream in=new FileInputStream(new File(System.getProperty("user.dir")+File.separator+"config.properties")); prop.load(in);

  

linux路徑分隔字元'/'與windows下的分隔字元'\\',以及java項目,web項目讀取項目的路徑

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.