java代碼中 路徑符號的寫法

來源:互聯網
上載者:User

標籤:

String path="D:\\建立檔案夾\\2.png";File file=new File(path);System.out.println(file.exists());  String path1="D:/建立檔案夾/2.png";File file1=new File(path);System.out.println(file1.getAbsolutePath()); System.out.println(file1.getCanonicalPath());  

win系統的分隔字元是\,因為需要轉義,所以為 \\

在Java中,File file = New File("E://test.txt")、File file = New File("E:\\test.txt")、File file = New File("E:/test.txt")都可以達到目的,但是這三種寫法有沒有什麼差別呢?

首先先澄清  這個三個都是絕對路徑 沒有相對路徑, E://test.txt 和 E:/test.txt 是一個意思  E:\\text.txt 用這樣的寫法是 \\ 轉義了 ‘\’  .

為什麼有\\ 的寫法 和 / 這樣的寫法呢  ,因為window 預設的是\\ 這樣的寫法  如果你把這樣的寫法放到了linux系統下面,那麼這樣的寫法就是錯誤的了,linux 系統下面只認識/ 的寫法。

File file = New File("E://test.txt")、

File file = New File("E:\\test.txt")、

File file = New File("E:/test.txt")

java代碼中 路徑符號的寫法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.